我有一个包含以下界面的改造服务
public interface ApiService {
@GET("/users/me")
Observable<Account> authenticateUser(@Header("Authorization") String auth);
@GET("/membership/{userId}")
SubscriptionStatus getSubscriptionStatus(@Path("userId") String userId);
}
我想定义一个方法来进行api调用以获取包含userId的Account,然后使用此ID进行第二次API调用以获取用户订阅状态. SubscriptionStatus包含一个布尔值,如果它是真的,我希望该方法返回Observable.
这就是我到目前为止的方式:
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
}
解决方法:
怎么样
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
);
如果您需要帐户和状态,最后一个map()应该返回两个中的composite type:
.map(status -> new Pair<>(account, status))