Mercurial > public > simoleon
view Simoleon/Helpers/NetworkHelper.swift @ 156:84137052813d
Refactor code
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sat, 28 Aug 2021 11:15:25 +0100 |
parents | |
children | 0c589138a6f3 |
line wrap: on
line source
// // Request.swift // Simoleon // // Created by Dennis Concepción Martín on 20/07/2021. // import Foundation // MARK: - HTTP Request func httpRequest<T: Decodable>(url: String, model: T.Type, completion: @escaping (_ result: T) -> Void) { // We take some model data T.Type guard let url = URL(string: url) else { print("Invalid URL") return } let request = URLRequest(url: url) URLSession.shared.dataTask(with: request) { data, response, error in if let data = data { do { // Decode response with the model passed let decodedResponse = try JSONDecoder().decode(model, from: data) DispatchQueue.main.async { completion(decodedResponse) } return } catch { // Return error regarding the escaping code print(error) } } // Error with the request print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")") } .resume() }