Hi everyone, I'm currently stuck in a "simple" use case using Hilt:
I want a Retrofit service singleton, which takes an OkHttpClient.
The OkHttpClient has an interceptor, which uses the Retrofit service singleton (to make a certain request inside the interceptor)
The problem is:
-> The OkHttpClient takes a Retrofit as argument
-> The Retrofit takes an OkHttpClient as argument
Cheers
l
Lukas Sztefek
02/05/2021, 12:04 PM
You can inject RetrofitService into Interceptor via Provider wrapper:
Copy code
@Singleton
class MyService @Inject constructor(
private val myOkHttpClient: MyOkHttpClient,
)
@Singleton
class MyOkHttpClient @Inject constructor(
private val myInterceptor: MyInterceptor,
)
class MyInterceptor @Inject constructor(
private val myService: Provider<MyService>,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
myService.get()
return chain.proceed(chain.request())
}
}