Last active
December 12, 2015 04:38
-
-
Save jstrachan/4715731 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
/** | |
* Use case: okay lets say i have 2 separate services, one for authentication and the other for loading profile data. | |
* so can you write me some code that will : | |
* handle a login http request, asynchronously authenticate the user with a | |
* user / pass (which returns a user identifier or fails), | |
* upon success will then retrieve the profile details of the user using the id asynchronously, | |
* then output the profile details to a http response. | |
*/ | |
interface APIs { | |
// for simplicity am shoving these on the same API | |
Future<AuthenticateResponse> authenticate(String user, String pwd); | |
Future<ProfileDetails> getProfileDetails(String user); | |
} | |
APIs api = ...; | |
Observable<AuthenticateResponse> authObs = Observers.toObserver(api.authenticate("foo", "bar")); | |
Observable<ProfileDetails> profileDetailsObj = authObs.mapMany( authResp -> | |
Observers.toObserver(api.getProfileDetails(authResp.getUser())) ); | |
// helper function to subscribe to the observable and | |
// when it completes, write the state to the http request | |
httpResponse.marshal(profileDetailsObj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment