Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Commit

Permalink
Initial Swift 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ishkawa committed Sep 21, 2016
1 parent dd225a8 commit 8ca53e2
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 205 deletions.
4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "antitypical/Result" "2.1.3"
github "ishkawa/APIKit" "2.0.5"
github "antitypical/Result" "3.0.0"
github "ishkawa/APIKit" "3.0.0-beta.1"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/APIKit
Submodule APIKit updated 55 files
+1 −1 .gitmodules
+12 −7 .travis.yml
+47 −51 APIKit.xcodeproj/project.pbxproj
+1 −1 Cartfile
+1 −1 Cartfile.private
+2 −2 Cartfile.resolved
+1 −1 Carthage/Checkouts/OHHTTPStubs
+1 −1 Carthage/Checkouts/Result
+16 −16 Demo.playground/Contents.swift
+1 −1 Documentation/APIKit2MigrationGuide.md
+0 −0 Sources/BodyParameters/AbstractInputStream.m
+20 −0 Sources/BodyParameters/BodyParameters.swift
+36 −0 Sources/BodyParameters/Data+InputStream.swift
+7 −7 Sources/BodyParameters/FormURLEncodedBodyParameters.swift
+9 −9 Sources/BodyParameters/JSONBodyParameters.swift
+76 −77 Sources/BodyParameters/MultipartFormDataBodyParameters.swift
+0 −20 Sources/BodyParametersType/BodyParametersType.swift
+0 −38 Sources/BodyParametersType/NSData+NSInputStream.swift
+0 −9 Sources/Box.swift
+13 −13 Sources/CallbackQueue.swift
+11 −0 Sources/DataParser/DataParser.swift
+13 −13 Sources/DataParser/FormURLEncodedDataParser.swift
+29 −0 Sources/DataParser/JSONDataParser.swift
+33 −0 Sources/DataParser/StringDataParser.swift
+0 −11 Sources/DataParserType/DataParserType.swift
+0 −29 Sources/DataParserType/JSONDataParser.swift
+0 −33 Sources/DataParserType/StringDataParser.swift
+6 −6 Sources/Error/RequestError.swift
+9 −9 Sources/Error/ResponseError.swift
+7 −7 Sources/Error/SessionTaskError.swift
+10 −10 Sources/HTTPMethod.swift
+148 −0 Sources/Request.swift
+0 −154 Sources/RequestType.swift
+31 −31 Sources/Serializations/URLEncodedSerialization.swift
+37 −35 Sources/Session.swift
+18 −0 Sources/SessionAdapter/SessionAdapter.swift
+75 −0 Sources/SessionAdapter/URLSessionAdapter.swift
+0 −75 Sources/SessionAdapterType/NSURLSessionAdapter.swift
+0 −18 Sources/SessionAdapterType/SessionAdapterType.swift
+2 −2 Tests/APIKit/BodyParametersType/FormURLEncodedBodyParametersTests.swift
+6 −6 Tests/APIKit/BodyParametersType/JSONBodyParametersTests.swift
+27 −27 Tests/APIKit/BodyParametersType/MultipartFormDataParametersTests.swift
+18 −18 Tests/APIKit/BodyParametersType/URLEncodedSerializationTests.swift
+8 −8 Tests/APIKit/DataParserType/FormURLEncodedDataParserTests.swift
+2 −2 Tests/APIKit/DataParserType/JSONDataParserTests.swift
+8 −8 Tests/APIKit/DataParserType/StringDataParserTests.swift
+94 −94 Tests/APIKit/RequestTests.swift
+0 −62 Tests/APIKit/SessionAdapterType/NSURLSessionAdapterSubclassTests.swift
+62 −0 Tests/APIKit/SessionAdapterType/URLSessionAdapterSubclassTests.swift
+28 −28 Tests/APIKit/SessionAdapterType/URLSessionAdapterTests.swift
+32 −32 Tests/APIKit/SessionCallbackQueueTests.swift
+70 −70 Tests/APIKit/SessionTests.swift
+14 −14 Tests/APIKit/TestComponents/TestRequest.swift
+14 −14 Tests/APIKit/TestComponents/TestSessionAdapter.swift
+2 −2 Tests/APIKit/TestComponents/TestSessionTask.swift
4 changes: 2 additions & 2 deletions Example/Example/BatchRequestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class BatchRequestViewController: UIViewController {

Session.sendRequest(httpRequest) { [weak self] result in
switch result {
case .Success(let subtractAnswer, let multiplyAnswer):
case .success(let subtractAnswer, let multiplyAnswer):
self?.subtractAnswerLabel.text = "\(subtractAnswer)"
self?.multiplyAnswerLabel.text = "\(multiplyAnswer)"

case .Failure(let error):
case .failure(let error):
self?.subtractAnswerLabel.text = "?"
self?.multiplyAnswerLabel.text = "?"
self?.showAlertWithError(error)
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/SingleRequestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class SingleRequestViewController: UIViewController {

Session.sendRequest(httpRequest) { [weak self] result in
switch result {
case .Success(let answer):
case .success(let answer):
self?.subtractAnswerLabel.text = "\(answer)"

case .Failure(let error):
case .failure(let error):
self?.subtractAnswerLabel.text = "?"
self?.showAlertWithError(error)
}
Expand Down
8 changes: 4 additions & 4 deletions JSONRPCKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "-.Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -295,7 +295,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "-.Tests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down Expand Up @@ -409,7 +409,7 @@
PRODUCT_NAME = "$(PROJECT_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -428,7 +428,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.bricklife.ios.JSONRPCKit;
PRODUCT_NAME = "$(PROJECT_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
75 changes: 40 additions & 35 deletions JSONRPCKit/BatchElementType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,109 +15,114 @@ public protocol BatchElementType {
var request: Request { get }
var version: String { get }
var id: Id? { get }
var body: AnyObject { get }
var body: Any { get }

func responseFromObject(object: AnyObject) throws -> Request.Response
func responseFromBatchObjects(objects: [AnyObject]) throws -> Request.Response
func responseFromObject(_ object: Any) throws -> Request.Response
func responseFromBatchObjects(_ objects: [Any]) throws -> Request.Response

func resultFromObject(object: AnyObject) -> Result<Request.Response, JSONRPCError>
func resultFromBatchObjects(objects: [AnyObject]) -> Result<Request.Response, JSONRPCError>
func resultFromObject(_ object: Any) -> Result<Request.Response, JSONRPCError>
func resultFromBatchObjects(_ objects: [Any]) -> Result<Request.Response, JSONRPCError>
}

public extension BatchElementType {
/// - Throws: JSONRPCError
public func responseFromObject(object: AnyObject) throws -> Request.Response {
public func responseFromObject(_ object: Any) throws -> Request.Response {
switch resultFromObject(object) {
case .Success(let response):
case .success(let response):
return response

case .Failure(let error):
case .failure(let error):
throw error
}
}

/// - Throws: JSONRPCError
public func responseFromBatchObjects(objects: [AnyObject]) throws -> Request.Response {
public func responseFromBatchObjects(_ objects: [Any]) throws -> Request.Response {
switch resultFromBatchObjects(objects) {
case .Success(let response):
case .success(let response):
return response

case .Failure(let error):
case .failure(let error):
throw error
}
}

public func resultFromObject(object: AnyObject) -> Result<Request.Response, JSONRPCError> {
let receivedVersion = object["jsonrpc"] as? String
public func resultFromObject(_ object: Any) -> Result<Request.Response, JSONRPCError> {
guard let dictionary = object as? [String: Any] else {
fatalError("FIXME")
}

let receivedVersion = dictionary["jsonrpc"] as? String
guard version == receivedVersion else {
return .Failure(.UnsupportedVersion(receivedVersion))
return .failure(.unsupportedVersion(receivedVersion))
}

guard id == object["id"].flatMap(Id.init) else {
return .Failure(.ResponseNotFound(requestId: id, object: object))
guard id == dictionary["id"].flatMap(Id.init) else {
return .failure(.responseNotFound(requestId: id, object: dictionary))
}

let resultObject: AnyObject? = object["result"]
let errorObject: AnyObject? = object["error"]
let resultObject = dictionary["result"]
let errorObject = dictionary["error"]

switch (resultObject, errorObject) {
case (nil, let errorObject?):
return .Failure(JSONRPCError(errorObject: errorObject))
return .failure(JSONRPCError(errorObject: errorObject))

case (let resultObject?, nil):
do {
return .Success(try request.responseFromResultObject(resultObject))
return .success(try request.responseFromResultObject(resultObject))
} catch {
return .Failure(.ResultObjectParseError(error))
return .failure(.resultObjectParseError(error))
}

default:
return .Failure(.MissingBothResultAndError(object))
return .failure(.missingBothResultAndError(dictionary))
}
}

public func resultFromBatchObjects(objects: [AnyObject]) -> Result<Request.Response, JSONRPCError> {
public func resultFromBatchObjects(_ objects: [Any]) -> Result<Request.Response, JSONRPCError> {
let matchedObject = objects
.flatMap { $0 as? [String: Any] }
.filter { $0["id"].flatMap(Id.init) == id }
.first

guard let object = matchedObject else {
return .Failure(.ResponseNotFound(requestId: id, object: objects))
return .failure(.responseNotFound(requestId: id, object: objects))
}

return resultFromObject(object)
}
}

public extension BatchElementType where Request.Response == Void {
public func responseFromObject(object: AnyObject) throws -> Request.Response {
public func responseFromObject(_ object: Any) throws -> Request.Response {
return ()
}

public func responseFromBatchObjects(objects: [AnyObject]) throws -> Request.Response {
public func responseFromBatchObjects(_ objects: [Any]) throws -> Request.Response {
return ()
}

public func resultFromObject(object: AnyObject) -> Result<Request.Response, JSONRPCError> {
return .Success()
public func resultFromObject(_ object: Any) -> Result<Request.Response, JSONRPCError> {
return .success()
}

public func resultFromBatchObjects(objects: [AnyObject]) -> Result<Request.Response, JSONRPCError> {
return .Success()
public func resultFromBatchObjects(_ objects: [Any]) -> Result<Request.Response, JSONRPCError> {
return .success()
}
}

public struct BatchElement<Request: RequestType>: BatchElementType {
public let request: Request
public let version: String
public let id: Id?
public let body: AnyObject
public let body: Any

public init(request: Request, version: String, id: Id) {
let id: Id? = request.isNotification ? nil : id
var body: [String: AnyObject] = [
"jsonrpc": version,
"method": request.method,
var body: [String: Any] = [
"jsonrpc": version as Any,
"method": request.method as Any,
]

if let id = id {
Expand All @@ -135,7 +140,7 @@ public struct BatchElement<Request: RequestType>: BatchElementType {
self.request = request
self.version = version
self.id = id
self.body = body
self.body = body as Any
}
}

20 changes: 10 additions & 10 deletions JSONRPCKit/BatchFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@ public final class BatchFactory {
public let version: String
public var idGenerator: IdGeneratorType

private let semaphore = dispatch_semaphore_create(1)
fileprivate let semaphore = DispatchSemaphore(value: 1)

public init(version: String = "2.0", idGenerator: IdGeneratorType = NumberIdGenerator()) {
self.version = version
self.idGenerator = idGenerator
}

public func create<Request: RequestType>(request: Request) -> Batch<Request> {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
public func create<Request: RequestType>(_ request: Request) -> Batch<Request> {
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
let batchElement = BatchElement(request: request, version: version, id: idGenerator.next())
dispatch_semaphore_signal(semaphore)
semaphore.signal()

return Batch(batchElement: batchElement)
}

public func create<Request1: RequestType, Request2: RequestType>(request1: Request1, _ request2: Request2) -> Batch2<Request1, Request2> {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
public func create<Request1: RequestType, Request2: RequestType>(_ request1: Request1, _ request2: Request2) -> Batch2<Request1, Request2> {
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
let batchElement1 = BatchElement(request: request1, version: version, id: idGenerator.next())
let batchElement2 = BatchElement(request: request2, version: version, id: idGenerator.next())
dispatch_semaphore_signal(semaphore)
semaphore.signal()

return Batch2(batchElement1: batchElement1, batchElement2: batchElement2)
}

public func create<Request1: RequestType, Request2: RequestType, Request3: RequestType>(request1: Request1, _ request2: Request2, _ request3: Request3) -> Batch3<Request1, Request2, Request3> {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
public func create<Request1: RequestType, Request2: RequestType, Request3: RequestType>(_ request1: Request1, _ request2: Request2, _ request3: Request3) -> Batch3<Request1, Request2, Request3> {
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
let batchElement1 = BatchElement(request: request1, version: version, id: idGenerator.next())
let batchElement2 = BatchElement(request: request2, version: version, id: idGenerator.next())
let batchElement3 = BatchElement(request: request3, version: version, id: idGenerator.next())
dispatch_semaphore_signal(semaphore)
semaphore.signal()

return Batch3(batchElement1: batchElement1, batchElement2: batchElement2, batchElement3: batchElement3)
}
Expand Down
Loading

0 comments on commit 8ca53e2

Please sign in to comment.