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 all 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,43 +2,23 @@
// NetworkAPIProtocol.swift
//

import Alamofire
import RxAlamofire
import Moya
import RxSwift

protocol NetworkAPIProtocol {

func performRequest<T: Decodable>(_ configuration: RequestConfiguration, for type: T.Type) -> Single<T>
func performRequest<T: Decodable>(_ configuration: TargetType, for type: T.Type) -> Single<T>
}

extension NetworkAPIProtocol {

func request<T: Decodable>(
session: Session,
configuration: RequestConfiguration,
provider: MoyaProvider<MultiTarget>,
blyscuit marked this conversation as resolved.
Show resolved Hide resolved
configuration: TargetType,
decoder: JSONDecoder
) -> 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(MultiTarget(configuration))
.map(T.self, using: decoder)
}
}

This file was deleted.

17 changes: 9 additions & 8 deletions {PROJECT_NAME}/Sources/Data/NetworkAPI/NetworkAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
// NetworkAPI.swift
//

import Alamofire
import Foundation
import Moya
import RxSwift

final class NetworkAPI: NetworkAPIProtocol {

private let provider: MoyaProvider<MultiTarget>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please work with @suho to clarify this comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shayokh144 Regarding this discussion, it's better to remove NetworkAPI and NetworkAPIProtocol in this PR

private let decoder: JSONDecoder

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

func performRequest<T: Decodable>(_ configuration: RequestConfiguration, for type: T.Type) -> Single<T> {
request(
session: Session(),
configuration: configuration,
decoder: decoder
)
func performRequest<T: Decodable>(_ configuration: TargetType, for type: T.Type) -> Single<T> {
request(provider: provider, configuration: configuration, decoder: decoder)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Typealiases.swift
//

import Moya
import UIKit

typealias AlertCompletion = (UIAlertAction) -> Void
typealias TargetType = Moya.TargetType