3

I have a retrofit service that contains the following interface

public interface ApiService {

    @GET("/users/me")
    Observable<Account> authenticateUser(@Header("Authorization") String auth);

    @GET("/membership/{userId}")
    SubscriptionStatus getSubscriptionStatus(@Path("userId") String userId);
}

I would like to define a method to make an api call to get the Account which contains the userId, and then use this ID to make a second API call to get The users subscription status. SubscriptionStatus contains a boolean and if its true I would like the method to return Observable.

This is how I have gone about it so far :

public Observable<Account> doLogin(ApiService service , String credentials) {

return service.authenticateuser(base64) // gets Account observable 
       .doOnNext(account -> {
            currentAccount = account; // setting the Account Variable
        })
        .flatMap(account -> service.getSubscriptionStatus(account.getUserId())) // get Account Subscription status

        ... //unsure where to go from here I need to check 
           //the subscriptionStatus object and return account           
           //observable if condition is valid
}

1 Answers1

6

How about

return service.authenticateuser(base64) // gets Account observable 
   .doOnNext(account -> {
        currentAccount = account; // setting the Account Variable
    })
    .flatMap(account -> 
         service.getSubscriptionStatus(account.getUserId())
         .filter(status -> status.isActive()) // don't let status pass if not active
         .map(status -> account)  // we actually need the original account
    );

In case you need both the account and the status, the last map() should return some composite type of the two:

 .map(status -> new Pair<>(account, status))
Community
  • 1
  • 1
akarnokd
  • 69,132
  • 14
  • 157
  • 192