Refresh Access Token globally (Separate logic as a module) using RxJava 2, RxAndroid 2 and Retrofit 2

Rajan Maurya
AndroidPub
Published in
3 min readAug 20, 2017

--

It is very common logic everyone needs, Like I was dealing with access token in my current GSoC Project. In my situation I knew, when my access token in going to expire, I have the timestamp of expiration. So there is two way to handle refresh the access token.

First way

1. Check the System time and Expiration time and refresh the access token.

Second way

2. Make a global logic(Separate logic as a module) that will resume the failed observable with exception 403 and refresh the access token as we will get new refreshed access token then execute failed source observable again.

My choice is Second way because I am using MVP architecture and I don’t want to check same logic in my all request again and again.

So let’s start how to Refresh access token globally with Retrofit 2, RxJava 2 and RxAndroid 2.

It is very common we make REST API calls using Retrofit 2 and RxJava 2 like this in Presenter.

Example: https://github.com/therajanmaurya/android-client-2.0/blob/development/app/src/main/java/com/mifos/apache/fineract/ui/online/customers/customerlist/CustomersPresenter.java

As we want to check only if any request will fail with Exception 403 So onErrorResumeNext() is the best method.

onErrorResumeNext() will call if any REST API will fail because of any exception.

Note: In My case 403 HttpException is the Unauthorized token.

I don’t want to check in my all presenters that Is any REST API failed because of 403 exception. don’t you think It is bad too, to implement onErrorResumeNex() in more than 100 Presenters So I wrote whole thing globally. As We know I am using the MVP architecture. I have DataManagers (Not Single DataManager because My project is too big that’s why I split my DataManager with multiple DataManager).

Here is my BaseDataManager

For Observables, Source: https://github.com/therajanmaurya/android-client-2.0/blob/development/app/src/main/java/com/mifos/apache/fineract/data/datamanager/MifosBaseDataManager.java
For Completable, Source: https://github.com/therajanmaurya/android-client-2.0/blob/development/app/src/main/java/com/mifos/apache/fineract/data/datamanager/MifosBaseDataManager.java

HttpException code

HttpException code, Source: https://github.com/therajanmaurya/android-client-2.0/blob/development/app/src/main/java/com/mifos/apache/fineract/exceptions/ExceptionStatusCode.java

Above you can see I wrote two methods one is Observable return type and second is Completable return type. I made because I have some REST APIs that are returning Void in response and some observable.

Now It’s time to bind BaseDataManager with main DataManagers

Source: https://github.com/therajanmaurya/android-client-2.0/blob/development/app/src/main/java/com/mifos/apache/fineract/data/datamanager/DataManagerCustomer.java

Here you can see you don’t need to write anything in Presenter and DataManagers and Fragment or Activity. BaseDataManager is handling everything, refreshing access token if needed and executing source observable again.

Basically refreshTokenAndRetryObser and refreshTokenAndRetryCompletable are the main logic put them in separately base class and bind them with your observable before implementing the Observer<T> in your presenter. I prefer in DataManager.

public interface Observer<T> {    void onSubscribe(Disposable d);

void onNext(T value);
void onError(Throwable e);

void onComplete();

}

I know sometime, some logic take time to implement because we want to implement globally so we don’t need to make changes everywhere, Just wanna write at once and forget about it.

Here is my PULL Request https://github.com/therajanmaurya/android-client-2.0/pull/131 and https://github.com/therajanmaurya/android-client-2.0/pull/132 that consist all work to implement Refresh access token globally.

Best of Luck Hope It will help !!!!

--

--

Rajan Maurya
AndroidPub

Senior Software Engineer at OpenLane Inc, Open source contributor at Mifos Initiative and mentoring GSoC Students. https://github.com/therajanmaurya