Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Created March 17, 2020 09:33
Show Gist options
  • Save lordcodes/60b9210e9c4acf9639e9531325fc85e2 to your computer and use it in GitHub Desktop.
Save lordcodes/60b9210e9c4acf9639e9531325fc85e2 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() = when (AuthorizationType.fromRequest(this)) {
AuthorizationType.ACCESS_TOKEN -> signWithFreshAccessToken()
AuthorizationType.CLIENT_CREDENTIALS -> signWithClientCredentialsToken()
AuthorizationType.NONE -> this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment