Skip to content

Instantly share code, notes, and snippets.

View lordcodes's full-sized avatar
⌨️
Coding

Andrew Lord lordcodes

⌨️
Coding
View GitHub Profile
@lordcodes
lordcodes / app_build.gradle.kts
Created November 28, 2020 01:14
Brief demo of using a platform module to constrain dependency versions
dependencies {
implementation(enforcedPlatform(project(":dependency-constraints")))
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("androidx.lifecycle:lifecycle-livedata-ktx")
implementation("io.coil-kt:coil")
}
@lordcodes
lordcodes / AuthorizationInterceptor.kt
Created March 17, 2020 09:33
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()
@lordcodes
lordcodes / CreateAccountRemoteApi.kt
Created March 17, 2020 09:32
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
interface CreateAccountRemoteApi {
@POST("user/identity")
fun createAccount(
@Body request: CreateAccountRemoteDto,
@Tag authorization: AuthorizationType = AuthorizationType.CLIENT_CREDENTIALS
): Single<AccountCreatedRemoteDto>
}
@lordcodes
lordcodes / AuthorizationType.kt
Created March 17, 2020 09:32
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
enum class AuthorizationType {
ACCESS_TOKEN,
CLIENT_CREDENTIALS,
NONE;
companion object {
fun fromRequest(request: Request): AuthorizationType =
request.tag(AuthorizationType::class.java) ?: ACCESS_TOKEN
}
}
@lordcodes
lordcodes / OkHttpClientBuilder.kt
Created March 17, 2020 09:31
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
OkHttpClient.Builder()
.addInterceptor(authorizationInterceptor)
.authenticator(tokenRefreshAuthenticator)
.build()
@lordcodes
lordcodes / TokenRefreshAuthenticator.kt
Created March 17, 2020 09:31
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
class TokenRefreshAuthenticator(
private val authorizationRepository: AuthorizationRepository
) : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? = when {
response.retryCount > 2 -> null
else -> response.createSignedRequest()
}
private fun Response.createSignedRequest(): Request? = try {
val accessToken = authenticationRepository.fetchFreshAccessToken()
@lordcodes
lordcodes / ResponseRetryCount.kt
Created March 17, 2020 09:30
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
private val Response.retryCount: Int
get() {
var currentResponse = priorResponse
var result = 0
while (currentResponse != null) {
result++
currentResponse = currentResponse.priorResponse
}
return result
}
@lordcodes
lordcodes / RequestSigning.kt
Created March 17, 2020 09:30
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
fun Request.signWithToken(accessToken: AccessToken) =
newBuilder()
.header("Authorization", "Bearer ${accessToken.rawToken}")
.build()
@lordcodes
lordcodes / OkHttpClientBuilder.kt
Created March 17, 2020 09:29
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
fun okHttpClient(authorizationInterceptor: AuthorizationInterceptor) =
OkHttpClient.Builder()
.addInterceptor(authorizationInterceptor)
.build()
@lordcodes
lordcodes / AuthorizationInterceptor.kt
Created March 17, 2020 09:28
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()