Skip to content

Instantly share code, notes, and snippets.

@nunogoncalves
Created May 27, 2016 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nunogoncalves/027979ddb89d7ebccee7ff42edfa22d3 to your computer and use it in GitHub Desktop.
Save nunogoncalves/027979ddb89d7ebccee7ff42edfa22d3 to your computer and use it in GitHub Desktop.
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