Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
Added Jay for JSON encoding due to poor Linux support of JSONSerializ…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
BennyKJohnson committed Nov 30, 2016
1 parent fc551be commit 285afa0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ let package = Package(
name: "OpenCloudKit",
dependencies: [
.Package(url: "https://github.com/vapor/clibressl.git", majorVersion: 1),
.Package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", majorVersion: 0)

.Package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", majorVersion: 0),
.Package(url: "https://github.com/czechboy0/Jay.git", majorVersion: 1)
]
)

Expand Down
30 changes: 20 additions & 10 deletions Sources/CKURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import Jay

enum CKOperationRequestType: String {
case records
Expand Down Expand Up @@ -68,7 +69,12 @@ class CKURLRequest: NSObject {

if let properties = requestProperties {

let jsonData: Data = try! JSONSerialization.data(withJSONObject: properties, options: [])
// While JSON Parsing doesn't support Swift Types on Linux, Use Jay
//let jsonData: Data = try! JSONSerialization.data(withJSONObject: properties, options: [])

let data = try! Jay(formatting: .prettified).dataFromJson(any: properties) // [UInt8]
let jsonData = Data(bytes: data)

urlRequest.httpBody = jsonData
urlRequest.httpMethod = "POST"
urlRequest.addValue(requestContentType, forHTTPHeaderField: "Content-Type")
Expand Down Expand Up @@ -106,33 +112,37 @@ class CKURLRequest: NSObject {
get {
let accountInfo = accountInfoProvider ?? CloudKit.shared.defaultAccount!

let baseURL = accountInfo.containerInfo.publicCloudDBURL(databaseScope: databaseScope).appendingPathComponent("\(operationType)/\(path)")
var baseURL = accountInfo.containerInfo.publicCloudDBURL(databaseScope: databaseScope).appendingPathComponent("\(operationType)/\(path)").absoluteString

var urlComponents = URLComponents(url: baseURL, resolvingAgainstBaseURL: false)!
// var urlComponents = URLComponents(url: baseURL, resolvingAgainstBaseURL: false)!
switch accountInfo.accountType {
case .server:
break
case .anoymous, .primary:
urlComponents.queryItems = []
// urlComponents.queryItems = []
// if let accountInfo = accountInfoProvider {

let apiTokenItem = URLQueryItem(name: "ckAPIToken", value: accountInfo.cloudKitAuthToken)
urlComponents.queryItems?.append(apiTokenItem)
// urlComponents.queryItems?.append(apiTokenItem)

baseURL += "?ckAPIToken=\(accountInfo.cloudKitAuthToken ?? "")"

if let icloudAuthToken = accountInfo.iCloudAuthToken {

let webAuthTokenQueryItem = URLQueryItem(name: "ckWebAuthToken", value: icloudAuthToken)
urlComponents.queryItems?.append(webAuthTokenQueryItem)
//let webAuthTokenQueryItem = URLQueryItem(name: "ckWebAuthToken", value: icloudAuthToken)
// urlComponents.queryItems?.append(webAuthTokenQueryItem)
let encodedWebAuthToken = icloudAuthToken.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!.replacingOccurrences(of: "+", with: "%2B")
baseURL += "&ckWebAuthToken=\(encodedWebAuthToken)"

}


}

// Perform Encoding
urlComponents.percentEncodedQuery = urlComponents.percentEncodedQuery?.replacingOccurrences(of:"+", with: "%2B")
CloudKit.debugPrint(urlComponents.url!)
return urlComponents.url!
// urlComponents.percentEncodedQuery = urlComponents.percentEncodedQuery?.replacingOccurrences(of:"+", with: "%2B")
//CloudKit.debugPrint(urlComponents.url!)
return URL(string: baseURL)!
}
}

Expand Down

0 comments on commit 285afa0

Please sign in to comment.