Skip to content

Commit

Permalink
Add more data classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rgryta committed Oct 24, 2024
1 parent baa2d86 commit 2fcb9b1
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 29 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ junit = "4.13.2"
kotlin = "2.0.21"
kotlinx-coroutines = "1.9.0"
kotlinx-serialization = "1.7.3"
kotlinx-datetime = "0.6.1"
ktor = "3.0.0-rc-1"
vanniktech = "0.30.0"

[libraries]
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-datetime" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
Expand Down
3 changes: 2 additions & 1 deletion library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ kotlin {
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.coroutines.test)
implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.datetime)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.client.serialization)
Expand Down Expand Up @@ -80,7 +81,7 @@ android {
compileSdk = 35

defaultConfig {
minSdk = 24
minSdk = 26
}
}

Expand Down
76 changes: 52 additions & 24 deletions library/src/commonMain/kotlin/io/wellmate/api/client/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@ import io.ktor.client.HttpClientConfig
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logging
import io.ktor.http.ContentType
import io.ktor.http.HeadersBuilder
import io.ktor.http.parameters
import io.ktor.serialization.kotlinx.json.json
import io.ktor.utils.io.ByteReadChannel
import io.wellmate.api.client.auth.Email
import io.wellmate.api.client.app.MealFields
import io.wellmate.api.client.app.Message
import io.wellmate.api.client.auth.EmailPassword
import io.wellmate.api.client.auth.OAuthToken
import io.wellmate.api.client.auth.Token
import io.wellmate.api.client.userData.PotentialUser
import io.wellmate.api.client.userData.PotentialUserFields
import io.wellmate.api.client.userData.UserInfo
import io.wellmate.api.client.userData.UserInfoFields
import kotlinx.serialization.json.Json

fun HttpClientConfig<*>.addLogging() {
Expand Down Expand Up @@ -49,9 +60,9 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: Message,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
): ResponseWrapper<MealFields> {
return endpoint.post(body = body) { headers() }
}
}
Expand All @@ -61,9 +72,9 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: Message,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
): ResponseWrapper<MealFields> {
return endpoint.post(body = body) { headers() }
}
}
Expand All @@ -73,9 +84,9 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: Message,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
): ResponseWrapper<MealFields> {
return endpoint.post(body = body) { headers() }
}
}
Expand All @@ -89,10 +100,14 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: ByteReadChannel,
contentType: ContentType = ContentType.Image.JPEG,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
): ResponseWrapper<MealFields> {
return endpoint.post(
body = body,
contentType = contentType
) { headers() }
}
}
}
Expand All @@ -106,11 +121,24 @@ object Client {
private const val URL = "${Login.URL}/password"
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
suspend fun submitForm(
username: String,
password: String,
secChUaModel: String = "Unknown device",
secChUaPlatform: String = "N/A",
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
): ResponseWrapper<Token> {

return endpoint.submitForm(formParameters = { parameters {
append("username", username)
append("password", password)
}}) {
io.ktor.http.headers {
headers()
append("sec-ch-ua-model", secChUaModel)
append("sec-ch-ua-platform", secChUaPlatform)
}
}
}
}

Expand All @@ -119,9 +147,9 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: OAuthToken,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
): ResponseWrapper<Token> {
return endpoint.post(body = body) { headers() }
}
}
Expand All @@ -131,9 +159,9 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: OAuthToken,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
): ResponseWrapper<Token> {
return endpoint.post(body = body) { headers() }
}
}
Expand Down Expand Up @@ -181,13 +209,13 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

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

suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<UserInfo> {
return endpoint.get { headers() }
}
}
Expand All @@ -201,7 +229,7 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: Email,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
return endpoint.post(body = body) { headers() }
Expand All @@ -224,8 +252,8 @@ 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> {
return endpoint.get { headers() }
suspend fun post(body: PotentialUserFields, headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<PotentialUser> {
return endpoint.post(body = body) { headers() }
}
}
}
Expand Down Expand Up @@ -298,7 +326,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<io.wellmate.api.client.userData.PotentialUser> {
return endpoint.get { headers() }
}
}
Expand Down
19 changes: 19 additions & 0 deletions library/src/commonMain/kotlin/io/wellmate/api/client/Endpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.wellmate.api.client
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.delete
import io.ktor.client.request.forms.submitForm
import io.ktor.client.request.get
import io.ktor.client.request.headers
import io.ktor.client.request.post
Expand All @@ -11,8 +12,11 @@ import io.ktor.client.statement.HttpResponse
import io.ktor.http.ContentType
import io.ktor.http.HeadersBuilder
import io.ktor.http.HttpStatusCode
import io.ktor.http.Parameters
import io.ktor.http.ParametersBuilder
import io.ktor.http.contentType
import io.ktor.http.isSuccess
import io.ktor.http.parameters
import io.ktor.util.reflect.TypeInfo
import io.ktor.util.reflect.typeInfo

Expand Down Expand Up @@ -45,6 +49,21 @@ class Endpoint(val client: HttpClient, val url: String) {
return ResponseWrapper(response, typeInfo<T>())
}

suspend inline fun <reified T : Any> submitForm(
crossinline formParameters: ParametersBuilder.() -> Unit = {},
crossinline headers: HeadersBuilder.() -> Unit = { },
): ResponseWrapper<T> {
val response: HttpResponse = client.submitForm(url = url,
formParameters = parameters {
formParameters()
}) {
headers {
headers()
}
}
return ResponseWrapper(response, typeInfo<T>())
}

suspend inline fun <reified T : Any> post(
body: Any,
contentType: ContentType = ContentType.Application.Json,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.wellmate.api.client.app

import kotlinx.serialization.Serializable


@Serializable
class IngredientFields(
val name: String,
val amount: Float,

val kilocalories: Int,
val proteins: Float,
val carbohydrates: Float,
val fats: Float,
val sugars: Float,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.wellmate.api.client.app

import kotlinx.serialization.Serializable

@Serializable
class MealFields(
val name: String,
val ingredients: List<IngredientFields>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.wellmate.api.client.app

import kotlinx.serialization.Serializable

@Serializable
data class Message(val message: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.wellmate.api.client.auth

import kotlinx.serialization.Serializable

@Serializable
data class Email(val email: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.wellmate.api.client.auth

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable


@Serializable
data class OAuthToken(@SerialName("access_token") val accessToken: String)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ import kotlinx.serialization.Serializable
data class Token(
@SerialName("access_token") val accessToken: String,
@SerialName("token_type") val tokenType: String
)

@Serializable
data class OAuthToken(@SerialName("access_token") val accessToken: String)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.wellmate.api.client.userData

import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer
import kotlinx.serialization.Serializable

@Serializable
data class PotentialUserFields(val email: String, val name: String, val info: String?)

@Serializable
data class PotentialUser(
val email: String,
val name: String,
val info: String?,
@Serializable(with = LocalDateTimeIso8601Serializer::class) val added: LocalDateTime
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.wellmate.api.client.userData

import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer

@Serializable
enum class Sex {
@SerialName("m") M,
@SerialName("f") F,
}

@Serializable
data class UserInfoFields(
val name: String?,
val surname: String?,

@Serializable(with = LocalDateTimeIso8601Serializer::class) val birthday: LocalDateTime?,
val sex: Sex?,
val weight: Int?,
val height: Int?,
val country: String?,
)

@Serializable
data class UserInfo(
@SerialName("user_id") val userId: Int?,
val name: String?,
val surname: String?,

@Serializable(with = LocalDateTimeIso8601Serializer::class) val birthday: LocalDateTime?,
val sex: Sex?,
val weight: Int?,
val height: Int?,
val country: String?,
)

0 comments on commit 2fcb9b1

Please sign in to comment.