Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#324] Use Moya as the main Network Layer #394

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 @@ protocol NetworkAPIProtocol {
}

extension NetworkAPIProtocol {

func request<T: Decodable>(
session: Session,
configuration: RequestConfiguration,
decoder: JSONDecoder
provider: MoyaProvider<RequestConfiguration>,
suho marked this conversation as resolved.
Show resolved Hide resolved
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)
.map(T.self)
blyscuit marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,20 @@
// RequestConfiguration.swift
//

import Alamofire
import Foundation
import Moya

protocol RequestConfiguration {
enum RequestConfiguration {}

var baseURL: String { get }
extension RequestConfiguration: TargetType {

var endpoint: String { get }
var baseURL: URL { URL(string: "https://base_url")! }

var method: HTTPMethod { get }
var path: String { "" }

var url: URLConvertible { get }
var method: Moya.Method { .get }

var parameters: Parameters? { get }
var task: Moya.Task { .requestPlain }

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 }
var headers: [String: String]? { nil }
}
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)
}
}