Created
May 4, 2016 16:09
-
-
Save xinmyname/7022754cb6ac953a7c6a23306fd32ac5 to your computer and use it in GitHub Desktop.
My extensions to NSURLSession that handle data/text/json and throw exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSURLSession { | |
enum UTF8Encoding : ErrorType { case Error } | |
enum JsonDecoding : ErrorType { case Error } | |
func dataForRequest(request:NSURLRequest, completeWith: (dataResult: () throws -> NSData) -> Void) { | |
let task = self.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) in | |
guard let data = data else { | |
completeWith(dataResult: { throw error! } ) | |
return | |
} | |
completeWith(dataResult: { return data } ) | |
} | |
task.resume() | |
} | |
func textForRequest(request:NSURLRequest, completeWith: (textResult: () throws -> String) -> Void) { | |
let task = self.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) in | |
guard let data = data else { | |
completeWith(textResult: { throw error! } ) | |
return | |
} | |
completeWith(textResult: { | |
guard let text = String(data: data, encoding: NSUTF8StringEncoding) else { | |
throw UTF8Encoding.Error | |
} | |
return text | |
}) | |
} | |
task.resume() | |
} | |
func jsonForRequest(request:NSURLRequest, completeWith: (jsonResult: () throws -> [String:AnyObject]) -> Void) { | |
let task = self.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) in | |
guard let data = data else { | |
completeWith(jsonResult: { throw error! } ) | |
return | |
} | |
completeWith(jsonResult: { | |
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [String:AnyObject] | |
if json == nil { | |
throw JsonDecoding.Error | |
} | |
return json! | |
}) | |
} | |
task.resume() | |
} | |
func awaitDataForRequest(request:NSURLRequest) throws -> NSData { | |
let wait: dispatch_semaphore_t = dispatch_semaphore_create(0); | |
var resData:NSData? | |
var resError:NSError? | |
let task = self.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) in | |
resData = data | |
resError = error | |
dispatch_semaphore_signal(wait) | |
} | |
task.resume() | |
dispatch_semaphore_wait(wait, DISPATCH_TIME_FOREVER) | |
guard let data = resData else { | |
throw resError! | |
} | |
return data | |
} | |
func awaitTextForRequest(request:NSURLRequest) throws -> String { | |
let data = try self.awaitDataForRequest(request) | |
guard let text = String(data: data, encoding: NSUTF8StringEncoding) else { | |
throw UTF8Encoding.Error | |
} | |
return text | |
} | |
func awaitJsonWithRequest(request:NSURLRequest) throws -> [String:AnyObject] { | |
let data = try self.awaitDataForRequest(request) | |
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [String:AnyObject] | |
if json == nil { | |
throw JsonDecoding.Error | |
} | |
return json! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment