Created
November 3, 2017 17:26
-
-
Save douglastaquary/ee4104a0ce33dcdba8d0ab100d5265a1 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
| // | |
| // Networking.swift | |
| // | |
| // Created by Douglas Taquary on 04/10/2017. | |
| // Copyright © 2017 Douglas Taquary. All rights reserved. | |
| // | |
| import Foundation | |
| import Moya | |
| import RxSwift | |
| import Alamofire | |
| class OnlineProvider<Target>: RxMoyaProvider<Target> where Target: TargetType { | |
| init(endpointClosure: @escaping EndpointClosure = MoyaProvider.defaultEndpointMapping, | |
| plugins: [PluginType] = []) { | |
| super.init(endpointClosure: endpointClosure, plugins: plugins) | |
| } | |
| } | |
| protocol NetworkingType { | |
| associatedtype T: TargetType | |
| var provider: OnlineProvider<MinhaAPI> { get } | |
| } | |
| struct Networking: NetworkingType { | |
| typealias T = MinhaAPI | |
| let provider: OnlineProvider<MinhaAPI> | |
| } | |
| extension Networking { | |
| func request(_ token: MinhaAPI) -> Observable<Moya.Response> { | |
| return self.provider.request(token) | |
| } | |
| } | |
| extension NetworkingType { | |
| static func newDefaultNetworking() -> Networking { | |
| return Networking(provider: newProvider([NetworkLoggerPlugin(verbose: false)])) | |
| } | |
| static func endpointsClosure<T>() -> (T) -> Endpoint<T> where T: TargetType { | |
| return { target in | |
| var endpoint: Endpoint<T> = Endpoint<T>(url: url(target), sampleResponseClosure: {.networkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters, parameterEncoding: target.parameterEncoding) | |
| // If we were given an Authorization, add it | |
| //if let Authorization = Authorization { | |
| endpoint = endpoint.adding(newHTTPHeaderFields: ["Content-Type": "application/json", | |
| "Accept": "application/json"]) | |
| //} | |
| return endpoint | |
| } | |
| } | |
| } | |
| private func newProvider<T>(_ plugins: [PluginType]) -> OnlineProvider<T> where T: TargetType { | |
| return OnlineProvider(endpointClosure: Networking.endpointsClosure(), plugins: plugins) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment