Created
November 3, 2017 17:32
-
-
Save douglastaquary/d882775b8129de0ecd2e982fec3ef5ba 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
| // | |
| // MinhaAPI.swift | |
| // | |
| // | |
| // Created by Douglas Taquary on 04/10/2017. | |
| // Copyright © 2017 Douglas Taquary. All rights reserved. | |
| // | |
| import Foundation | |
| import RxSwift | |
| import Moya | |
| import Alamofire | |
| enum MinhaAPI { | |
| case A(id: Int) | |
| case B(idCategoria: Int, id: Int) | |
| } | |
| extension MinhaAPI: TargetType { | |
| var task: Task { | |
| return .request | |
| } | |
| var base: String { return "http://api.v1.inf.br/" } | |
| var baseURL: URL { return URL(string: base)! } | |
| var path: String { | |
| switch self { | |
| case .A: | |
| return "/endpoint/ZX" | |
| case .B: | |
| return "/endpoint/BX" | |
| } | |
| } | |
| var method: Moya.Method { | |
| switch self { | |
| default: | |
| return .post | |
| } | |
| } | |
| var parameters: [String: Any]? { | |
| switch self { | |
| case .A(let id): | |
| return ["algumacoisa": ["id": id as AnyObject]] | |
| case .B(let idCategoria, let id): | |
| return ["restaurante": ["id": idCategoria as AnyObject], | |
| "categoria": ["id": id as AnyObject]] | |
| } | |
| } | |
| //Se vc precisar passar parametros no JSON, é colocar qual endpoint vai fazer isso, caso contrario ele vai | |
| //usar a url para mandar os parametros | |
| var parameterEncoding: Moya.ParameterEncoding { | |
| switch self { | |
| case .A, .B: | |
| return JSONEncoding.default | |
| } | |
| } | |
| var headers: [String: String]? { | |
| return nil | |
| } | |
| var sampleData: Data { | |
| switch self { | |
| default: | |
| return Data() | |
| } | |
| } | |
| } | |
| // MARK: Provider Support | |
| private extension String { | |
| var URLEscapedString: String { | |
| return self.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)! | |
| } | |
| } | |
| func url(_ route: TargetType) -> String { | |
| return route.baseURL.appendingPathComponent(route.path).absoluteString | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment