Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rgryta committed Oct 23, 2024
1 parent a8d6bda commit a534df3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
58 changes: 30 additions & 28 deletions library/src/commonMain/kotlin/io/wellmate/api/client/Client.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("PrivatePropertyName", "unused")

package io.wellmate.api.client

import io.ktor.client.HttpClient
Expand Down Expand Up @@ -48,7 +50,7 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}
Expand All @@ -60,7 +62,7 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}
Expand All @@ -72,7 +74,7 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}
Expand All @@ -88,7 +90,7 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}
Expand All @@ -106,7 +108,7 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}
Expand All @@ -118,7 +120,7 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}
Expand All @@ -130,7 +132,7 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}
Expand All @@ -143,7 +145,7 @@ object Client {

suspend fun post(
body: EmailPassword,
headers: HeadersBuilder.() -> Unit = { run {} }
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Token> {
return endpoint.post(body = body) {
headers()
Expand All @@ -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<io.wellmate.api.client.userData.Me> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<io.wellmate.api.client.userData.Me> {
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<Any> {
suspend fun delete(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
return endpoint.delete { headers() }
}
}
Expand All @@ -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<Any> {
return endpoint.post(body = body) { headers() }
}

suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
return endpoint.get { headers() }
}
}
Expand All @@ -200,18 +202,18 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
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<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
return endpoint.get { headers() }
}
}
Expand All @@ -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<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
return endpoint.get { headers() }
}
}
Expand All @@ -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<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
return endpoint.get { headers() }
}

Expand All @@ -242,21 +244,21 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}

suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
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<Any> {
suspend fun delete(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
return endpoint.delete { headers() }
}
}
Expand All @@ -268,21 +270,21 @@ object Client {

suspend fun post(
body: Any,
headers: HeadersBuilder.() -> Unit
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
}

suspend fun get(headers: HeadersBuilder.() -> Unit): ResponseWrapper<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
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<Any> {
suspend fun delete(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
return endpoint.delete { headers() }
}
}
Expand All @@ -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<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
return endpoint.get { headers() }
}
}
Expand Down
10 changes: 6 additions & 4 deletions library/src/commonTest/kotlin/ApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
Expand All @@ -37,19 +37,20 @@ class ApiTest {
IllegalArgumentException::class,
"/api/user:post should not be successful when body is empty"
) {
endpoint.post(body = EmailPassword("", "")) { { } }
endpoint.post(body = EmailPassword("", ""))
}
}

@Test
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 = "[email protected]",
password = "nFbz\$Qc%!!@PgLl@5\\$^pH47XW9vl2D!SEp@b",
)

val resultCreate = endpoint.post(body = emailPassword)
assertTrue(resultCreate.status.isSuccess())
val token = resultCreate.body()
Expand All @@ -63,7 +64,8 @@ class ApiTest {
assertTrue(resultMe.status.isSuccess())
val me = resultMe.body()

val resultDelete = endpoint.userId(userId = me.id).delete<Any> {
val deleteEndpoint = Client.Api.User.UserId(userId = me.id)
val resultDelete = deleteEndpoint.delete {
append(
HttpHeaders.Authorization,
"${token.tokenType} ${token.accessToken}",
Expand Down

0 comments on commit a534df3

Please sign in to comment.