From e7b50d037507f2ecc6d02cc0a797bb0f9bc9b238 Mon Sep 17 00:00:00 2001 From: Yosuke Ishikawa Date: Thu, 26 Mar 2015 22:31:04 +0900 Subject: [PATCH] Reorganize swift files --- APIKit.xcodeproj/project.pbxproj | 12 +++ APIKit/API.swift | 151 ++++++++++++++----------------- APIKit/Method.swift | 13 +++ APIKit/Request.swift | 9 ++ 4 files changed, 101 insertions(+), 84 deletions(-) create mode 100644 APIKit/Method.swift create mode 100644 APIKit/Request.swift diff --git a/APIKit.xcodeproj/project.pbxproj b/APIKit.xcodeproj/project.pbxproj index 4612e283..d75dd190 100644 --- a/APIKit.xcodeproj/project.pbxproj +++ b/APIKit.xcodeproj/project.pbxproj @@ -32,6 +32,10 @@ 7F45FD711A94DA2B006863BB /* LlamaKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7F45FD1C1A94D1B4006863BB /* LlamaKit.framework */; }; 7F45FD721A94DA2B006863BB /* LlamaKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 7F45FD1C1A94D1B4006863BB /* LlamaKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 7F45FD741A94E832006863BB /* Models.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F45FD731A94E832006863BB /* Models.swift */; }; + 7F68ABDA1AC4412E00688D68 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F68ABD91AC4412E00688D68 /* Request.swift */; }; + 7F68ABDB1AC4412E00688D68 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F68ABD91AC4412E00688D68 /* Request.swift */; }; + 7F68ABDD1AC4414500688D68 /* Method.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F68ABDC1AC4414500688D68 /* Method.swift */; }; + 7F68ABDE1AC4414500688D68 /* Method.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F68ABDC1AC4414500688D68 /* Method.swift */; }; 7FAC25A01AA2C03400E92500 /* OHHTTPStubs.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7FAC259E1AA2C01100E92500 /* OHHTTPStubs.framework */; }; 7FAC25A11AA2C04000E92500 /* OHHTTPStubs.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7FAC259C1AA2C00600E92500 /* OHHTTPStubs.framework */; }; 7FAC25A21AA2C1D500E92500 /* OHHTTPStubs.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7FAC259C1AA2C00600E92500 /* OHHTTPStubs.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -134,6 +138,8 @@ 7F45FD561A94D9A9006863BB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 7F45FD6A1A94D9F9006863BB /* GitHub.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitHub.swift; sourceTree = ""; }; 7F45FD731A94E832006863BB /* Models.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = ""; }; + 7F68ABD91AC4412E00688D68 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = ""; }; + 7F68ABDC1AC4414500688D68 /* Method.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Method.swift; sourceTree = ""; }; 7FAC259C1AA2C00600E92500 /* OHHTTPStubs.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OHHTTPStubs.framework; sourceTree = ""; }; 7FAC259E1AA2C01100E92500 /* OHHTTPStubs.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OHHTTPStubs.framework; sourceTree = ""; }; 7FCBE9DC1A9734880075AFD9 /* RequestBodyBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestBodyBuilder.swift; sourceTree = ""; }; @@ -224,6 +230,8 @@ children = ( 7F45FCE21A94D02C006863BB /* APIKit.h */, 7F45FD171A94D085006863BB /* API.swift */, + 7F68ABD91AC4412E00688D68 /* Request.swift */, + 7F68ABDC1AC4414500688D68 /* Method.swift */, 7F0869A51A978BCA001AD3E1 /* URLEncodedSerialization.swift */, 7FCBE9DC1A9734880075AFD9 /* RequestBodyBuilder.swift */, 7FCBE9DF1A9734950075AFD9 /* ResponseBodyParser.swift */, @@ -538,6 +546,8 @@ files = ( 7FCBE9DD1A9734880075AFD9 /* RequestBodyBuilder.swift in Sources */, 7F45FD181A94D085006863BB /* API.swift in Sources */, + 7F68ABDD1AC4414500688D68 /* Method.swift in Sources */, + 7F68ABDA1AC4412E00688D68 /* Request.swift in Sources */, 7FCBE9E01A9734950075AFD9 /* ResponseBodyParser.swift in Sources */, 7F0869A61A978BCA001AD3E1 /* URLEncodedSerialization.swift in Sources */, ); @@ -549,6 +559,8 @@ files = ( 7FCBE9DE1A9734880075AFD9 /* RequestBodyBuilder.swift in Sources */, 7F0869A81A979088001AD3E1 /* API.swift in Sources */, + 7F68ABDE1AC4414500688D68 /* Method.swift in Sources */, + 7F68ABDB1AC4412E00688D68 /* Request.swift in Sources */, 7FCBE9E11A9734950075AFD9 /* ResponseBodyParser.swift in Sources */, 7F0869A71A978BCA001AD3E1 /* URLEncodedSerialization.swift in Sources */, ); diff --git a/APIKit/API.swift b/APIKit/API.swift index 00bb5e8d..afc3891a 100644 --- a/APIKit/API.swift +++ b/APIKit/API.swift @@ -6,88 +6,6 @@ import LlamaKit public let APIKitErrorDomain = "APIKitErrorDomain" -public protocol Request { - typealias Response: Any - - var URLRequest: NSURLRequest? { get } - - func responseFromObject(object: AnyObject) -> Response? -} - -public enum Method: String { - case GET = "GET" - case POST = "POST" - case PUT = "PUT" - case HEAD = "HEAD" - case DELETE = "DELETE" - case PATCH = "PATCH" - case TRACE = "TRACE" - case OPTIONS = "OPTIONS" - case CONNECT = "CONNECT" -} - -private var taskRequestKey = 0 -private var dataTaskResponseBufferKey = 0 -private var dataTaskCompletionHandlerKey = 0 - -private extension NSURLSessionDataTask { - // `var request: Request?` is not available in both of Swift 1.1 and 1.2 - // ("protocol can only be used as a generic constraint") - private var request: Any? { - get { - return (objc_getAssociatedObject(self, &taskRequestKey) as? Box)?.unbox - } - - set { - if let value = newValue { - objc_setAssociatedObject(self, &taskRequestKey, Box(value), UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) - } else { - objc_setAssociatedObject(self, &taskRequestKey, nil, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) - } - } - } - - private var responseBuffer: NSMutableData { - if let responseBuffer = objc_getAssociatedObject(self, &dataTaskResponseBufferKey) as? NSMutableData { - return responseBuffer - } else { - let responseBuffer = NSMutableData() - objc_setAssociatedObject(self, &dataTaskResponseBufferKey, responseBuffer, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) - return responseBuffer - } - } - - private var completionHandler: ((NSData, NSURLResponse?, NSError?) -> Void)? { - get { - return (objc_getAssociatedObject(self, &dataTaskCompletionHandlerKey) as? Box<(NSData, NSURLResponse?, NSError?) -> Void>)?.unbox - } - - set { - if let value = newValue { - objc_setAssociatedObject(self, &dataTaskCompletionHandlerKey, Box(value), UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) - } else { - objc_setAssociatedObject(self, &dataTaskCompletionHandlerKey, nil, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) - } - } - } -} - -extension NSURLSessionDownloadTask { - private var request: Any? { - get { - return (objc_getAssociatedObject(self, &taskRequestKey) as? Box)?.unbox - } - - set { - if let value = newValue { - objc_setAssociatedObject(self, &taskRequestKey, Box(value), UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) - } else { - objc_setAssociatedObject(self, &taskRequestKey, nil, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) - } - } - } -} - // use private, global scope variable until we can use stored class var in Swift 1.2 private let internalDefaultURLSession = NSURLSession( configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), @@ -249,15 +167,16 @@ public class API { } } +// MARK: - default implementation of URLSessionDelegate public class URLSessionDelegate: NSObject, NSURLSessionDelegate, NSURLSessionDataDelegate { - // MARK: - NSURLSessionTaskDelegate + // MARK: NSURLSessionTaskDelegate public func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError connectionError: NSError?) { if let dataTask = task as? NSURLSessionDataTask { dataTask.completionHandler?(dataTask.responseBuffer, dataTask.response, connectionError) } } - // MARK: - NSURLSessionDataDelegate + // MARK: NSURLSessionDataDelegate public func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) { dataTask.responseBuffer.appendData(data) } @@ -266,3 +185,67 @@ public class URLSessionDelegate: NSObject, NSURLSessionDelegate, NSURLSessionDat downloadTask.request = dataTask.request } } + +// MARK: - NSURLSessionTask extensions +private var taskRequestKey = 0 +private var dataTaskResponseBufferKey = 0 +private var dataTaskCompletionHandlerKey = 0 + +private extension NSURLSessionDataTask { + // `var request: Request?` is not available in both of Swift 1.1 and 1.2 + // ("protocol can only be used as a generic constraint") + private var request: Any? { + get { + return (objc_getAssociatedObject(self, &taskRequestKey) as? Box)?.unbox + } + + set { + if let value = newValue { + objc_setAssociatedObject(self, &taskRequestKey, Box(value), UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) + } else { + objc_setAssociatedObject(self, &taskRequestKey, nil, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) + } + } + } + + private var responseBuffer: NSMutableData { + if let responseBuffer = objc_getAssociatedObject(self, &dataTaskResponseBufferKey) as? NSMutableData { + return responseBuffer + } else { + let responseBuffer = NSMutableData() + objc_setAssociatedObject(self, &dataTaskResponseBufferKey, responseBuffer, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) + return responseBuffer + } + } + + private var completionHandler: ((NSData, NSURLResponse?, NSError?) -> Void)? { + get { + return (objc_getAssociatedObject(self, &dataTaskCompletionHandlerKey) as? Box<(NSData, NSURLResponse?, NSError?) -> Void>)?.unbox + } + + set { + if let value = newValue { + objc_setAssociatedObject(self, &dataTaskCompletionHandlerKey, Box(value), UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) + } else { + objc_setAssociatedObject(self, &dataTaskCompletionHandlerKey, nil, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) + } + } + } +} + +extension NSURLSessionDownloadTask { + private var request: Any? { + get { + return (objc_getAssociatedObject(self, &taskRequestKey) as? Box)?.unbox + } + + set { + if let value = newValue { + objc_setAssociatedObject(self, &taskRequestKey, Box(value), UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) + } else { + objc_setAssociatedObject(self, &taskRequestKey, nil, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) + } + } + } +} + diff --git a/APIKit/Method.swift b/APIKit/Method.swift new file mode 100644 index 00000000..fea29ad3 --- /dev/null +++ b/APIKit/Method.swift @@ -0,0 +1,13 @@ +import Foundation + +public enum Method: String { + case GET = "GET" + case POST = "POST" + case PUT = "PUT" + case HEAD = "HEAD" + case DELETE = "DELETE" + case PATCH = "PATCH" + case TRACE = "TRACE" + case OPTIONS = "OPTIONS" + case CONNECT = "CONNECT" +} diff --git a/APIKit/Request.swift b/APIKit/Request.swift new file mode 100644 index 00000000..757a248e --- /dev/null +++ b/APIKit/Request.swift @@ -0,0 +1,9 @@ +import Foundation + +public protocol Request { + typealias Response: Any + + var URLRequest: NSURLRequest? { get } + + func responseFromObject(object: AnyObject) -> Response? +}