Skip to content

Commit

Permalink
Reorganize swift files
Browse files Browse the repository at this point in the history
  • Loading branch information
ishkawa committed Mar 26, 2015
1 parent 6da9eda commit e7b50d0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 84 deletions.
12 changes: 12 additions & 0 deletions APIKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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, ); }; };
Expand Down Expand Up @@ -134,6 +138,8 @@
7F45FD561A94D9A9006863BB /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
7F45FD6A1A94D9F9006863BB /* GitHub.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitHub.swift; sourceTree = "<group>"; };
7F45FD731A94E832006863BB /* Models.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = "<group>"; };
7F68ABD91AC4412E00688D68 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = "<group>"; };
7F68ABDC1AC4414500688D68 /* Method.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Method.swift; sourceTree = "<group>"; };
7FAC259C1AA2C00600E92500 /* OHHTTPStubs.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OHHTTPStubs.framework; sourceTree = "<group>"; };
7FAC259E1AA2C01100E92500 /* OHHTTPStubs.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OHHTTPStubs.framework; sourceTree = "<group>"; };
7FCBE9DC1A9734880075AFD9 /* RequestBodyBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestBodyBuilder.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
);
Expand All @@ -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 */,
);
Expand Down
151 changes: 67 additions & 84 deletions APIKit/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any>)?.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<Any>)?.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(),
Expand Down Expand Up @@ -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)
}
Expand All @@ -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<Any>)?.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<Any>)?.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))
}
}
}
}

13 changes: 13 additions & 0 deletions APIKit/Method.swift
Original file line number Diff line number Diff line change
@@ -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"
}
9 changes: 9 additions & 0 deletions APIKit/Request.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

public protocol Request {
typealias Response: Any

var URLRequest: NSURLRequest? { get }

func responseFromObject(object: AnyObject) -> Response?
}

0 comments on commit e7b50d0

Please sign in to comment.