Last active
July 11, 2018 08:10
-
-
Save abhimuralidharan/71abd7e1394478325cd83013e97a4f0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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