Created
September 14, 2017 21:06
-
-
Save fellipecaetano/d789fed65e39ced84a9548586b0f3da8 to your computer and use it in GitHub Desktop.
Epics
This file contains hidden or 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 RxSwift | |
import Alamofire | |
import RxAlamofire | |
func LoadPoliticiansEpic(actions: Observable<Action>) -> Observable<Action> { | |
let sessionConfiguration = AppEnvironment.sessionConfiguration | |
let sessionManager = SessionManager(configuration: sessionConfiguration) | |
return actions | |
.filter({ action in | |
if case PoliticiansAction.startLoading = action { | |
return true | |
} else { | |
return false | |
} | |
}) | |
.map(PoliticiansRequestFactory.request) | |
.flatMapLatest({ request in | |
return sessionManager.request(request).rx.value() | |
}) | |
.map(PoliticiansAction.load) | |
.catchError({ error in | |
return Observable.just(PoliticiansAction.fail(error)) | |
}) | |
} | |
final class PoliticiansRequestFactory { | |
static func request(action: Action) -> Request<[Politicians]> { | |
return Request( | |
url: URL(string: "/politicians")!, | |
method: .get, | |
parameters: [:], | |
headers: [:], | |
parameterEncoding: URLEncoding() | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment