Skip to content

Instantly share code, notes, and snippets.

@ARamy23
Created June 13, 2018 21:27
Show Gist options
  • Save ARamy23/e85ab6f801d098c36d37755d0429df63 to your computer and use it in GitHub Desktop.
Save ARamy23/e85ab6f801d098c36d37755d0429df63 to your computer and use it in GitHub Desktop.
/// Response completion handler beautified.
typealias CallResponse<T> = ((ServerResponse<T>) -> Void)?
/// API protocol, The alamofire wrapper
protocol APIRequestHandler: HandleAlamoResponse {
/// Calling network layer via (Alamofire), this implementation can be replaced anytime in one place which is the protocol itself, applied anywhere.
///
/// - Parameters:
/// - decoder: Codable confirmed class/struct, Model.type.
/// - completion: Results of the request, general errors cases handled.
/// - Returns: Void.
func send<T: CodableInit>(_ decoder: T.Type, completion: CallResponse<T>)
}
extension APIRequestHandler where Self: URLRequestConvertible {
func send<T: CodableInit>(_ decoder: T.Type, completion: CallResponse<T>) {
request(self).validate().responseData {(response) in
self.handleResponse(response, completion: completion)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment