Created
May 3, 2017 20:19
-
-
Save douglastaquary/bebde2418c7e4a142a1d264a2cac49a0 to your computer and use it in GitHub Desktop.
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
| case cartUpdate(PUQueryParameters, PUQueryParameters, S.Type) | |
| // 2 - Criamos a variavel computada que vai processar os dados pra compor o body | |
| public var body: [String: Any]? { | |
| switch self { | |
| case .cartUpdate(let bodyParams): | |
| return bodyParams.count > 0 ? bodyParams : nil | |
| default: return nil | |
| } | |
| } | |
| // 3 - Por fim, na criação do request, se rolar um body a gente transforma ele em Data e adiciona ao request como body | |
| // Obs: fazemos isso logo antes de retornar a request mesmo | |
| var request:URLRequest? { | |
| let baseUrlPath = "\(basePath)/\(path)" | |
| ... | |
| if let body = self.body { | |
| if let bodyData = try? JSONSerialization.data(withJSONObject: body, options: []) { | |
| request.httpBody = bodyData | |
| request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") | |
| } | |
| } | |
| return request | |
| } |
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
| enum RequestResult<T> { | |
| case successo(T) | |
| case erro(Error) | |
| } | |
| enum Router { | |
| case xAuth(params: [String: Any]) | |
| case dashboard(token: String) | |
| case dimensions | |
| var basePath: String { | |
| return "http://10.0.0.167:8081/v1" | |
| } | |
| var path: String { | |
| switch self { | |
| case .xAuth: return "login" | |
| case .dashboard: return "dashboard" | |
| default: | |
| return "/" | |
| } | |
| } | |
| var parameters: [String: Any] { | |
| switch self { | |
| case .dashboard(let token): | |
| return ["token": token] | |
| default: | |
| return [:] | |
| } | |
| } | |
| var method: String { | |
| switch self { | |
| case .xAuth: | |
| return "POST" | |
| default: | |
| return "GET" | |
| } | |
| } | |
| var body: [String: Any]? { | |
| switch self { | |
| case .xAuth(let bodyParams): | |
| return bodyParams.count > 0 ? bodyParams : nil | |
| default: | |
| return nil | |
| } | |
| } | |
| var request: URLRequest? { | |
| let baseUrlPath = "\(basePath)/\(path)" | |
| var baseURL = URLComponents(string: baseUrlPath) | |
| baseURL?.queryItems = parameters.map { qParamter in | |
| return URLQueryItem(name: qParamter.key, value: "\(qParamter.value)") | |
| } | |
| guard let url = baseURL?.url else { | |
| print("Impossível iniciar request com essa url") | |
| return nil | |
| } | |
| let request = URLRequest(url: url) | |
| if let body = self.body { | |
| if let bodyData = try? JSONSerialization.data(withJSONObject: body, options: []) { | |
| request.httpBody = bodyData | |
| request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") | |
| } | |
| } | |
| return request | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment