Skip to content

v3, generated a new client SDK for Kotlin (ktor with Kotlin Multiplatform) #31

@SamuelMarks

Description

@SamuelMarks

Related #3

Currently just copypasta around, but if there's interest then will create a new OpenAPI generator for ktor. For my WiP see https://github.com/SamuelMarks/openfoodfacts-kotlin-openapi

My hacky usage that I copied into my Kotlin Multiplatform [desktop, web, iOS, Android] project looks something like:

val scope = rememberCoroutineScope()
        LaunchedEffect(barcode) {
            try {
                val product = scope.async {
                    openFoodFactsApi.productById(barcode)
                }.await().product

With this one route I implemented by hand, obviously needs to be generated:

class HttpResponseException(val response: HttpResponse) : Exception(response.toString())

class OpenFoodFactsApi(private val client: HttpClient) {
    private val base = "https://world.openfoodfacts.org/api/v3"

    suspend fun productById(id: String): PatchApiV3ProductBarcode200Response {
        val response: HttpResponse = client.get("$base/product/$id.json")
        println("status = ${response.status}")
        if (response.status.isSuccess())
            return response.body()
        throw HttpResponseException(response)
    }
}

And yes I know PatchApiV3ProductBarcode200Response is the wrong type but actually it's the right type based on what GET returns. Happily using the code generated code with some find/replace focusing on switching to kotlinx for marshalling and avoiding the square moshi stuff. Writing it by hand would be painful! - All those attributes

Lots of weirdness from your OpenAPI schema. @teolemon not sure how you want to proceed. Like, for example, there's a bunch of commits where I've need to replace ints with floats like in your nutrition_score_warning_fruits_vegetables_nuts_estimate_from_ingredients_value & others.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Untriaged backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions