Custom tags for handling tokens with Retrofit on Android

Furkan Aşkın
Ozan SuperApp
Published in
3 min readDec 13, 2021
Photo by JJ Ying on Unsplash

Introduction

In this blogpost i will try to explain handling different tokens using custom tag and interceptor.

Problem

First of all, suppose that we have two different tokens. Like;

  • AwesomeToken
  • PerfectToken

Unless otherwise stated, all api calls must be performed with the PerfectToken. But in some cases we need to use AwesomeToken which i mentioned on top. And suppose that you have only one network interceptor for all api calls. It sets platform, language, token etc..

Actually, the problem is interceptor doesn’t know which tokens are associated with which endpoints.

Quick Solution

Our first interaction with this problem is a creating some extension functions. You know, we can get request chain from interceptor’s intercept method and we defined some extension like Request.isAwesomeTokenRequest() and the function has an really simple logic.

In early stages we thought that solution will be enough for us. Actually yes, it’s working but after a couple months this extension became something like that :

Trust me it was really looking something like that. And as you can see we had to add ComplexMethod suppress for avoiding Detekt rule 😀

In early stages as a quick solution; the solution was easy to handle, easy to understand but after a moment it became really hard to understand and you’re were finding yourself while reading the endpoint names.

So, we decided to refactor this extensions with a different solution.

Solution

The solution is using annotations for requests. With this way, we can easily mark requests for setting which tokens will be used by this requests.

Let’s start with creating annotation :

You can create a annotation class like that also if you need you can set a constructor that take some parameters.

Then just mark your request definitions with this annotation. After that create some extensions which is shown below:

Just a quick explanation for curious people : Invocation is a Retrofit class, which is holding our function name, request type, request parameters etc. By default Retrofit automatically adds an invocation to each OkHttp request as a tag.

In the end we can easily set our tokens as we wish. Else case means the request isn’t marked with any custom annotation and these requests will use PerfectToken. Others will use AwesomeToken.

Testing

For writing unit test we gonna use MockWebServer and MockK.

First of all create a mockWebServer and OkHttp client, then add your interceptor to client and we are ready to go.

We enqueue a mockResponse but we aren’t gonna use it, it’s just simulating the network call.

Now we need to find which endpoints are called with custom annotations, it’s kinda challenging but we can use reflection for finding functions and the annotations.

This function which is shown above will return first method in our service class which is marked with AwesomeToken.

Let’s say if the request marked with AwesomeToken we should add AUTHORIZATION header to our request. The test case will be like that:

Basically, as i mention before each request has an Invocation by default and we are imitating the same thing. We just crated a Invocation, the method of this Invocation is a service call method which is have an AwesomeToken annotation.

And if everything is set up properly, the test will be successful.

Conclusion

With creating custom annotations you can handle your different tokens in a single interceptor also if you need to apply custom operations for some endpoints with this way you can easily make it too. Passing some arguments to your annotation class will be useful.

Thanks for reading! If you enjoyed this story, please click the 👏 button and share it to help others!

If you have any kind of feedback, feel free to connect with me on Twitter.

--

--

Furkan Aşkın
Ozan SuperApp

Android Developer @OzanSuperApp | Speaker @JuniorTalks