Skip to content

Commit

Permalink
[324] [Chore] Use Moya as network layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Shayokh144 committed Nov 28, 2022
1 parent 5080933 commit 653aa94
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ target '{PROJECT_NAME}' do
pod 'SnapKit'

# Rx
pod 'RxAlamofire'
pod 'Moya/RxSwift'
pod 'RxCocoa'
pod 'RxDataSources'
pod 'RxSwift'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// NetworkAPIProtocol.swift
//

import Alamofire
import RxAlamofire
import Moya
import RxSwift

protocol NetworkAPIProtocol {
Expand All @@ -12,33 +11,12 @@ protocol NetworkAPIProtocol {
}

extension NetworkAPIProtocol {

func request<T: Decodable>(
session: Session,
configuration: RequestConfiguration,
decoder: JSONDecoder
provider: MoyaProvider<RequestConfiguration>,
configuration: RequestConfiguration
) -> Single<T> {
return session.rx.request(
configuration.method,
configuration.url,
parameters: configuration.parameters,
encoding: configuration.encoding,
headers: configuration.headers,
interceptor: configuration.interceptor
)
.responseData()
.flatMap { _, data -> Observable<T> 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
14 changes: 5 additions & 9 deletions {PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<RequestConfiguration>

init(decoder: JSONDecoder = JSONDecoder()) {
self.decoder = decoder
init(provider: MoyaProvider<RequestConfiguration> = MoyaProvider<RequestConfiguration>()) {
self.provider = provider
}

func performRequest<T: Decodable>(_ configuration: RequestConfiguration, for type: T.Type) -> Single<T> {
request(
session: Session(),
configuration: configuration,
decoder: decoder
)
request(provider: provider, configuration: configuration)
}
}

0 comments on commit 653aa94

Please sign in to comment.