Skip to content

qdsfdhvh/ktor-fit

Repository files navigation

ktor-fit

Maven Central

use ktor like retrofit.

Setup

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")
    //...
}

How to Use

1.Only Ksp

You can create a kotlin expect class, ksp will generate the actual class.

@Suppress("NO_ACTUAL_FOR_EXPECT")
@GenerateApi
expect class TestService(client: HttpClient) : 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)

2. Ksp and Kcp

This way is still experimental:

plugins {
    id("io.github.qdsfdhvh.ktor-fit-plugin") version $ktorfit_version
}
@GenerateApi
interface TestService {
    //...
}

ksp will auto generate code like:

class _TestServiceImpl(private val client: HttpClient) : TestService {
    //...
}

and kcp will auto generate code like:

interface TestService {
    //...
    companion object {
        fun create(client: HttpClient): TestService {
            return _TestServiceImpl(client)
        }
    }
}

so, you can use like this:

val client = HttpClient {
  defaultRequest {
    url("https://example.api/")
  }
}
val api = TestService.create(client)

however, at the moment you need the ktor-fit-extensions plugin to get the IDE to prompt for the create(client) function.

image

Thx

Ktorfit

About

use ktor like retrofit.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages