I built an APIManager
class that has function to make API calls to my API that is working fine. Then I use a class APIClient
to store every calls that I need to do on my API with the specific parameters such as url queries or POST data.
The problem is that these function are void functions and return nothing. I'm wondering how I can modify this to return data that I can store in a variable in my ViewController
.
Here is the function that I use in APIManager
:
func makeRequest(toURL url: URL,
withHttpMethod httpMethod: HttpMethod,
completion: @escaping (_ result: Results) -> Void) {
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
let targetURL = self?.addURLQueryParameters(toURL: url)
let httpBody = self?.getHttpBody()
guard let request = self?.prepareRequest(withURL: targetURL, httpBody: httpBody, httpMethod: httpMethod) else
{
completion(Results(withError: CustomError.failedToCreateRequest))
return
}
let sessionConfiguration = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfiguration)
let task = session.dataTask(with: request) { (data, response, error) in
completion(Results(withData: data,
response: Response(fromURLResponse: response),
error: error))
}
task.resume()
}
}
Then in my ViewController I define my APIClient
and calls one function on button tap :
let apiClient = APIClient()
@IBAction func testTapped(_ sender: Any) {
apiClient.getData()
}
and then my APIClient
handles the call :
func getData() {
var decodedResponse: Any?
guard let url = URL(string: "https://XX.API.XX/XX//XXX/XXX/XXX") else { return }
apiManager.makeRequest(toURL: url, withHttpMethod: .get) { (results) in
guard let response = results.response else { return }
if response.httpStatusCode == 200 {
guard let data = results.data else { return }
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(Data.self, from: data) else { return }
}
}
}
Does anybody has a clue on which way I can modify these files so I can return decodedData
from APIClient
and store it in my ViewController
?
question from:
https://stackoverflow.com/questions/66064562/how-to-pass-data-from-an-api-call-to-the-viewcontroller 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…