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
198 lines (162 loc) · 7.73 KB
/
Copy pathAPIKit.swift
File metadata and controls
198 lines (162 loc) · 7.73 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
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 let internalDefaultURLSession = NSURLSession(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate: URLSessionDelegate(),
delegateQueue: nil
)
public class API {
// 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 var defaultURLSession: NSURLSession {
return internalDefaultURLSession
}
public class var acceptableStatusCodes: [Int] {
return [Int](200..<300)
}
// 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
}
}
// In Swift 1.1, we could not omit `URLSession` argument of `func send(request:URLSession(=default):handler(=default):)`
// with trailing closure, so we provide following 2 methods
// - `func sendRequest(request:handler(=default):)`
// - `func sendRequest(request:URLSession:handler(=default):)`.
// In Swift 1.2, we can omit default arguments with trailing closure, so they should be replaced with
// - `func sendRequest(request:URLSession(=default):handler(=default):)`
public class func sendRequest<T: Request>(request: T, handler: (Result<T.Response, NSError>) -> Void = {r in}) -> NSURLSessionDataTask? {
return sendRequest(request, URLSession: defaultURLSession, handler: handler)
}
// send request and build response object
public class func sendRequest<T: Request>(request: T, URLSession: NSURLSession, handler: (Result<T.Response, NSError>) -> Void = {r in}) -> NSURLSessionDataTask? {
let mainQueue = dispatch_get_main_queue()
if let URLRequest = request.URLRequest {
let task = URLSession.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(self.acceptableStatusCodes, statusCode) {
let error: NSError = {
switch self.responseBodyParser().parseData(data) {
case .Success(let box): return self.responseErrorFromObject(box.unbox)
case .Failure(let box): return box.unbox
}
}()
dispatch_async(mainQueue) { handler(failure(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
}
}
public class func responseErrorFromObject(object: AnyObject) -> NSError {
let userInfo = [NSLocalizedDescriptionKey: "received status code that represents error"]
let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo)
return error
}
}
public class URLSessionDelegate: NSObject, NSURLSessionDelegate, NSURLSessionDataDelegate {
// 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
public func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
dataTask.responseBuffer.appendData(data)
}
}