Skip to content

Commit

Permalink
- Making the OAuth class general purpose Interceptor and Authenticato…
Browse files Browse the repository at this point in the history
…r. (#50)

- Improving the linting of client utility classes
  • Loading branch information
cjbooms authored Jun 3, 2021
1 parent 23fa5c8 commit d7c8343
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry

fun <T> withCircuitBreaker(
circuitBreakerRegistry: CircuitBreakerRegistry,
apiClientName: String, apiCall: () -> ApiResponse<T>
apiClientName: String,
apiCall: () -> ApiResponse<T>
): ApiResponse<T> {
val circuitBreaker = circuitBreakerRegistry.circuitBreaker(apiClientName)
return CircuitBreaker.decorateSupplier(circuitBreaker, apiCall).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class LoggingInterceptor : Interceptor {
val request = chain.request()

val t1 = System.nanoTime()
logger.info("Client Request: ${request}")
logger.info("Client Request: $request")

val response = chain.proceed(request)

val t2 = System.nanoTime()
logger.info("Client Response after ${(t2 - t1) / 100_000}ms: ${response}")
logger.info("Client Response after ${(t2 - t1) / 100_000}ms: $response")

return response
}
}
}
20 changes: 12 additions & 8 deletions src/main/resources/templates/client-code/oauth.kt.hbs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package {{ client }}

import okhttp3.Authenticator
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import okio.IOException

class OAuth2 : Authenticator {
var accessToken: String? = null
class OAuth2(val accessToken: () -> String) : Authenticator, Interceptor {

@Throws(IOException::class)
override fun authenticate(route: Route?, response: Response): Request? =
override fun authenticate(route: Route?, response: Response): Request =
response.request.newBuilder()
.header("Authorization", "Bearer $accessToken")
.build()
.header("Authorization", "Bearer ${accessToken().trim()}")
.build()

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.header("Authorization", "Bearer ${accessToken().trim()}")
.build()
return chain.proceed(request)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry

fun <T> withCircuitBreaker(
circuitBreakerRegistry: CircuitBreakerRegistry,
apiClientName: String, apiCall: () -> ApiResponse<T>
apiClientName: String,
apiCall: () -> ApiResponse<T>
): ApiResponse<T> {
val circuitBreaker = circuitBreakerRegistry.circuitBreaker(apiClientName)
return CircuitBreaker.decorateSupplier(circuitBreaker, apiCall).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class LoggingInterceptor : Interceptor {
val request = chain.request()

val t1 = System.nanoTime()
logger.info("Client Request: ${request}")
logger.info("Client Request: $request")

val response = chain.proceed(request)

val t2 = System.nanoTime()
logger.info("Client Response after ${(t2 - t1) / 100_000}ms: ${response}")
logger.info("Client Response after ${(t2 - t1) / 100_000}ms: $response")

return response
}
}
}
21 changes: 13 additions & 8 deletions src/test/resources/examples/okHttpClient/client/OAuth.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package examples.okHttpClient.client

import okhttp3.Authenticator
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import okio.IOException

class OAuth2 : Authenticator {
var accessToken: String? = null
class OAuth2(val accessToken: () -> String) : Authenticator, Interceptor {

@Throws(IOException::class)
override fun authenticate(route: Route?, response: Response): Request? =
override fun authenticate(route: Route?, response: Response): Request =
response.request.newBuilder()
.header("Authorization", "Bearer $accessToken")
.build()
}
.header("Authorization", "Bearer ${accessToken().trim()}")
.build()

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.header("Authorization", "Bearer ${accessToken().trim()}")
.build()
return chain.proceed(request)
}
}

0 comments on commit d7c8343

Please sign in to comment.