Created
September 14, 2018 08:37
-
-
Save aibobrov/009e4bcc4dd3fa6f5deaf51bc49722c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension MoyaProvider { | |
public func request(_ target: Target, | |
callbackQueue: DispatchQueue? = nil, | |
progress: ProgressBlock? = nil) -> Promise<Moya.Response> { | |
return Promise { [weak self] fulfill, reject in | |
self?.request( | |
target, | |
callbackQueue: callbackQueue, | |
progress: progress, | |
completion: self?.completion(for: fulfill, reject: reject) ?? {_ in } | |
) | |
} | |
} | |
private func completion(for fulfill: @escaping (Moya.Response) -> Void, | |
reject: @escaping (Swift.Error) -> Void) -> Moya.Completion { | |
return { result in | |
switch result { | |
case let .success(response): | |
fulfill(response) | |
case let .failure(error): | |
reject(error) | |
} | |
} | |
} | |
} | |
public extension Promise where Value: Moya.Response { | |
public func map<T: Decodable>(to: T.Type) -> Promise<T> { | |
return self.then({ response in | |
return try JSONDecoder().decode(T.self, from: response.data) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment