
Lightweight wrapper around Ktor Client offering a clean, testable networking layer with buildClient API, JSON serialization, base‑URL and timeout handling, offline detection, and lambda interceptors.
A lightweight Kotlin Multiplatform (KMP) wrapper around Ktor Client, designed to provide a clean, unified, and testable networking layer for Android, iOS, and JVM.
This library simplifies:
buildClient()
NetworkInterceptor)HttpClientInterceptor (λ-based)buildClient() APIcommonMain)commonMain {
dependencies {
implementation("com.kdani.ktor.helper:ktor-helper")
}
}class ApiClient {
private val client = KtorHelper.buildClient(
baseUrl = "https://api.example.com",
timeoutInMillis = 10_000,
interceptors = listOf(
authInterceptor,
)
)
}val authInterceptor: HttpClientInterceptor = {
defaultRequest {
val token = tokenProvider()
if (token != null) {
headers.append("Authorization", "Bearer $token")
}
}
}Usage:
buildClient(
baseUrl = "https://api.example.com",
interceptors = listOf(authInterceptor)
)val loggingInterceptor: HttpClientInterceptor = {
install(Logging) {
level = LogLevel.ALL
}
}suspend fun fetchUser(id: String): NetworkResponse<UserDto> =
client.safeGet<UserDto>("/users/$id")suspend fun createUser(name: String): NetworkResponse<UserDto> =
client.safePost<UserDto>(
urlString = "/users",
body = CreateUserRequest(name)
)@kotlinx.serialization.Serializable
data class UserDto(
val id: String,
val name: String,
val email: String
)A lightweight Kotlin Multiplatform (KMP) wrapper around Ktor Client, designed to provide a clean, unified, and testable networking layer for Android, iOS, and JVM.
This library simplifies:
buildClient()
NetworkInterceptor)HttpClientInterceptor (λ-based)buildClient() APIcommonMain)commonMain {
dependencies {
implementation("com.kdani.ktor.helper:ktor-helper")
}
}class ApiClient {
private val client = KtorHelper.buildClient(
baseUrl = "https://api.example.com",
timeoutInMillis = 10_000,
interceptors = listOf(
authInterceptor,
)
)
}val authInterceptor: HttpClientInterceptor = {
defaultRequest {
val token = tokenProvider()
if (token != null) {
headers.append("Authorization", "Bearer $token")
}
}
}Usage:
buildClient(
baseUrl = "https://api.example.com",
interceptors = listOf(authInterceptor)
)val loggingInterceptor: HttpClientInterceptor = {
install(Logging) {
level = LogLevel.ALL
}
}suspend fun fetchUser(id: String): NetworkResponse<UserDto> =
client.safeGet<UserDto>("/users/$id")suspend fun createUser(name: String): NetworkResponse<UserDto> =
client.safePost<UserDto>(
urlString = "/users",
body = CreateUserRequest(name)
)@kotlinx.serialization.Serializable
data class UserDto(
val id: String,
val name: String,
val email: String
)