HttpService Generic protocol:
import Foundation
import Moya
protocol HttpService {
associatedtype T
associatedtype D
typealias ResultClosure<D> = (Result<D, Moya.MoyaError>) -> Void
typealias ActivityClosure = ((_: NetworkActivityChangeType) -> Void)?
static func request(
_ target: T,
completion: @escaping ResultClosure<D>,
progress: ProgressBlock?,
activity: ActivityClosure
) -> Cancellable
}
MoyaHttpService inheriting HttpService protocol:
final class MoyaHttpService: HttpService {
typealias D = Decodable
typealias T = TargetType
func request<D: Decodable, T: TargetType>(
_ target: T,
completion: @escaping ResultClosure<D>,
progress: ProgressBlock?,
activity: ActivityClosure
) -> Cancellable {
let provider = MoyaProvider<T>(endpointClosure: { (target: T) -> Endpoint in
let endpoint = MoyaProvider.defaultEndpointMapping(for: target)
//Configuration here
return endpoint
})
return provider.request(target, progress: progress) { (result) in
switch result {
case .success(let response):
guard [200, 201].contains(response.statusCode) else {
completion(.failure(MoyaError.statusCode(response)))
return
}
guard let obj: D = try? JSONDecoder().decode(D.self, from: response.data) else {
completion(.failure(MoyaError.jsonMapping(response)))
return
}
completion(.success(obj))
case .failure(let error):
completion(.failure(error))
}
}
}
}
VIPER > Interactor class:
let service=MoyaHttpService()
service.request(AuthenticationRouter.getLoginData(request: req), completion: {_ in }, progress: nil, activity: nil)
How to call request function here? I am calling service.request() wrong.
or:
service.request<GetLoginDataResponse,AuthenticationRouter>(AuthenticationRouter.getLoginData(request: req), completion: {_ in }, progress: nil, activity: nil)
Gives me error: Cannot explicitly specialize a generic function
Generic parameter 'D' could not be inferred