Thiago
09/30/2020, 12:35 PMinternal class CustomInterceptor(
private val processor: Something
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request())
// FIXME: is this fine? Can this throw Socket timeout?
return runBlocking(Dispatchers.Default) {
processor.suspendUtilHaveAnotherResponseOrThrow(response, chain)
}
}
}
Zach Klippenstein (he/him) [MOD]
09/30/2020, 4:13 PMsuspendUtilHaveAnotherResponseOrThrow
already exist? If you have to call a suspend function from an intercept
method and need its return value, then you need runBlocking
, yes. However, there’s probably no need to specify a custom dispatcher – if the suspend function you’re calling needs to be on a particular dispatcher to do some work, that should be a private concern of that function.
Zooming out, i’m not sure what exactly the implications of having an interceptor block for a long time is on general okhttp performance, but it seems like interceptors might be the wrong level of abstraction if you need to chain multiple requests a lot. Might be worth building your own abstraction on top of okhttp (instead of inside it) that can use coroutines properly.
(FYI there’s #C1CFAFJSK and #C5HT9AL7Q channels which might be more helpful in the future.)