Skip to content

Instantly share code, notes, and snippets.

Avatar
⌨️
Coding

Andrew Lord lordcodes

⌨️
Coding
View GitHub Profile
@lordcodes
lordcodes / app_build.gradle.kts
Created Nov 28, 2020
Brief demo of using a platform module to constrain dependency versions
View app_build.gradle.kts
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 Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View AuthorizationInterceptor.kt
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 Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View CreateAccountRemoteApi.kt
interface CreateAccountRemoteApi {
@POST("user/identity")
fun createAccount(
@Body request: CreateAccountRemoteDto,
@Tag authorization: AuthorizationType = AuthorizationType.CLIENT_CREDENTIALS
): Single<AccountCreatedRemoteDto>
}
@lordcodes
lordcodes / AuthorizationType.kt
Created Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View AuthorizationType.kt
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 Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View OkHttpClientBuilder.kt
OkHttpClient.Builder()
.addInterceptor(authorizationInterceptor)
.authenticator(tokenRefreshAuthenticator)
.build()
@lordcodes
lordcodes / TokenRefreshAuthenticator.kt
Created Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View TokenRefreshAuthenticator.kt
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 Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View ResponseRetryCount.kt
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 Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View RequestSigning.kt
fun Request.signWithToken(accessToken: AccessToken) =
newBuilder()
.header("Authorization", "Bearer ${accessToken.rawToken}")
.build()
@lordcodes
lordcodes / OkHttpClientBuilder.kt
Created Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View OkHttpClientBuilder.kt
fun okHttpClient(authorizationInterceptor: AuthorizationInterceptor) =
OkHttpClient.Builder()
.addInterceptor(authorizationInterceptor)
.build()
@lordcodes
lordcodes / AuthorizationInterceptor.kt
Created Mar 17, 2020
Code for the article: "Authorization and retrying of web requests for OkHttp and Retrofit"
View AuthorizationInterceptor.kt
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()