foss42 / apidash

API Dash is a beautiful open-source cross-platform API Client built using Flutter which can help you easily create & customize your API requests, visually inspect responses and generate API integration code. It is supported for macOS, Windows, Linux, Android & iOS. A lightweight alternative to postman/insomnia.
https://apidash.dev
Apache License 2.0
1.66k stars 317 forks source link

Add feat codegen for Kotlin using retrofit #294

Closed Photon3009 closed 7 months ago

Photon3009 commented 7 months ago

PR Description

This PR introduces support for generating Kotlin code using Retrofit for API communication. Retrofit is a type-safe HTTP client for Android and Java by Square, Inc., that makes it easy to consume RESTful web services

Description of image

Related Issues

Checklist

Added/updated tests?

We encourage you to add relevant test cases.

animator commented 7 months ago

flutter test is failing for 4 test cases. Recheck.

Photon3009 commented 7 months ago

okay

Photon3009 commented 7 months ago

@animator please now check, I've updated the pr

animator commented 7 months ago

@Photon3009 Few points:

Photon3009 commented 7 months ago

hey @animator, please check the example below. If it works, then I will write the remaining tests for this. Otherwise, let me know if we need to add code to print a generic response in the generated code too

Structure of Response Body The `ResponseBody` class in Retrofit is a generic class that represents the body of an HTTP response. To handle the response properly, you should define a data class that matches the structure of the JSON response you're expecting. For example, if you're fetching user data from an API, your data class might look something like this: ```kotlin data class User( val id: String, val name: String, val email: String ) ```
The actual generated code ```kotlin import retrofit2.Call import retrofit2.http.GET import retrofit2.http.Query interface ApiService { @GET("/api/") fun getUser(@Query("format") format: String): Call } fun main() { val service = Retrofit.Builder() .baseUrl("https://randomuser.me") .addConverterFactory(GsonConverterFactory.create()) .build() .create(ApiService::class.java) // Call your API methods here using service object } ```
Complete Example in Koltin ```kotlin import retrofit2.Call import retrofit2.http.GET import retrofit2.http.Query interface ApiService { @GET("/api/") fun getUser(@Query("format") format: String): Call } fun main() { val service = Retrofit.Builder() .baseUrl("https://randomuser.me") .addConverterFactory(GsonConverterFactory.create()) .build() .create(ApiService::class.java) val call = service.getUser("json") call.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if (response.isSuccessful) { val user = response.body() println("User ID: ${user?.id}, Name: ${user?.name}, Email: ${user?.email}") } else { println("Error: ${response.errorBody()}") } } override fun onFailure(call: Call, t: Throwable) { println("Failed to fetch user: ${t.message}") } }) } ```
animator commented 7 months ago

This works

animator commented 7 months ago

Can you add the remaining test cases.

Photon3009 commented 7 months ago

sorry for the delay, currently going through my exams, will complete this asap, probably within two days

animator commented 7 months ago

No problem. Good luck for the exams.

Photon3009 commented 7 months ago

Thanks @animator I have updated the test cases!

Photon3009 commented 7 months ago

@animator did you check this?

animator commented 7 months ago

As announced in the discord server, we are busy with application reviews and all PR reviews are on hold.

Photon3009 commented 7 months ago

okay!

animator commented 7 months ago

The generated code for POST with form fields are incorrect. Add complete working examples in comment for the below cases:

animator commented 7 months ago

Please raise a new PR after ironing out everything.