2. Getting Started¶
Dependency¶
Pulls in kotlinx-coroutines-jdk8 and jackson-module-kotlin (data class DTO serialization) as
transitive dependencies.
First Request¶
zlinkHttpClient("http://127.0.0.1:18080").use { client ->
val player = client.get("/players/7281").await<PlayerProfile>()
println(player.body().name)
}
zlinkHttpClient(baseUrl) { ... }applies the DSL block to a fluent builder to create the client.- The client is reusable and thread-safe. Since it's
AutoCloseable, managing its lifetime withuse { ... }cleans up the internal connection pool and executor. await<T>()is a reified extension that suspends all the way to a typed JSON response.
DTO¶
data classes are used directly as request/response bodies.
data class CreateGameReq(val mode: String)
data class CreateGameRes(val id: String, val ranked: Boolean)
One-Line Request¶
suspend fun createGame(): CreateGameRes =
zlinkHttpClient("https://game-api.example.internal").use { client ->
client.post("/games").body(CreateGameReq("ranked-match-0611")).fetch()
}
fetch<T>() is a suspend extension that directly returns the typed body (a convenience for
await<T>().body()).
Test · CLI¶
Outside a coroutine (tests, main), wrap the call in runBlocking.