diff --git a/Podfile b/Podfile index 0c06ba70..df072526 100644 --- a/Podfile +++ b/Podfile @@ -19,7 +19,7 @@ target '{PROJECT_NAME}' do pod 'SnapKit' # Rx - pod 'RxAlamofire' + pod 'Moya/RxSwift' pod 'RxCocoa' pod 'RxDataSources' pod 'RxSwift' diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift index c8be3e5b..4e0ce04d 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift @@ -2,8 +2,7 @@ // NetworkAPIProtocol.swift // -import Alamofire -import RxAlamofire +import Moya import RxSwift protocol NetworkAPIProtocol { @@ -12,33 +11,12 @@ protocol NetworkAPIProtocol { } extension NetworkAPIProtocol { - func request( - session: Session, - configuration: RequestConfiguration, - decoder: JSONDecoder + provider: MoyaProvider, + configuration: RequestConfiguration ) -> Single { - return session.rx.request( - configuration.method, - configuration.url, - parameters: configuration.parameters, - encoding: configuration.encoding, - headers: configuration.headers, - interceptor: configuration.interceptor - ) - .responseData() - .flatMap { _, data -> Observable in - Observable.create { observer in - do { - let decodable = try decoder.decode(T.self, from: data) - observer.on(.next(decodable)) - } catch { - observer.on(.error(error)) - } - observer.on(.completed) - return Disposables.create() - } - } - .asSingle() + provider.rx.request(configuration) + .filterSuccessfulStatusAndRedirectCodes() + .map(T.self) } } diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift index 32ddb9d3..f289e996 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift @@ -2,38 +2,34 @@ // RequestConfiguration.swift // -import Alamofire import Foundation +import Moya -protocol RequestConfiguration { +enum RequestConfiguration { - var baseURL: String { get } - - var endpoint: String { get } - - var method: HTTPMethod { get } - - var url: URLConvertible { get } - - var parameters: Parameters? { get } + /// Add cases for each endpoint +} - var encoding: ParameterEncoding { get } +extension RequestConfiguration: TargetType { - var headers: HTTPHeaders? { get } + /// Return base URL for a target + var baseURL: URL { URL(string: "https://base_url")! } - var interceptor: RequestInterceptor? { get } -} + /// Return endpoint path for each endpoint case + var path: String { "" } -extension RequestConfiguration { + /// Return HTTP method for each endpoint case + var method: Moya.Method { .get } - var url: URLConvertible { - let url = URL(string: baseURL)?.appendingPathComponent(endpoint) - return url?.absoluteString ?? "\(baseURL)\(endpoint)" - } + /// Build and Return HTTP task for each endpoint case + var task: Moya.Task { .requestPlain } - var parameters: Parameters? { nil } + /// Return the appropriate HTTP headers for every endpoint case + var headers: [String: String]? { ["Content-Type": "application/json"] } - var headers: HTTPHeaders? { nil } + /// Return stub/mock data for use in testing. Default is `Data()` + var sampleData: Data { Data() } - var interceptor: RequestInterceptor? { nil } + /// Return the type of validation to perform on the request. Default is `.none`. + var validationType: ValidationType { .successCodes } } diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift index a713490e..0adb5a36 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift @@ -2,23 +2,19 @@ // NetworkAPI.swift // -import Alamofire import Foundation +import Moya import RxSwift final class NetworkAPI: NetworkAPIProtocol { - private let decoder: JSONDecoder + private let provider: MoyaProvider - init(decoder: JSONDecoder = JSONDecoder()) { - self.decoder = decoder + init(provider: MoyaProvider = MoyaProvider()) { + self.provider = provider } func performRequest(_ configuration: RequestConfiguration, for type: T.Type) -> Single { - request( - session: Session(), - configuration: configuration, - decoder: decoder - ) + request(provider: provider, configuration: configuration) } }