use ktor like retrofit.
Add the dependency in your common module's commonMain sourceSet
plugins {
kotlin("multiplatform")
id("com.google.devtools.ksp").version("$ksp_version")
// ...
}
kotlin {
// ...
sourceSets {
dependencies {
api("io.github.qdsfdhvh:ktor-fit-annotation:$ktorfit_version")
}
}
}
dependencies {
// add("kspCommonMainMetadata", "io.github.qdsfdhvh:ktor-fit-ksp:$ktorfit_version")
add("kspJvm", "io.github.qdsfdhvh:ktor-fit-ksp:$ktorfit_version")
// add("kspMacosX64", "io.github.qdsfdhvh:ktor-fit-ksp:$ktorfit_version")
// add("kspIosX64", "io.github.qdsfdhvh:ktor-fit-ksp:$ktorfit_version")
// add("kspJs","io.github.qdsfdhvh:ktor-fit-ksp:$ktorfit_version")
//...
}
Create a kotlin expect class
// @Suppress("NO_ACTUAL_FOR_EXPECT")
@GenerateApi
expect class TestService(client: HttpClient, /* baseUrl: String */) /* : TestOtherApi1, TestOtherApi2 */ {
@GET("get/{id}")
suspend fun getData(@Path("id") id: String, @Query("name") name: String): String
}
// interface TestOtherApi1 {
// @GET("get1/{id}")
// suspend fun getOtherData1(): String
// }
//
// interface TestOtherApi2 {
// @GET("get2/{id}")
// suspend fun getOtherData2(): String
// }
And then create api
val client = HttpClient {
defaultRequest {
url("https://example.api/")
}
}
val api = TestService(client)