Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SamuelMarks opened this issue Oct 7, 2024 · 0 comments

Comments

@SamuelMarks
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

1 participant