Mercurial > public > simoleon
view Simoleon/Functions/HttpRequest.swift @ 195:7b009649a063 default tip
Move to mercurial
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Tue, 03 Jun 2025 14:43:48 +0100 |
parents | 2fc95efcb1ee |
children |
line wrap: on
line source
// // HttpRequest.swift // Simoleon // // Created by Dennis Concepción Martín on 21/12/21. // import Foundation // Network request func httpRequest<T: Decodable>(url: String, model: T.Type, completion: @escaping (_ result: T) -> Void) { 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() }