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..f7192e96 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift @@ -2,43 +2,23 @@ // NetworkAPIProtocol.swift // -import Alamofire -import RxAlamofire +import Moya import RxSwift protocol NetworkAPIProtocol { - func performRequest(_ configuration: RequestConfiguration, for type: T.Type) -> Single + func performRequest(_ configuration: TargetType, for type: T.Type) -> Single } extension NetworkAPIProtocol { - func request( - session: Session, - configuration: RequestConfiguration, + provider: MoyaProvider, + configuration: TargetType, decoder: JSONDecoder ) -> 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(MultiTarget(configuration)) + .map(T.self, using: decoder) } } diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift deleted file mode 100644 index 32ddb9d3..00000000 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift +++ /dev/null @@ -1,39 +0,0 @@ -// -// RequestConfiguration.swift -// - -import Alamofire -import Foundation - -protocol RequestConfiguration { - - var baseURL: String { get } - - var endpoint: String { get } - - var method: HTTPMethod { get } - - var url: URLConvertible { get } - - var parameters: Parameters? { get } - - var encoding: ParameterEncoding { get } - - var headers: HTTPHeaders? { get } - - var interceptor: RequestInterceptor? { get } -} - -extension RequestConfiguration { - - var url: URLConvertible { - let url = URL(string: baseURL)?.appendingPathComponent(endpoint) - return url?.absoluteString ?? "\(baseURL)\(endpoint)" - } - - var parameters: Parameters? { nil } - - var headers: HTTPHeaders? { nil } - - var interceptor: RequestInterceptor? { nil } -} diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift index a713490e..834415c3 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift @@ -2,23 +2,24 @@ // NetworkAPI.swift // -import Alamofire import Foundation +import Moya import RxSwift final class NetworkAPI: NetworkAPIProtocol { + private let provider: MoyaProvider private let decoder: JSONDecoder - init(decoder: JSONDecoder = JSONDecoder()) { + init( + provider: MoyaProvider = MoyaProvider(), + decoder: JSONDecoder = JSONDecoder() + ) { + self.provider = provider self.decoder = decoder } - func performRequest(_ configuration: RequestConfiguration, for type: T.Type) -> Single { - request( - session: Session(), - configuration: configuration, - decoder: decoder - ) + func performRequest(_ configuration: TargetType, for type: T.Type) -> Single { + request(provider: provider, configuration: configuration, decoder: decoder) } } diff --git a/{PROJECT_NAME}/Sources/Supports/Helpers/Typealiases/Typealiases.swift b/{PROJECT_NAME}/Sources/Supports/Helpers/Typealiases/Typealiases.swift index b27d5758..8a2c4101 100644 --- a/{PROJECT_NAME}/Sources/Supports/Helpers/Typealiases/Typealiases.swift +++ b/{PROJECT_NAME}/Sources/Supports/Helpers/Typealiases/Typealiases.swift @@ -3,6 +3,8 @@ // Typealiases.swift // +import Moya import UIKit typealias AlertCompletion = (UIAlertAction) -> Void +typealias TargetType = Moya.TargetType