This repository was archived by the owner on Sep 14, 2024. It is now read-only.
forked from daltoniam/SwiftHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPTask.swift
More file actions
513 lines (447 loc) · 21 KB
/
Copy pathHTTPTask.swift
File metadata and controls
513 lines (447 loc) · 21 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// HTTPTask.swift
//
// Created by Dalton Cherry on 6/3/14.
// Copyright (c) 2014 Vluxe. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
import Foundation
/// HTTP Verbs.
///
/// - GET: For GET requests.
/// - POST: For POST requests.
/// - PUT: For PUT requests.
/// - HEAD: For HEAD requests.
/// - DELETE: For DELETE requests.
/// - PATCH: For PATCH requests.
public enum HTTPMethod: String {
case GET = "GET"
case POST = "POST"
case PUT = "PUT"
case HEAD = "HEAD"
case DELETE = "DELETE"
case PATCH = "PATCH"
}
/// Object representation of a HTTP Response.
public class HTTPResponse {
/// The header values in HTTP response.
public var headers: Dictionary<String,String>?
/// The mime type of the HTTP response.
public var mimeType: String?
/// The suggested filename for a downloaded file.
public var suggestedFilename: String?
/// The body or response data of the HTTP Response.
public var responseObject: AnyObject?
/// The status code of the HTTP Response.
public var statusCode: Int?
///Returns the response as a string
public func text() -> String? {
if let d = self.responseObject as? NSData {
return NSString(data: d, encoding: NSUTF8StringEncoding) as? String
}
return nil
}
/// The URL of the HTTP Response.
public var URL: NSURL?
}
/// Holds the blocks of the background task.
class BackgroundBlocks {
// these 2 only get used for background download/upload since they have to be delegate methods
var success:((HTTPResponse) -> Void)?
var failure:((NSError, HTTPResponse?) -> Void)?
var progress:((Double) -> Void)?
/**
Initializes a new Background Block
:param: success The block that is run on a sucessful HTTP Request.
:param: failure The block that is run on a failed HTTP Request.
:param: progress The block that is run on the progress of a HTTP Upload or Download.
*/
init(_ success: ((HTTPResponse) -> Void)?, _ failure: ((NSError, HTTPResponse?) -> Void)?,_ progress: ((Double) -> Void)?) {
self.failure = failure
self.success = success
self.progress = progress
}
}
/// Subclass of NSOperation for handling and scheduling HTTPTask on a NSOperationQueue.
public class HTTPOperation : NSOperation {
private var task: NSURLSessionDataTask!
private var stopped = false
private var running = false
/// Controls if the task is finished or not.
public var done = false
//MARK: Subclassed NSOperation Methods
/// Returns if the task is asynchronous or not. This should always be false.
override public var asynchronous: Bool {
return false
}
/// Returns if the task has been cancelled or not.
override public var cancelled: Bool {
return stopped
}
/// Returns if the task is current running.
override public var executing: Bool {
return running
}
/// Returns if the task is finished.
override public var finished: Bool {
return done
}
/// Returns if the task is ready to be run or not.
override public var ready: Bool {
return !running
}
/// Starts the task.
override public func start() {
super.start()
stopped = false
running = true
done = false
task.resume()
}
/// Cancels the running task.
override public func cancel() {
super.cancel()
running = false
stopped = true
done = true
task.cancel()
}
/// Sets the task to finished.
public func finish() {
self.willChangeValueForKey("isExecuting")
self.willChangeValueForKey("isFinished")
running = false
done = true
self.didChangeValueForKey("isExecuting")
self.didChangeValueForKey("isFinished")
}
}
/// Configures NSURLSession Request for HTTPOperation. Also provides convenience methods for easily running HTTP Request.
public class HTTPTask : NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {
var backgroundTaskMap = Dictionary<String,BackgroundBlocks>()
//var sess: NSURLSession?
public var baseURL: String?
public var requestSerializer = HTTPRequestSerializer()
public var responseSerializer: HTTPResponseSerializer?
//This gets called on auth challenges. If nil, default handling is use.
//Returning nil from this method will cause the request to be rejected and cancelled
public var auth:((NSURLAuthenticationChallenge) -> NSURLCredential?)?
//MARK: Public Methods
/// A newly minted HTTPTask for your enjoyment.
public override init() {
super.init()
}
/**
Creates a HTTPOperation that can be scheduled on a NSOperationQueue. Called by convenience HTTP verb methods below.
:param: url The url you would like to make a request to.
:param: method The HTTP method/verb for the request.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: success The block that is run on a sucessful HTTP Request.
:param: failure The block that is run on a failed HTTP Request.
:returns: A freshly constructed HTTPOperation to add to your NSOperationQueue.
*/
public func create(url: String, method: HTTPMethod, parameters: Dictionary<String,AnyObject>!, success:((HTTPResponse) -> Void)!, failure:((NSError, HTTPResponse?) -> Void)!) -> HTTPOperation? {
let serialReq = createRequest(url, method: method, parameters: parameters)
if serialReq.error != nil {
if failure != nil {
failure(serialReq.error!, nil)
}
return nil
}
let opt = HTTPOperation()
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.dataTaskWithRequest(serialReq.request,
completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
opt.finish()
if error != nil {
if failure != nil {
failure(error, nil)
}
return
}
if data != nil {
var responseObject: AnyObject = data
if self.responseSerializer != nil {
let resObj = self.responseSerializer!.responseObjectFromResponse(response, data: data)
if resObj.error != nil {
if failure != nil {
failure(resObj.error!, nil)
}
return
}
if resObj.object != nil {
responseObject = resObj.object!
}
}
var extraResponse = HTTPResponse()
if let hresponse = response as? NSHTTPURLResponse {
extraResponse.headers = hresponse.allHeaderFields as? Dictionary<String,String>
extraResponse.mimeType = hresponse.MIMEType
extraResponse.suggestedFilename = hresponse.suggestedFilename
extraResponse.statusCode = hresponse.statusCode
extraResponse.URL = hresponse.URL
}
extraResponse.responseObject = responseObject
if extraResponse.statusCode > 299 {
if failure != nil {
failure(self.createError(extraResponse.statusCode!), extraResponse)
}
} else if success != nil {
success(extraResponse)
}
} else if failure != nil {
failure(error, nil)
}
})
opt.task = task
return opt
}
/**
Creates a HTTPOperation as a HTTP GET request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: success The block that is run on a sucessful HTTP Request.
:param: failure The block that is run on a failed HTTP Request.
*/
public func GET(url: String, parameters: Dictionary<String,AnyObject>?, success:((HTTPResponse) -> Void)!, failure:((NSError, HTTPResponse?) -> Void)!) {
var opt = self.create(url, method:.GET, parameters: parameters,success: success,failure: failure)
if opt != nil {
opt!.start()
}
}
/**
Creates a HTTPOperation as a HTTP POST request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: success The block that is run on a sucessful HTTP Request.
:param: failure The block that is run on a failed HTTP Request.
*/
public func POST(url: String, parameters: Dictionary<String,AnyObject>?, success:((HTTPResponse) -> Void)!, failure:((NSError, HTTPResponse?) -> Void)!) {
var opt = self.create(url, method:.POST, parameters: parameters,success: success,failure: failure)
if opt != nil {
opt!.start()
}
}
/**
Creates a HTTPOperation as a HTTP PATCH request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: success The block that is run on a sucessful HTTP Request.
:param: failure The block that is run on a failed HTTP Request.
*/
public func PATCH(url: String, parameters: Dictionary<String,AnyObject>?, success:((HTTPResponse) -> Void)!, failure:((NSError, HTTPResponse?) -> Void)!) {
var opt = self.create(url, method:.PATCH, parameters: parameters,success: success,failure: failure)
if opt != nil {
opt!.start()
}
}
/**
Creates a HTTPOperation as a HTTP PUT request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: success The block that is run on a sucessful HTTP Request.
:param: failure The block that is run on a failed HTTP Request.
*/
public func PUT(url: String, parameters: Dictionary<String,AnyObject>?, success:((HTTPResponse) -> Void)!, failure:((NSError, HTTPResponse?) -> Void)!) {
var opt = self.create(url, method:.PUT, parameters: parameters,success: success,failure: failure)
if opt != nil {
opt!.start()
}
}
/**
Creates a HTTPOperation as a HTTP DELETE request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: success The block that is run on a sucessful HTTP Request.
:param: failure The block that is run on a failed HTTP Request.
*/
public func DELETE(url: String, parameters: Dictionary<String,AnyObject>?, success:((HTTPResponse) -> Void)!, failure:((NSError, HTTPResponse?) -> Void)!) {
var opt = self.create(url, method:.DELETE, parameters: parameters,success: success,failure: failure)
if opt != nil {
opt!.start()
}
}
/**
Creates a HTTPOperation as a HTTP HEAD request and starts it for you.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: success The block that is run on a sucessful HTTP Request.
:param: failure The block that is run on a failed HTTP Request.
*/
public func HEAD(url: String, parameters: Dictionary<String,AnyObject>?, success:((HTTPResponse) -> Void)!, failure:((NSError, HTTPResponse?) -> Void)!) {
var opt = self.create(url, method:.HEAD, parameters: parameters,success: success,failure: failure)
if opt != nil {
opt!.start()
}
}
/**
Creates and starts a HTTPOperation to download a file in the background.
:param: url The url you would like to make a request to.
:param: parameters The parameters are HTTP parameters you would like to send.
:param: progress The progress returned in the progress block is between 0 and 1.
:param: success The block that is run on a sucessful HTTP Request. The HTTPResponse responseObject object will be a fileURL. You MUST copy the fileURL return in HTTPResponse.responseObject to a new location before using it (e.g. your documents directory).
:param: failure The block that is run on a failed HTTP Request.
*/
public func download(url: String, parameters: Dictionary<String,AnyObject>?,progress:((Double) -> Void)!, success:((HTTPResponse) -> Void)!, failure:((NSError, HTTPResponse?) -> Void)!) -> NSURLSessionDownloadTask? {
let serialReq = createRequest(url,method: .GET, parameters: parameters)
if serialReq.error != nil {
failure(serialReq.error!, nil)
return nil
}
let ident = createBackgroundIdent()
let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(ident)
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
let task = session.downloadTaskWithRequest(serialReq.request)
self.backgroundTaskMap[ident] = BackgroundBlocks(success,failure,progress)
//this does not have to be queueable as Apple's background dameon *should* handle that.
task.resume()
return task
}
//TODO: not implemented yet.
/// not implemented yet.
public func uploadFile(url: String, parameters: Dictionary<String,AnyObject>?, progress:((Double) -> Void)!, success:((HTTPResponse) -> Void)!, failure:((NSError) -> Void)!) -> Void {
let serialReq = createRequest(url,method: .GET, parameters: parameters)
if serialReq.error != nil {
failure(serialReq.error!)
return
}
let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(createBackgroundIdent())
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
//session.uploadTaskWithRequest(serialReq.request, fromData: nil)
}
//MARK: Private Helper Methods
/**
Creates and starts a HTTPOperation to download a file in the background.
:param: url The url you would like to make a request to.
:param: method The HTTP method/verb for the request.
:param: parameters The parameters are HTTP parameters you would like to send.
:returns: A NSURLRequest from configured requestSerializer.
*/
private func createRequest(url: String, method: HTTPMethod, parameters: Dictionary<String,AnyObject>!) -> (request: NSURLRequest, error: NSError?) {
var urlVal = url
//probably should change the 'http' to something more generic
if !url.hasPrefix("http") && self.baseURL != nil {
var split = url.hasPrefix("/") ? "" : "/"
urlVal = "\(self.baseURL!)\(split)\(url)"
}
if let u = NSURL(string: urlVal) {
return self.requestSerializer.createRequest(u, method: method, parameters: parameters)
}
return (NSURLRequest(),createError(-1001))
}
/**
Creates a random string to use for the identifier of the background download/upload requests.
:returns: Identifier String.
*/
private func createBackgroundIdent() -> String {
let letters = "abcdefghijklmnopqurstuvwxyz"
var str = ""
for var i = 0; i < 14; i++ {
let start = Int(arc4random() % 14)
str.append(letters[advance(letters.startIndex,start)])
}
return "com.vluxe.swifthttp.request.\(str)"
}
/**
Creates a random string to use for the identifier of the background download/upload requests.
:param: code Code for error.
:returns: An NSError.
*/
private func createError(code: Int) -> NSError {
var text = "An error occured"
if code == 404 {
text = "Page not found"
} else if code == 401 {
text = "Access denied"
} else if code == -1001 {
text = "Invalid URL"
}
return NSError(domain: "HTTPTask", code: code, userInfo: [NSLocalizedDescriptionKey: text])
}
/**
Creates a random string to use for the identifier of the background download/upload requests.
:param: identifier The identifier string.
:returns: An NSError.
*/
private func cleanupBackground(identifier: String) {
self.backgroundTaskMap.removeValueForKey(identifier)
}
//MARK: NSURLSession Delegate Methods
/// Method for authentication challenge.
public func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
if let a = auth {
let cred = a(challenge)
if let c = cred {
completionHandler(.UseCredential, c)
return
}
completionHandler(.RejectProtectionSpace, nil)
return
}
completionHandler(.PerformDefaultHandling, nil)
}
/// Called when the background task failed.
public func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if let err = error {
let blocks = self.backgroundTaskMap[session.configuration.identifier]
if blocks?.failure != nil { //Swift bug. Can't use && with block (radar: 17469794)
blocks?.failure!(err, nil)
cleanupBackground(session.configuration.identifier)
}
}
}
/// The background download finished and reports the url the data was saved to.
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) {
let blocks = self.backgroundTaskMap[session.configuration.identifier]
if blocks?.success != nil {
var resp = HTTPResponse()
if let hresponse = downloadTask.response as? NSHTTPURLResponse {
resp.headers = hresponse.allHeaderFields as? Dictionary<String,String>
resp.mimeType = hresponse.MIMEType
resp.suggestedFilename = hresponse.suggestedFilename
resp.statusCode = hresponse.statusCode
resp.URL = hresponse.URL
}
resp.responseObject = location
if resp.statusCode > 299 {
if blocks?.failure != nil {
blocks?.failure!(self.createError(resp.statusCode!), resp)
}
return
}
blocks?.success!(resp)
cleanupBackground(session.configuration.identifier)
}
}
/// Will report progress of background download
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let increment = 100.0/Double(totalBytesExpectedToWrite)
var current = (increment*Double(totalBytesWritten))*0.01
if current > 1 {
current = 1;
}
let blocks = self.backgroundTaskMap[session.configuration.identifier]
if blocks?.progress != nil {
blocks?.progress!(current)
}
}
/// The background download finished, don't have to really do anything.
public func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
}
//TODO: not implemented yet.
/// not implemented yet. The background upload finished and reports the response data (if any).
func URLSession(session: NSURLSession!, dataTask: NSURLSessionDataTask!, didReceiveData data: NSData!) {
//add upload finished logic
}
//TODO: not implemented yet.
/// not implemented yet.
public func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
//add progress block logic
}
//TODO: not implemented yet.
/// not implemented yet.
func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
}
}