Skip to content

Instantly share code, notes, and snippets.

@justinmakaila
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save justinmakaila/5e73ae4e1e31d9fd34e0 to your computer and use it in GitHub Desktop.

Select an option

Save justinmakaila/5e73ae4e1e31d9fd34e0 to your computer and use it in GitHub Desktop.
Alamofire JSONSerializable Extensions
import Foundation
import Alamofire
import SwiftyJSON
typealias AlamofireResponseCompletion = (NSURLRequest, NSHTTPURLResponse?, AnyObject?, NSError?) -> Void
private let QueueIdentifier: String = "com.alamofire.Alamofire.SerializationQueue"
private let ErrorDomain: String = "com.alamofire.Alamofire.responseJSONSerializable"
// MARK: - Serializer for the API
internal extension Alamofire.Request {
func responseJSONSerializableCollection<T: JSONSerializable>(completionHandler: ((NSURLRequest, NSHTTPURLResponse?, [T]?, Int?, NSError?) -> Void)) -> Self {
let serializationQueue = dispatch_queue_create(QueueIdentifier, DISPATCH_QUEUE_SERIAL)
return responseSwiftyJSON(queue: serializationQueue, options: .MutableContainers, completionHandler: { request, response, JSON, error in
var objects: [T] = []
var requestError: NSError?
if error != nil {
let APIError = Error(json: JSON)
var userInfo = error?.userInfo ?? [:]
userInfo["APIError"] = APIError
requestError = NSError(domain: ErrorDomain, code: error?.code ?? -1, userInfo: userInfo)
} else {
if let collection = JSON.array {
objects = collection.map { T(json: $0) }
}
}
dispatch_async(dispatch_get_main_queue()) {
completionHandler(request, response, objects, -1, requestError)
}
})
}
func responseJSONSerializable<T: JSONSerializable>(completionHandler: ((NSURLRequest, NSHTTPURLResponse?, T?, NSError?) -> Void)) -> Self {
let serializationQueue = dispatch_queue_create(QueueIdentifier, DISPATCH_QUEUE_SERIAL)
return responseSwiftyJSON(queue: serializationQueue, options: .MutableContainers, completionHandler: { request, response, JSON, error in
var object: T?
var requestError: NSError?
if error != nil {
let APIError = Error(json: JSON)
var userInfo = error?.userInfo ?? [:]
userInfo["APIError"] = APIError
requestError = NSError(domain: ErrorDomain, code: error?.code ?? -1, userInfo: userInfo)
} else {
object = T(json: JSON)
}
dispatch_async(dispatch_get_main_queue()) {
completionHandler(request, response, object, requestError)
}
})
}
}
// MARK: - Serializer for Swifty JSON
extension Alamofire.Request {
/**
Adds a handler to be called once the request has finished.
:param: completionHandler A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the SwiftyJSON enum, if one could be created from the URL response and data, and any error produced while creating the SwiftyJSON enum.
:returns: The request.
*/
internal func responseSwiftyJSON(completionHandler: (NSURLRequest, NSHTTPURLResponse?, SwiftyJSON.JSON, NSError?) -> Void) -> Self {
return responseSwiftyJSON(queue: nil, options: NSJSONReadingOptions.AllowFragments, completionHandler: completionHandler)
}
/**
Adds a handler to be called once the request has finished.
:param: queue The queue on which the completion handler is dispatched.
:param: options The JSON serialization reading options. `.AllowFragments` by default.
:param: completionHandler A closure to be executed once the request has finished. The closure takes 4 arguments: the URL request, the URL response, if one was received, the SwiftyJSON enum, if one could be created from the URL response and data, and any error produced while creating the SwiftyJSON enum.
:returns: The request.
*/
internal func responseSwiftyJSON(queue: dispatch_queue_t? = nil, options: NSJSONReadingOptions = .AllowFragments, completionHandler: (NSURLRequest, NSHTTPURLResponse?, JSON, NSError?) -> Void) -> Self {
return response(queue: queue, serializer: Alamofire.Request.JSONResponseSerializer(options: options), completionHandler: { (request, response, object, error) -> Void in
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
var responseJSON: JSON
if object == nil {
responseJSON = JSON.nullJSON
} else {
responseJSON = SwiftyJSON.JSON(object!)
}
dispatch_async(queue ?? dispatch_get_main_queue(), {
completionHandler(self.request, self.response, responseJSON, error)
})
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment