view Simoleon/Functions/HttpRequest.swift @ 185:2fc95efcb1ee

connect backend
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Wed, 22 Dec 2021 16:12:23 +0100
parents
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()
}