diff --git a/library/src/commonMain/kotlin/io/wellmate/api/client/Client.kt b/library/src/commonMain/kotlin/io/wellmate/api/client/Client.kt index 2ee6001..8af3d2c 100644 --- a/library/src/commonMain/kotlin/io/wellmate/api/client/Client.kt +++ b/library/src/commonMain/kotlin/io/wellmate/api/client/Client.kt @@ -1,3 +1,5 @@ +@file:Suppress("PrivatePropertyName", "unused") + package io.wellmate.api.client import io.ktor.client.HttpClient @@ -48,7 +50,7 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } @@ -60,7 +62,7 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } @@ -72,7 +74,7 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } @@ -88,7 +90,7 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } @@ -106,7 +108,7 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } @@ -118,7 +120,7 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } @@ -130,7 +132,7 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } @@ -143,7 +145,7 @@ object Client { suspend fun post( body: EmailPassword, - headers: HeadersBuilder.() -> Unit = { run {} } + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() @@ -154,17 +156,17 @@ object Client { private const val URL = "${User.URL}/me" private val endpoint = Endpoint(client = client, url = URL) - suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.get { headers() } } } class UserId(private val userId: Int) { - val URL: String + private val URL: String get() = "${User.URL}/$userId" private val endpoint = Endpoint(client = client, url = URL) - suspend fun delete(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun delete(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.delete { headers() } } } @@ -174,18 +176,18 @@ object Client { private val endpoint = Endpoint(client = client, url = URL) class UserId(private val userId: Int) { - val URL: String + private val URL: String get() = "${Info.URL}/$userId" private val endpoint = Endpoint(client = client, url = URL) suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } - suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.get { headers() } } } @@ -200,18 +202,18 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } } class Activate(private val activationCode: String) { - val URL: String + private val URL: String get() = "${Activation.URL}/activate/${activationCode}" private val endpoint = Endpoint(client = client, url = URL) - suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.get { headers() } } } @@ -222,7 +224,7 @@ object Client { private const val URL = "${User.URL}/potential" private val endpoint = Endpoint(client = client, url = URL) - suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.get { headers() } } } @@ -232,7 +234,7 @@ object Client { private const val URL = "${Api.URL}/entry" private val endpoint = Endpoint(client = client, url = URL) - suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.get { headers() } } @@ -242,21 +244,21 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } - suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.get { headers() } } class MealId(private val mealId: Int) { - val URL: String + private val URL: String get() = "${Meal.URL}/$mealId" private val endpoint = Endpoint(client = client, url = URL) - suspend fun delete(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun delete(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.delete { headers() } } } @@ -268,21 +270,21 @@ object Client { suspend fun post( body: Any, - headers: HeadersBuilder.() -> Unit + headers: HeadersBuilder.() -> Unit = { } ): ResponseWrapper { return endpoint.post(body = body) { headers() } } - suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.get { headers() } } class TimerId(private val timerId: Int) { - val URL: String + private val URL: String get() = "${Timer.URL}/$timerId" private val endpoint = Endpoint(client = client, url = URL) - suspend fun delete(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun delete(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.delete { headers() } } } @@ -296,7 +298,7 @@ object Client { private const val URL = "${Admin.URL}/potential-user" private val endpoint = Endpoint(client = client, url = URL) - suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper { + suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper { return endpoint.get { headers() } } } diff --git a/library/src/commonTest/kotlin/ApiTest.kt b/library/src/commonTest/kotlin/ApiTest.kt index ec5cb74..c028081 100644 --- a/library/src/commonTest/kotlin/ApiTest.kt +++ b/library/src/commonTest/kotlin/ApiTest.kt @@ -25,7 +25,7 @@ class ApiTest { fun `api-user-me get fails with no headers`() = runTest(testDispatcher) { val endpoint = Client.Api.User.Me assertFalse( - endpoint.get { { } }.status.isSuccess(), + endpoint.get { }.status.isSuccess(), "/api/user/me:get should not be successful when no header is provided" ) } @@ -37,7 +37,7 @@ class ApiTest { IllegalArgumentException::class, "/api/user:post should not be successful when body is empty" ) { - endpoint.post(body = EmailPassword("", "")) { { } } + endpoint.post(body = EmailPassword("", "")) } } @@ -45,11 +45,12 @@ class ApiTest { fun `api-user post+delete succeeds with proper body + with me get`() = runTest(testDispatcher) { val endpoint = Client.Api.User - // Some fake credentials + // Fake credentials generated for test purposes only val emailPassword = EmailPassword( email = "noreply@wellmate.io", password = "nFbz\$Qc%!!@PgLl@5\\$^pH47XW9vl2D!SEp@b", ) + val resultCreate = endpoint.post(body = emailPassword) assertTrue(resultCreate.status.isSuccess()) val token = resultCreate.body() @@ -63,7 +64,8 @@ class ApiTest { assertTrue(resultMe.status.isSuccess()) val me = resultMe.body() - val resultDelete = endpoint.userId(userId = me.id).delete { + val deleteEndpoint = Client.Api.User.UserId(userId = me.id) + val resultDelete = deleteEndpoint.delete { append( HttpHeaders.Authorization, "${token.tokenType} ${token.accessToken}",