NetworkManager.swift 1.89 KB
//
//  NetworkManager.swift
//  InterQR-Internship
//
//  Created by Дмитрий Тимофеев on 25.06.2022.
//

import Foundation
import Alamofire
struct RequestModel: Codable {
    var deviceUUID: String
    var manufacturer: String
    var model: String
    var platform: String
    var osVersion: String
    var appVersion: String
}
class NetworkManager {
    typealias WebServiceResponse = (RequestModel) -> Void
    typealias FailedHandler = (Error) -> Void
    
    func authorizationRequest(_ url: URL, model: RequestModel, completion: @escaping WebServiceResponse, fail: @escaping FailedHandler) {
        
        let params = ["device_uuid": model.deviceUUID,
                      "manufacturer": model.manufacturer,
                      "model": model.model,
                      "platform": model.platform,
                      "os_version": model.osVersion,
                      "app_version": model.appVersion] as [String: Any]
        
        AF.request(url, method: .post, parameters: params, encoding: JSONEncoding.default).responseDecodable(of: RequestModel.self ) { response in
            
            if let error = response.error {
                fail(error)
            } else {
                if let res = response.value {
                completion(res)
                }
            }
        }
        
        //        AF.request(url).response { response in
        //            guard let data = response.data else { return }
        //            do {
        //                let decoder = JSONDecoder()
        //                let json = try decoder.decode(Initialize.self, from: data)
        //
        //                DispatchQueue.main.async {
        //                    completion(json)
        //                }
        //            }
        //            catch {
        //                print("❌\(error)❌")
        //            }
        //        }
    }
}