Created
May 27, 2016 07:57
-
-
Save nunogoncalves/027979ddb89d7ebccee7ff42edfa22d3 to your computer and use it in GitHub Desktop.
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
protocol Requestable { | |
associatedtype Element | |
var httpMethod: HTTPMethod { get } | |
var headers: HeadParams? { get } | |
var bodyParams: BodyParams? { get } | |
var encoding: RequestEncoding { get } | |
var url: String { get } | |
func getDataFrom(dictionary: NSDictionary) -> Element | |
func call(success success: Element -> (), failure: ApiResponse -> ()) | |
func fillBodyParams() | |
func fillHeaders() | |
} | |
extension Requestable { | |
func call(success success: Element -> (), failure: ApiResponse -> ()) { | |
let qos = Int(QOS_CLASS_USER_INTERACTIVE.rawValue) | |
dispatch_async(dispatch_get_global_queue(qos, 0)) { | |
var responseHandler = Network.ResponseHandler() | |
responseHandler.failureCallback = { apiResponse in | |
dispatch_async(dispatch_get_main_queue()) { | |
failure(apiResponse) | |
} | |
} | |
responseHandler.successCallback = { dictionary in | |
let data = self.getDataFrom(dictionary) | |
dispatch_async(dispatch_get_main_queue()) { | |
success(data) | |
} | |
} | |
self.fillHeaders() | |
self.fillBodyParams() | |
self.makeTheCall(responseHandler) | |
} | |
} | |
private func makeTheCall(responseHandler: Network.ResponseHandler) { | |
Network.AlamofireCaller(networkResponseHandler: responseHandler).call( | |
url, | |
httpMethod: httpMethod, | |
encoding: encoding, | |
headers: headers, | |
bodyParams: bodyParams) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment