https://kotlinlang.org logo
#android
Title
# android
a

Alvin Dizon

03/28/2024, 2:32 AM
I have a
AuthorizationInterceptor
which is a Retrofit Interceptor that just adds a
Bearer
header to the request. Currently, this header is stored to shared preferences, so it was being used in a synchronous manner. If I store the header in
DataStore
, with `DataStore`'s async API, is
runBlocking
the way to go to access the said header within the Interceptor's
intercept()
function? So something like this:
Copy code
override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        val authToken = runBlocking {
            dataStore.header.firstOrNull()
        }
        val withHeader =
            originalRequest.newBuilder().header("Authorization", "Bearer $authToken").build()
        return chain.proceed(withHeader)
    }
s

SanjayKarki

03/28/2024, 8:01 AM
Yes you can use runblocking
👍 1