From 653aa949bda5706027c8f0209a6946cbf3d26a95 Mon Sep 17 00:00:00 2001 From: Shayokh144 Date: Mon, 28 Nov 2022 14:08:27 +0700 Subject: [PATCH 1/4] [324] [Chore] Use Moya as network layer --- Podfile | 2 +- .../NetworkAPI/Core/NetworkAPIProtocol.swift | 34 +++------------ .../Core/RequestConfiguration.swift | 42 +++++++++---------- .../Sources/Data/NetworkAPI/NetworkAPI.swift | 14 +++---- 4 files changed, 31 insertions(+), 61 deletions(-) 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) } } From 7cfad009590075d05069519e13eb6f3fe07ca014 Mon Sep 17 00:00:00 2001 From: Shayokh144 Date: Tue, 29 Nov 2022 14:58:42 +0700 Subject: [PATCH 2/4] [324] [Chore] Remove comments and default items --- .../NetworkAPI/Core/NetworkAPIProtocol.swift | 1 - .../NetworkAPI/Core/RequestConfiguration.swift | 18 ++---------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift index 4e0ce04d..cae242e4 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift @@ -16,7 +16,6 @@ extension NetworkAPIProtocol { configuration: RequestConfiguration ) -> Single { 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 f289e996..49a01809 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift @@ -5,31 +5,17 @@ import Foundation import Moya -enum RequestConfiguration { - - /// Add cases for each endpoint -} +enum RequestConfiguration {} extension RequestConfiguration: TargetType { - /// Return base URL for a target var baseURL: URL { URL(string: "https://base_url")! } - /// Return endpoint path for each endpoint case var path: String { "" } - /// Return HTTP method for each endpoint case var method: Moya.Method { .get } - /// Build and Return HTTP task for each endpoint case var task: Moya.Task { .requestPlain } - /// Return the appropriate HTTP headers for every endpoint case - var headers: [String: String]? { ["Content-Type": "application/json"] } - - /// Return stub/mock data for use in testing. Default is `Data()` - var sampleData: Data { Data() } - - /// Return the type of validation to perform on the request. Default is `.none`. - var validationType: ValidationType { .successCodes } + var headers: [String: String]? { nil } } From d37a1e0af77dc98bfedf7be31ded6a3ed901aee4 Mon Sep 17 00:00:00 2001 From: Shayokh144 Date: Thu, 1 Dec 2022 17:30:09 +0700 Subject: [PATCH 3/4] [324] [Chore] Remove RequestConfiguration and use Maya MultiTarget --- .../NetworkAPI/Core/NetworkAPIProtocol.swift | 8 +++---- .../Core/RequestConfiguration.swift | 21 ------------------- .../Sources/Data/NetworkAPI/NetworkAPI.swift | 6 +++--- 3 files changed, 7 insertions(+), 28 deletions(-) delete mode 100644 {PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift index cae242e4..c57e118a 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift @@ -7,15 +7,15 @@ 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( - provider: MoyaProvider, - configuration: RequestConfiguration + provider: MoyaProvider, + configuration: TargetType ) -> Single { - provider.rx.request(configuration) + provider.rx.request(MultiTarget(configuration)) .map(T.self) } } 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 49a01809..00000000 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/RequestConfiguration.swift +++ /dev/null @@ -1,21 +0,0 @@ -// -// RequestConfiguration.swift -// - -import Foundation -import Moya - -enum RequestConfiguration {} - -extension RequestConfiguration: TargetType { - - var baseURL: URL { URL(string: "https://base_url")! } - - var path: String { "" } - - var method: Moya.Method { .get } - - var task: Moya.Task { .requestPlain } - - var headers: [String: String]? { nil } -} diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift index 0adb5a36..26471429 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift @@ -8,13 +8,13 @@ import RxSwift final class NetworkAPI: NetworkAPIProtocol { - private let provider: MoyaProvider + private let provider: MoyaProvider - init(provider: MoyaProvider = MoyaProvider()) { + init(provider: MoyaProvider = MoyaProvider()) { self.provider = provider } - func performRequest(_ configuration: RequestConfiguration, for type: T.Type) -> Single { + func performRequest(_ configuration: TargetType, for type: T.Type) -> Single { request(provider: provider, configuration: configuration) } } From f7d5b3c7c3bb747646829b3f884e5338170260d7 Mon Sep 17 00:00:00 2001 From: Shayokh144 Date: Fri, 2 Dec 2022 13:52:21 +0700 Subject: [PATCH 4/4] [324] [Chore] Add decoder and typealias --- .../Data/NetworkAPI/Core/NetworkAPIProtocol.swift | 9 ++++++--- {PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift | 9 +++++++-- .../Supports/Helpers/Typealiases/Typealiases.swift | 2 ++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift index c57e118a..f7192e96 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/Core/NetworkAPIProtocol.swift @@ -13,9 +13,12 @@ protocol NetworkAPIProtocol { extension NetworkAPIProtocol { func request( provider: MoyaProvider, - configuration: TargetType + configuration: TargetType, + decoder: JSONDecoder ) -> Single { - provider.rx.request(MultiTarget(configuration)) - .map(T.self) + provider + .rx + .request(MultiTarget(configuration)) + .map(T.self, using: decoder) } } diff --git a/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift index 26471429..834415c3 100644 --- a/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift +++ b/{PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift @@ -9,12 +9,17 @@ import RxSwift final class NetworkAPI: NetworkAPIProtocol { private let provider: MoyaProvider + private let decoder: JSONDecoder - init(provider: MoyaProvider = MoyaProvider()) { + init( + provider: MoyaProvider = MoyaProvider(), + decoder: JSONDecoder = JSONDecoder() + ) { self.provider = provider + self.decoder = decoder } func performRequest(_ configuration: TargetType, for type: T.Type) -> Single { - request(provider: provider, configuration: configuration) + 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