Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rgryta committed Oct 24, 2024
1 parent 2fcb9b1 commit 4fc57cd
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 77 deletions.
37 changes: 22 additions & 15 deletions library/src/commonMain/kotlin/io/wellmate/api/client/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ 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.entry.MealFields
import io.wellmate.api.client.entry.MealFieldsClient
import io.wellmate.api.client.entry.TimerFieldsClient
import io.wellmate.api.client.generics.Email
import io.wellmate.api.client.generics.Message
import io.wellmate.api.client.userData.PotentialUser
import io.wellmate.api.client.userData.PotentialUserFields
import io.wellmate.api.client.userData.UserInfo
Expand Down Expand Up @@ -129,10 +131,12 @@ object Client {
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Token> {

return endpoint.submitForm(formParameters = { parameters {
append("username", username)
append("password", password)
}}) {
return endpoint.submitForm(formParameters = {
parameters {
append("username", username)
append("password", password)
}
}) {
io.ktor.http.headers {
headers()
append("sec-ch-ua-model", secChUaModel)
Expand Down Expand Up @@ -252,7 +256,10 @@ object Client {
private const val URL = "${User.URL}/potential"
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(body: PotentialUserFields, headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<PotentialUser> {
suspend fun post(
body: PotentialUserFields,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<PotentialUser> {
return endpoint.post(body = body) { headers() }
}
}
Expand All @@ -262,7 +269,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<io.wellmate.api.client.entry.Entry> {
return endpoint.get { headers() }
}

Expand All @@ -271,13 +278,13 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: MealFieldsClient,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
): ResponseWrapper<io.wellmate.api.client.entry.Meal> {
return endpoint.post(body = body) { headers() }
}

suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<io.wellmate.api.client.entry.Meal> {
return endpoint.get { headers() }
}

Expand All @@ -297,13 +304,13 @@ object Client {
private val endpoint = Endpoint(client = client, url = URL)

suspend fun post(
body: Any,
body: TimerFieldsClient,
headers: HeadersBuilder.() -> Unit = { }
): ResponseWrapper<Any> {
): ResponseWrapper<io.wellmate.api.client.entry.Timer> {
return endpoint.post(body = body) { headers() }
}

suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<Any> {
suspend fun get(headers: HeadersBuilder.() -> Unit = { }): ResponseWrapper<io.wellmate.api.client.entry.Timer> {
return endpoint.get { headers() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.wellmate.api.client.entry

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


interface EntryBaseFieldsClient {
val timestamp: LocalDateTime
}


interface EntryBase : EntryBaseFieldsClient {
val id: Int
val userId: Int

val added: LocalDateTime
}

@Serializable
enum class EntryType {
@SerialName("meal")
MEAL,

@SerialName("timer")
TIMER,
}

@Serializable
data class Entry(
val id: Int,
@Serializable(with = LocalDateTimeIso8601Serializer::class) val added: LocalDateTime,
@SerialName("user_id") val userId: Int,
@Serializable(with = LocalDateTimeIso8601Serializer::class) val timestamp: LocalDateTime,
val type: EntryType,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.wellmate.api.client.entry

import kotlinx.serialization.Serializable


@Serializable
data class IngredientFields(
val name: String,

val amount: Float,

val kilocalories: Int,
val proteins: Float,
val carbohydrates: Float,
val fats: Float,
val sugars: Float,
)

@Serializable
data class Ingredient(
val id: Int,

val name: String,
val amount: Float,

val kilocalories: Int,
val proteins: Float,
val carbohydrates: Float,
val fats: Float,
val sugars: Float,
)
37 changes: 37 additions & 0 deletions library/src/commonMain/kotlin/io/wellmate/api/client/entry/Meal.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.wellmate.api.client.entry

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

interface MealInterface {
val name: String
val ingredients: List<Any>
}

@Serializable
data class MealFields(
override val name: String,
override val ingredients: List<IngredientFields>,
) : MealInterface

@Serializable
data class MealFieldsClient(
@Serializable(with = LocalDateTimeIso8601Serializer::class) override val timestamp: LocalDateTime,

override val name: String,
override val ingredients: List<IngredientFields>,
) : EntryBaseFieldsClient, MealInterface

@Serializable
data class Meal(
override val id: Int,
@SerialName("user_id") override val userId: Int,

@Serializable(with = LocalDateTimeIso8601Serializer::class) override val added: LocalDateTime,
@Serializable(with = LocalDateTimeIso8601Serializer::class) override val timestamp: LocalDateTime,

override val name: String,
override val ingredients: List<Ingredient>,
) : EntryBase, MealInterface
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.wellmate.api.client.entry

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

interface TimerInterface {
val duration: Int
}

@Serializable
data class TimerFieldsClient(
@Serializable(with = LocalDateTimeIso8601Serializer::class) override val timestamp: LocalDateTime,

override val duration: Int,
) : EntryBaseFieldsClient, TimerInterface


@Serializable
data class Timer(
override val id: Int,
@SerialName("user_id") override val userId: Int,

@Serializable(with = LocalDateTimeIso8601Serializer::class) override val timestamp: LocalDateTime,
@Serializable(with = LocalDateTimeIso8601Serializer::class) override val added: LocalDateTime,

override val duration: Int,
) : EntryBase, TimerInterface
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.wellmate.api.client.auth
package io.wellmate.api.client.generics

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.wellmate.api.client.app
package io.wellmate.api.client.generics

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package io.wellmate.api.client.userData

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

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

@SerialName("f")
F,
}

@Serializable
Expand Down
Loading

0 comments on commit 4fc57cd

Please sign in to comment.