Created
June 11, 2018 14:30
-
-
Save ramiresnas/e1becad19da4e78dfba102202a128337 to your computer and use it in GitHub Desktop.
Request in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Router.swift | |
| // TestMasterDetail | |
| // | |
| // Created by Ramires Moreira on 5/20/18. | |
| // Copyright © 2018 Ada 2018. All rights reserved. | |
| // | |
| import Foundation | |
| enum HTTPMethod : String { | |
| case get = "GET" | |
| } | |
| enum Router : String { | |
| case jogos = "/games" | |
| private var prodution : Bool { | |
| return false | |
| } | |
| private var host : String { | |
| if prodution { | |
| return "https://academyworldcup.herokuapp.com/api/v1" | |
| } | |
| return "http://localhost:3000/api/v1" | |
| } | |
| private var path : String { return self.rawValue } | |
| private func buildQueryItem( _ param : (key:String, value:Any) ) -> URLQueryItem { | |
| return URLQueryItem(name: param.key, value: "\(param.value)") | |
| } | |
| func requestWithMethod<T : Decodable >(_ method : HTTPMethod, params : [String:Any]? = nil , completion : ((T?,Error?)->Void)? ) { | |
| let baseUrl = host.appending(path) | |
| var urlComponents = URLComponents(string: baseUrl) | |
| if let params = params { | |
| urlComponents?.queryItems = params.map { buildQueryItem($0) } | |
| } | |
| guard let url = urlComponents?.url else { return} | |
| print(url) | |
| var request = URLRequest(url: url) | |
| request.httpMethod = method.rawValue | |
| URLSession.shared.dataTask(with: request) { (data, _ , error) in | |
| if error != nil { | |
| DispatchQueue.main.async { | |
| completion?(nil,error) | |
| } | |
| } | |
| let decoder = JSONDecoder() | |
| guard let data = data else {return} | |
| do { | |
| let result = try decoder.decode(T.self, from: data) | |
| DispatchQueue.main.async { | |
| completion?(result,nil) | |
| } | |
| }catch{ | |
| print(error) | |
| } | |
| }.resume() | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment