Skip to content

Instantly share code, notes, and snippets.

@chuganzy
Last active April 24, 2017 10:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chuganzy/638e16b12ba080588cc2 to your computer and use it in GitHub Desktop.
Save chuganzy/638e16b12ba080588cc2 to your computer and use it in GitHub Desktop.
RxSwift x APIKit
import Foundation
import APIKit
import RxSwift
extension Session {
func rx_sendRequest<T: RequestType>(request: T) -> Observable<T.Response> {
return Observable.create { observer in
let task = self.sendRequest(request) { result in
switch result {
case .Success(let res):
observer.on(.Next(res))
observer.on(.Completed)
case .Failure(let err):
observer.onError(err)
}
}
return AnonymousDisposable { [weak task] in
task?.cancel()
}
}
}
class func rx_sendRequest<T: RequestType>(request: T) -> Observable<T.Response> {
return sharedSession.rx_sendRequest(request)
}
}
@sohayb
Copy link

sohayb commented Feb 18, 2017

Thank you! this was quite helpful. For those looking for an updated version (swift 3.0 and RxSwift 3.0) check this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment