Created
July 26, 2017 09:28
-
-
Save vzsg/b1a7405c987bdf18cc9035591ea2f58c to your computer and use it in GitHub Desktop.
Alamofire+RX multipart encoding
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
import Foundation | |
import Alamofire | |
import RxSwift | |
extension Reactive where Base: SessionManager { | |
func encodeMultipartUpload(to url: URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders = [:], data: @escaping (MultipartFormData) -> Void) -> Observable<UploadRequest> { | |
return Observable.create { observer in | |
self.base.upload(multipartFormData: data, | |
to: url, | |
method: method, | |
headers: headers, | |
encodingCompletion: { (result: SessionManager.MultipartFormDataEncodingResult) in | |
switch result { | |
case .failure(let error): | |
observer.onError(error) | |
case .success(let request, _, _): | |
observer.onNext(request) | |
observer.onCompleted() | |
} | |
}) | |
return Disposables.create() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment