I want to use regenerate token api in my android app with Retrofit. I was searching for solution how can i call regenerate token api when i get 401 response code from api. I found handy way of authenticator() method where i create instance of TokenAuthenticator class and this class has authenticate() overridden function under that i can call generate token api Which actually needs Retrofit builder Apiservice class. I am stuck in Circular dependency problem because TokenAuthenticator requires ApiServer and TokenAuthenticator() itself creates in Retrofit builder which returns ApiService. I found solutions on this problem using Dagger2 or hilt (Android Retrofit2 Refresh Oauth 2 Token). I am not using DI(Dagger2 or Hilt) in project. How can i pass ApiService to TokenAuthenticator() constructor. I also need to pass Preference(Jetpack datastore) to TokenAuthenticator() constructor because while calling regenerate api i am passing old token and other body.
How can i solve this problem can anyone help me please. thanks.
object RetrofitBuilder {
private const val BASE_URL = "..."
operator fun invoke(
networkConnectionInterceptor: NetworkConnectionInterceptor
) : ApiService {
val interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
val okkHttpclient = OkHttpClient.Builder()
.authenticator(TokenAuthenticator())
.addInterceptor(networkConnectionInterceptor)
.addInterceptor(interceptor)
.build()
return Retrofit.Builder()
.client(okkHttpclient)
.baseUrl (BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build() //Doesn't require the adapter
.create(ApiService::class.java)
}
}