Skip to content

Commit 4f2c44d

Browse files
committed
refactor: update token handling to use lambda type and enhance exception handling in generators and tests
- Changed `token` parameter and property to accept `() -> String` instead of `String`. - Improved exception handling by adding `HttpRequestTimeoutException` in `safeCall`. - Adjusted tests to reflect updated `token` and exception handling logic.
1 parent 109a81f commit 4f2c44d

5 files changed

Lines changed: 30 additions & 9 deletions

File tree

core/src/main/kotlin/com/avsystem/justworks/core/gen/ApiClientBaseGenerator.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ object ApiClientBaseGenerator {
6363
.beginControlFlow("return when (status.value)")
6464
.addStatement("in 200..299 -> %T(status.value, %L())", HTTP_SUCCESS, SUCCESS_BODY)
6565
.addStatement(
66+
"in 300..399 -> %M(%T(status.value, %M(), %T.Server))",
67+
RAISE_FUN,
68+
HTTP_ERROR,
69+
BODY_AS_TEXT_FUN,
70+
HTTP_ERROR_TYPE,
71+
).addStatement(
6672
"in 400..499 -> %M(%T(status.value, %M(), %T.Client))",
6773
RAISE_FUN,
6874
HTTP_ERROR,
@@ -97,10 +103,12 @@ object ApiClientBaseGenerator {
97103
.build()
98104

99105
private fun buildApiClientBaseClass(): TypeSpec {
106+
val tokenType = LambdaTypeName.get(returnType = STRING)
107+
100108
val constructor = FunSpec
101109
.constructorBuilder()
102110
.addParameter(BASE_URL, STRING)
103-
.addParameter(TOKEN, STRING)
111+
.addParameter(TOKEN, tokenType)
104112
.build()
105113

106114
val baseUrlProp = PropertySpec
@@ -110,7 +118,7 @@ object ApiClientBaseGenerator {
110118
.build()
111119

112120
val tokenProp = PropertySpec
113-
.builder(TOKEN, STRING)
121+
.builder(TOKEN, tokenType)
114122
.initializer(TOKEN)
115123
.addModifiers(KModifier.PRIVATE)
116124
.build()
@@ -149,7 +157,7 @@ object ApiClientBaseGenerator {
149157
.addStatement(
150158
"append(%T.Authorization, %P)",
151159
HTTP_HEADERS,
152-
CodeBlock.of($$"Bearer ${'$'}{$$TOKEN}"),
160+
CodeBlock.of($$"Bearer ${'$'}{$$TOKEN()}"),
153161
).endControlFlow()
154162
.build()
155163

@@ -162,6 +170,13 @@ object ApiClientBaseGenerator {
162170
.beginControlFlow("return try")
163171
.addStatement("%L()", BLOCK)
164172
.nextControlFlow("catch (e: %T)", IO_EXCEPTION)
173+
.addStatement(
174+
"%M(%T(0, e.message ?: %S, %T.Network))",
175+
RAISE_FUN,
176+
HTTP_ERROR,
177+
NETWORK_ERROR,
178+
HTTP_ERROR_TYPE,
179+
).nextControlFlow("catch (e: %T)", HTTP_REQUEST_TIMEOUT_EXCEPTION)
165180
.addStatement(
166181
"%M(%T(0, e.message ?: %S, %T.Network))",
167182
RAISE_FUN,

core/src/main/kotlin/com/avsystem/justworks/core/gen/ClientGenerator.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.squareup.kotlinpoet.ExperimentalKotlinPoetApi
1313
import com.squareup.kotlinpoet.FileSpec
1414
import com.squareup.kotlinpoet.FunSpec
1515
import com.squareup.kotlinpoet.KModifier
16+
import com.squareup.kotlinpoet.LambdaTypeName
1617
import com.squareup.kotlinpoet.MemberName
1718
import com.squareup.kotlinpoet.ParameterSpec
1819
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
@@ -50,10 +51,12 @@ class ClientGenerator(private val apiPackage: String, private val modelPackage:
5051
CodeBlock.of("$CREATE_HTTP_CLIENT()")
5152
}
5253

54+
val tokenType = LambdaTypeName.get(returnType = STRING)
55+
5356
val primaryConstructor = FunSpec
5457
.constructorBuilder()
5558
.addParameter(BASE_URL, STRING)
56-
.addParameter(TOKEN, STRING)
59+
.addParameter(TOKEN, tokenType)
5760
.build()
5861

5962
val httpClientProperty = PropertySpec

core/src/main/kotlin/com/avsystem/justworks/core/gen/Names.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ val HTTP_SUCCESS = ClassName("com.avsystem.justworks", "HttpSuccess")
6969

7070
val CLOSEABLE = ClassName("java.io", "Closeable")
7171
val IO_EXCEPTION = ClassName("java.io", "IOException")
72+
val HTTP_REQUEST_TIMEOUT_EXCEPTION = ClassName("io.ktor.client.plugins", "HttpRequestTimeoutException")
7273
val OPT_IN = ClassName("kotlin", "OptIn")
7374

7475
// ============================================================================

core/src/test/kotlin/com/avsystem/justworks/core/gen/ApiClientBaseGeneratorTest.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.squareup.kotlinpoet.FunSpec
55
import com.squareup.kotlinpoet.KModifier
66
import com.squareup.kotlinpoet.ParameterizedTypeName
77
import com.squareup.kotlinpoet.TypeSpec
8-
import com.squareup.kotlinpoet.TypeVariableName
98
import kotlin.test.Test
109
import kotlin.test.assertEquals
1110
import kotlin.test.assertNotNull
@@ -34,11 +33,13 @@ class ApiClientBaseGeneratorTest {
3433
}
3534

3635
@Test
37-
fun `ApiClientBase has constructor with baseUrl and token`() {
36+
fun `ApiClientBase has constructor with baseUrl and token provider`() {
3837
val constructor = assertNotNull(classSpec.primaryConstructor)
3938
val paramNames = constructor.parameters.map { it.name }
4039
assertTrue("baseUrl" in paramNames)
4140
assertTrue("token" in paramNames)
41+
val tokenParam = constructor.parameters.first { it.name == "token" }
42+
assertEquals("() -> kotlin.String", tokenParam.type.toString(), "token should be a () -> String lambda")
4243
}
4344

4445
@Test
@@ -64,7 +65,7 @@ class ApiClientBaseGeneratorTest {
6465
val body = applyAuth.body.toString()
6566
assertTrue(body.contains("Authorization"), "Expected Authorization header")
6667
assertTrue(body.contains("Bearer"), "Expected Bearer prefix")
67-
assertTrue(body.contains("token"), "Expected token reference")
68+
assertTrue(body.contains("token()"), "Expected token() invocation")
6869
}
6970

7071
@Test
@@ -75,6 +76,7 @@ class ApiClientBaseGeneratorTest {
7576
assertTrue(safeCall.contextParameters.isNotEmpty(), "Expected context parameter")
7677
val body = safeCall.body.toString()
7778
assertTrue(body.contains("IOException"), "Expected IOException catch")
79+
assertTrue(body.contains("HttpRequestTimeoutException"), "Expected HttpRequestTimeoutException catch")
7880
assertTrue(body.contains("Network error"), "Expected Network error message")
7981
}
8082

core/src/test/kotlin/com/avsystem/justworks/core/gen/ClientGeneratorTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ class ClientGeneratorTest {
297297
// -- AUTH-01: Client constructor has token parameter --
298298

299299
@Test
300-
fun `client constructor has token parameter`() {
300+
fun `client constructor has token provider parameter`() {
301301
val cls = clientClass(listOf(endpoint()))
302302
val constructor = assertNotNull(cls.primaryConstructor)
303303
val token = constructor.parameters.first { it.name == "token" }
304-
assertTrue(token.type.toString().contains("String"), "token should return String")
304+
assertEquals("() -> kotlin.String", token.type.toString(), "token should be a () -> String lambda")
305305
}
306306

307307
// -- Pitfall 3: Untagged endpoints go to DefaultClient --

0 commit comments

Comments
 (0)