Repository: cjbooms/fabrikt
Pull request #50 (2021-06-03 14:23:32)

Improving client
- Making the OAuth class a general purpose Interceptor and Authenticator that takes a token function
- Improving formatting of client classes

cjbooms: 👍 

Patch:
diff --git a/src/main/resources/templates/client-code/http-resilience4j-util.kt.hbs b/src/main/resources/templates/client-code/http-resilience4j-util.kt.hbs
index 495613b..ac584e3 100644
--- a/src/main/resources/templates/client-code/http-resilience4j-util.kt.hbs
+++ b/src/main/resources/templates/client-code/http-resilience4j-util.kt.hbs
@@ -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()
diff --git a/src/main/resources/templates/client-code/logging-interceptor.kt.hbs b/src/main/resources/templates/client-code/logging-interceptor.kt.hbs
index 6df9283..47e4430 100644
--- a/src/main/resources/templates/client-code/logging-interceptor.kt.hbs
+++ b/src/main/resources/templates/client-code/logging-interceptor.kt.hbs
@@ -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
     }
-}
\ No newline at end of file
+}
diff --git a/src/main/resources/templates/client-code/oauth.kt.hbs b/src/main/resources/templates/client-code/oauth.kt.hbs
index 9d29db8..797936a 100644
--- a/src/main/resources/templates/client-code/oauth.kt.hbs
+++ b/src/main/resources/templates/client-code/oauth.kt.hbs
@@ -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)
+    }
 }
\ No newline at end of file


Test patch:
diff --git a/src/test/resources/examples/okHttpClient/client/HttpResilience4jUtil.kt b/src/test/resources/examples/okHttpClient/client/HttpResilience4jUtil.kt
index 21de2a2..d7d929d 100644
--- a/src/test/resources/examples/okHttpClient/client/HttpResilience4jUtil.kt
+++ b/src/test/resources/examples/okHttpClient/client/HttpResilience4jUtil.kt
@@ -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()
diff --git a/src/test/resources/examples/okHttpClient/client/LoggingInterceptor.kt b/src/test/resources/examples/okHttpClient/client/LoggingInterceptor.kt
index 59ba11d..6e95b99 100644
--- a/src/test/resources/examples/okHttpClient/client/LoggingInterceptor.kt
+++ b/src/test/resources/examples/okHttpClient/client/LoggingInterceptor.kt
@@ -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
     }
-}
\ No newline at end of file
+}
diff --git a/src/test/resources/examples/okHttpClient/client/OAuth.kt b/src/test/resources/examples/okHttpClient/client/OAuth.kt
index 500a4d4..ab4c431 100644
--- a/src/test/resources/examples/okHttpClient/client/OAuth.kt
+++ b/src/test/resources/examples/okHttpClient/client/OAuth.kt
@@ -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()
-}
\ No newline at end of file
+            .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)
+    }
+}