Skip to content

Instantly share code, notes, and snippets.

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 XuYanci/bcf4f1b034d60341acf17d370a92e797 to your computer and use it in GitHub Desktop.
Save XuYanci/bcf4f1b034d60341acf17d370a92e797 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