Forked from abhimuralidharan/ChainingMethodsCode.swift
Created
April 28, 2018 02:12
-
-
Save XuYanci/bcf4f1b034d60341acf17d370a92e797 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