Skip to content

Instantly share code, notes, and snippets.

@abhimuralidharan
Last active July 11, 2018 08:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save abhimuralidharan/71abd7e1394478325cd83013e97a4f0c to your computer and use it in GitHub Desktop.
Save abhimuralidharan/71abd7e1394478325cd83013e97a4f0c to your computer and use it in GitHub Desktop.
class APICaller {
var url:URL?
var method = HTTPMethods.get
var params:[String:String]?
enum HTTPMethods: String { // http methods
case get = "GET"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
}
func urlString(_ urlString: String?) -> APICaller {
if let urlString = urlString {
self.url = URL(string: urlString)
}
return self
}
func method(_ method:HTTPMethods) -> APICaller {
self.method = method
return self
}
func parameters(_ params:[String:String]) -> APICaller {
self.params = params
return self
}
func response(response:@escaping([String:AnyObject]) -> Void) {
// create URLRequest with the http method, url, parameters etc and do the api call using URLSession method.. say use DataTask.
let resultDictionary = ["result":["Result values1","Result values2","Result values3","Result values4"]]
// let resultDictionary be the output of the API call made. Now, call the completion closure and pass the value .
//DispatchQueue.main.async {
response(resultDictionary as [String:AnyObject])
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment