Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Created March 17, 2020 09:28
Show Gist options
  • Save lordcodes/71bdbaa5cde0bcf5b640514079796580 to your computer and use it in GitHub Desktop.
Save lordcodes/71bdbaa5cde0bcf5b640514079796580 to your computer and use it in GitHub Desktop.
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
class AuthorizationInterceptor(
private val authorizationRepository: AuthorizationRepository
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val newRequest = chain.request().signedRequest()
return chain.proceed(newRequest)
}
private fun Request.signedRequest(): Request {
val accessToken = authorizationRepository.fetchFreshAccessToken()
return newBuilder()
.header("Authorization", "Bearer ${accessToken.rawToken}")
.build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment