Skip to content

Instantly share code, notes, and snippets.

@aibobrov
Created September 14, 2018 08:37
Show Gist options
  • Save aibobrov/009e4bcc4dd3fa6f5deaf51bc49722c5 to your computer and use it in GitHub Desktop.
Save aibobrov/009e4bcc4dd3fa6f5deaf51bc49722c5 to your computer and use it in GitHub Desktop.
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