I am using an auto-generated AWS Android SDK to access my REST API. Example
Repos = restClient.getRepos();
This call needs to be in an Async Task but I am converting it in an Observable by
final Observable reposRx = Observable.fromCallable(() -> restClient.getRepos());
The http client that uses the REST API uses a token for authentication, if I execute a method from the REST API and the token is expired it will throw an error so before i execute any REST call I need to check if the token has expired(This is done just checking in a sync method that checks a variable), if it's expired then I need to renew it by executing another async network call.
if(isTokenExpired()) {
refreshToken((new RefreshIdTokenCallback() {
@Override
public void onSuccess(final String idToken)
{
getRepos()
}
@Override
public void onFailure(final Throwable error)
{
//How to connect it with Rx
}
});
}
else
{
getRepos()
}
So after the token is renewed I can now execute the original rest api call.
public void getRepos()
{
reposRx
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Repos>() {
@Override
public void onCompleted() {
}
@Override
public void onError(final Throwable e) {
}
@Override
public void onNext(final Repos repos) {
//update the list
}
});
}
Is there any magic way to convert everything to RxJava and also have a method that that I can reuse with all the REST API methods that checks the token expiration, if expired refreshes and calls the REST API. And also connects the onFailed ? (All with Rx)