forked from ishkawa/APIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIKit.swift
More file actions
209 lines (170 loc) · 8.08 KB
/
Copy pathAPIKit.swift
File metadata and controls
209 lines (170 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Foundation
#if APIKIT_DYNAMIC_FRAMEWORK
import LlamaKit
#endif
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 dataTaskResponseBufferKey = 0
private var dataTaskCompletionHandlerKey = 0
private extension NSURLSessionDataTask {
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))
}
}
}
}
// use private, global scope variable until we can use stored class var in Swift 1.2
private var instancePairDictionary = [String: (API, NSURLSession)]()
private let instancePairSemaphore = dispatch_semaphore_create(1)
public class API: NSObject, NSURLSessionDelegate, NSURLSessionDataDelegate {
// configurations
public class func baseURL() -> NSURL {
fatalError("API.baseURL() must be overrided in subclasses.")
}
public class func requestBodyBuilder() -> RequestBodyBuilder {
return .JSON(writingOptions: nil)
}
public class func responseBodyParser() -> ResponseBodyParser {
return .JSON(readingOptions: nil)
}
public class func URLSessionConfiguration() -> NSURLSessionConfiguration {
return NSURLSessionConfiguration.defaultSessionConfiguration()
}
public class func URLSessionDelegateQueue() -> NSOperationQueue? {
// nil indicates NSURLSession creates its own serial operation queue.
// see doc of NSURLSession.init(configuration:delegate:delegateQueue:) for more details.
return nil
}
// prevent instantiation
override private init() {
super.init()
}
// create session and instance of API for each subclasses
private final class var instancePair: (API, NSURLSession) {
let className = NSStringFromClass(self)
dispatch_semaphore_wait(instancePairSemaphore, DISPATCH_TIME_FOREVER)
let pair: (API, NSURLSession) = instancePairDictionary[className] ?? {
let instance = (self as NSObject.Type)() as API
let configuration = self.URLSessionConfiguration()
let queue = self.URLSessionDelegateQueue()
let session = NSURLSession(configuration: configuration, delegate: instance, delegateQueue: queue)
let pair = (instance, session)
instancePairDictionary[className] = pair
return pair
}()
dispatch_semaphore_signal(instancePairSemaphore)
return pair
}
public final class var instance: API {
return instancePair.0
}
public final class var URLSession: NSURLSession {
return instancePair.1
}
// build NSURLRequest
public class func URLRequest(method: Method, _ path: String, _ parameters: [String: AnyObject] = [:]) -> NSURLRequest? {
if let components = NSURLComponents(URL: baseURL(), resolvingAgainstBaseURL: true) {
let request = NSMutableURLRequest()
switch method {
case .GET, .HEAD, .DELETE:
components.query = URLEncodedSerialization.stringFromObject(parameters, encoding: NSUTF8StringEncoding)
default:
switch requestBodyBuilder().buildBodyFromObject(parameters) {
case .Success(let box):
request.HTTPBody = box.unbox
case .Failure(let box):
return nil
}
}
components.path = (components.path ?? "").stringByAppendingPathComponent(path)
request.URL = components.URL
request.HTTPMethod = method.rawValue
request.setValue(requestBodyBuilder().contentTypeHeader, forHTTPHeaderField: "Content-Type")
request.setValue(responseBodyParser().acceptHeader, forHTTPHeaderField: "Accept")
return request
} else {
return nil
}
}
// send request and build response object
public class func sendRequest<T: Request>(request: T, handler: (Result<T.Response, NSError>) -> Void = {r in}) -> NSURLSessionDataTask? {
let session = URLSession
let mainQueue = dispatch_get_main_queue()
if let URLRequest = request.URLRequest {
let task = session.dataTaskWithRequest(URLRequest)
task.completionHandler = { data, URLResponse, connectionError in
if let error = connectionError {
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
return
}
let statusCode = (URLResponse as? NSHTTPURLResponse)?.statusCode ?? 0
if !contains(200..<300, statusCode) {
let userInfo = [NSLocalizedDescriptionKey: "received status code that represents error"]
let error = NSError(domain: APIKitErrorDomain, code: statusCode, userInfo: userInfo)
dispatch_async(mainQueue, { handler(.Failure(Box(error))) })
return
}
let mappedResponse: Result<T.Response, NSError> = self.responseBodyParser().parseData(data).flatMap { rawResponse in
if let response = request.responseFromObject(rawResponse) {
return success(response)
} else {
let userInfo = [NSLocalizedDescriptionKey: "failed to create model object from raw object."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
return failure(error)
}
}
dispatch_async(mainQueue, { handler(mappedResponse) })
}
task.resume()
return task
} else {
let userInfo = [NSLocalizedDescriptionKey: "failed to build request."]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
dispatch_async(mainQueue, { handler(failure(error)) })
return nil
}
}
// MARK: - NSURLSessionTaskDelegate
// TODO: add attributes like NS_REQUIRES_SUPER when it is available in future version of Swift.
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
// TODO: add attributes like NS_REQUIRES_SUPER when it is available in future version of Swift.
public func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
dataTask.responseBuffer.appendData(data)
}
}