1. Why “network in the CLI” turns into a problem
When you write your first network program, it’s really tempting to do everything right in main: read a command, immediately do client.get(...), immediately parse JSON, immediately println. In a tiny example this can even look “fine”, like instant noodles: pour boiling water over it—and it’s ready. The problem is that after a couple of new commands that noodle mess starts living its own life and taking revenge for the shortcuts.
Let’s imagine we already have a console app from previous days—a hypothetical expense tracker. Suppose it can store expenses locally, and today we want to add the tip command that fetches a “financial tip of the day” from a remote server. Sounds harmless—right up until you try to do it “directly in the CLI”.
Network in the CLI usually leads to three problems at once.
First, responsibilities get mixed: in one place you have command parsing, HTTP headers, JSON models, and error handling. Second, it’s unclear what to test: the “command” or the “network”? Third, any change (for example, a different URL or a new response format) forces you to dig into the “sacred main”, which is already scary to touch because it, like an ancient artifact, is held together by sheer luck.
To get out of this, we’ll split the program into two new layers:
- Client layer — “how we go to the network”.
- Scenario layer — “why we go to the network and what we do with the result”.
And the CLI will remain what it should be: “read a command → call a scenario → print the result”.
2. Layer diagram: who is responsible for what
When people say “architecture”, beginners often hear: “now there will be 43 folders, 12 abbreviations, and one shaman tambourine”. We won’t do that. We need the simplest learning-oriented scheme that actually helps: the CLI doesn’t know about Ktor and HTTP details, the scenario doesn’t print to the console, and the client doesn’t decide what to tell the user.
Here’s a visual model (as a flowchart) that we’ll keep coming back to:
flowchart TD
CLI["CLI (main): reads commands, prints text"]
SC["Scenario: decides what to do with the result"]
API["Client API: interface of the remote service"]
KTOR["Ktor implementation: HttpClient + requests"]
NET["Internet/Server"]
CLI --> SC --> API --> KTOR --> NET
KTOR --> API --> SC --> CLI
The idea is simple: the lower the layer, the less it “knows” about your program and the more it “knows” about the technical side. That’s why the client knows about HTTP and JSON, but doesn’t know you have a tip command. The scenario knows what “tip of the day” is, but doesn’t know what headers the request has. The CLI knows the user typed tip, but doesn’t know what HttpResponse is.
It’s useful to keep a small table in mind:
| Layer | What it does | What it does NOT do |
|---|---|---|
| CLI | reads commands, prints messages | doesn’t build HTTP requests, doesn’t parse JSON |
| Scenario | decides “what to return to the user” | doesn’t print, doesn’t manage HttpClient |
| Network Client | goes to the network, returns data/errors | doesn’t know about commands and UI text |
This setup works well with Kotlin code and familiar code organization rules: keep related things close together, don’t sort methods alphabetically, and go “top to bottom by meaning”—this matches the general recommendations in common style conventions.
3. Network layer contract: interface and explicit result
The first practical step is agreeing on what exactly the network layer returns. This is a key point: if the client returns a “bare HttpResponse”, you drag HTTP details upward. If the client does println, you lose control over output. So the most beginner-friendly option is to return an explicit result, where success and errors are separated by types.
Explicit request result: ApiResult
Let’s start with a simple sealed class that can distinguish three cases:
- success (data received),
- HTTP error (there is a response, but non-2xx),
- network error (exception, timeout, unavailability, etc.).
sealed class ApiResult<out T> {
data class Ok<T>(val value: T) : ApiResult<T>()
data class HttpError(val status: Int, val body: String) : ApiResult<Nothing>()
data class NetworkError(val message: String) : ApiResult<Nothing>()
}
Response model and remote service interface
Now let’s define the response model for our tip command. Suppose the server returns JSON like:
{ "text": "Save 10% of your income right after payday" }
Then the Kotlin model:
import kotlinx.serialization.Serializable
@Serializable
data class TipResponse(val text: String)
And here is the main element of the architecture—the interface that describes the capabilities of the remote service:
interface TipsApi {
suspend fun fetchDailyTip(): ApiResult<TipResponse>
}
Notice two things.
First, it’s suspend—our network is asynchronous, and we’re already used to that from coroutines.
Second, the CLI is no longer required to know what Ktor, HttpResponse are and how headers work. The CLI only knows: “there is TipsApi, and you can ask it for the tip of the day”.
This approach matches the idea of a “narrow contract”: you describe what the program needs, not how you will obtain it (Ktor today, a different client tomorrow). Ktor fits this model perfectly: it can be an implementation, not the meaning of the program.
4. Ktor-based client implementation: technical details live here
Now let’s implement TipsApi using Ktor. This is exactly where all the “technical stuff” belongs: HttpClient, URL, status checking, reading the body, JSON deserialization, exception handling.
HttpClient factory
First, a client factory (so we create it in one place and configure it consistently):
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
fun buildHttpClient(): HttpClient =
HttpClient(CIO) {
install(HttpTimeout) { requestTimeoutMillis = 3_000 }
install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true }) }
}
TipsApi implementation via Ktor
Now the API implementation. We’ll intentionally stick to the style “explicitly got HttpResponse → explicitly read the body”, so the code stays readable:
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
class KtorTipsApi(
private val client: HttpClient,
private val baseUrl: String
) : TipsApi {
override suspend fun fetchDailyTip(): ApiResult<TipResponse> {
return try {
val response: HttpResponse = client.get("$baseUrl/tip")
if (response.status.value in 200..299) ApiResult.Ok(response.body())
else ApiResult.HttpError(response.status.value, response.bodyAsText())
} catch (e: Exception) {
ApiResult.NetworkError(e.message ?: "Network error")
}
}
}
It’s important to feel the responsibility boundary here. This class:
- builds the URL,
- makes the request,
- checks the status,
- returns an understandable ApiResult.
But it doesn’t print and doesn’t decide what to say to the user. It just honestly returns the result.
5. Scenarios and CLI: each does its own job
If the client returns ApiResult, we need a place where this result turns into an “understandable human answer”. That place is the scenario. It lives “above” the network, but “below” the CLI.
Scenario: translating ApiResult into meaning
A scenario is great because it’s convenient to keep rules like: “if HTTP 404—say one thing, if timeout—say another, if success—show the tip text”.
Let’s make a scenario that returns a ready-to-print string for the CLI:
class GetDailyTipScenario(private val api: TipsApi) {
suspend fun execute(): String {
return when (val r = api.fetchDailyTip()) {
is ApiResult.Ok -> "Tip of the day: ${r.value.text}"
is ApiResult.HttpError -> "Tips service returned HTTP ${r.status}"
is ApiResult.NetworkError -> "Failed to get a tip: ${r.message}"
}
}
}
This code looks “too simple”, and that’s the point. The scenario is where the program becomes a program, not a collection of protocols.
Notice another small architectural detail: the scenario depends on the interface TipsApi, not on KtorTipsApi. This allows us to swap implementations (for example, with a “stub” for development) without changing the scenario and CLI.
CLI: command → scenario → output
Now the nicest part: main() doesn’t have to deal with HTTP, JSON, and status codes. It just manages input/output and the lifecycle of resources.
Let’s make a mini-CLI with the commands tip and exit. For simplicity, we’ll keep the command-reading loop the way you’ve done it many times before:
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val client = buildHttpClient()
try {
val api: TipsApi = KtorTipsApi(client, baseUrl = "https://example.com")
val scenario = GetDailyTipScenario(api)
while (true) {
print("> ")
when (readln().trim()) {
"tip" -> println(scenario.execute())
"exit" -> return@runBlocking
else -> println("Commands: tip, exit") // Commands: tip, exit
}
}
} finally {
client.close()
}
}
Notice: main() now reads like an instruction, not like an article titled “How the internet works”.
And yes, try/finally around client.close() is not paranoia—it’s a grown-up habit. Networking likes resources, and resources like being closed.
6. Dependency composition: assemble the app in one place
When an app grows, a desire appears to “create everything wherever”: HttpClient inside the API, API inside the scenario, scenario inside the command handler... and suddenly you have 17 clients and 17 timeouts because “it creates itself”.
To avoid that, we use one simple trick: compose dependencies in one place (sometimes this is called the “composition root”). In a learning project it can be a separate function or a small class.
For example, let’s create AppContext that creates and holds everything we need:
import io.ktor.client.HttpClient
class AppContext(baseUrl: String) : AutoCloseable {
private val client: HttpClient = buildHttpClient()
val tipsApi: TipsApi = KtorTipsApi(client, baseUrl)
val getDailyTipScenario = GetDailyTipScenario(tipsApi)
override fun close() = client.close()
}
And then main() becomes even cleaner:
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
AppContext(baseUrl = "https://example.com").use { ctx ->
println(ctx.getDailyTipScenario.execute())
// Tip of the day: ...
}
}
Here we use the same principle you’ve already seen in the I/O topics: a resource is created in one place and closed there as well. HttpClient has close(), and use conveniently wraps the lifecycle.
7. Extension without pain: a new endpoint and minimal edits
Architecture is considered successful not when “everything is nicely arranged into folders”, but when a new feature is added locally. Let’s test that.
Imagine that tomorrow you want the ping command to quickly check whether the service is alive (or just to practice). With bad architecture you go into main() and start adding requests there. With ours—you add a method to the API, an implementation in the Ktor client, and a scenario. You touch the CLI minimally.
Update the contract
Let’s add to the contract:
interface TipsApi {
suspend fun fetchDailyTip(): ApiResult<TipResponse>
suspend fun ping(): ApiResult<String>
}
Add the implementation in the Ktor client
Implementation (briefly, like in the text):
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
override suspend fun ping(): ApiResult<String> {
return try {
val response = client.get("$baseUrl/ping")
if (response.status.value in 200..299) ApiResult.Ok(response.bodyAsText())
else ApiResult.HttpError(response.status.value, response.bodyAsText())
} catch (e: Exception) {
ApiResult.NetworkError(e.message ?: "Network error")
}
}
Add a scenario
Scenario:
class PingScenario(private val api: TipsApi) {
suspend fun execute(): String =
when (val r = api.ping()) {
is ApiResult.Ok -> "ping: ${r.value}"
is ApiResult.HttpError -> "ping: HTTP ${r.status}"
is ApiResult.NetworkError -> "ping: ${r.message}"
}
}
And only at the very end you add one branch in the CLI. That is the main sign that layers work: changes don’t “spread everywhere”.
8. Typical mistakes
Mistake #1: creating HttpClient inside every request function.
People often do this “to keep it simple”: create an HttpClient inside fetchDailyTip(), call get, then close it. In practice you get extra overhead, it’s harder to control timeouts/plugins, and at some point you start forgetting to close the client in one of the branches. It’s better to create the client once (in main or AppContext) and pass it into the API implementation.
Mistake #2: returning HttpResponse outward and “let them deal with it”.
This looks like saving effort, but in reality you pull HTTP details upward, and then status/bodyAsText() pop up in scenarios and the CLI. That’s exactly what we were trying to avoid. It’s better to return ApiResult<T> or another explicit result, so upper layers work with meaningful types rather than transport details.
Mistake #3: doing println inside the network client.
It’s very tempting to “help yourself debug”: print the URL, status, body. But then those prints end up in user output and get in the way. If you need logging, that’s a separate topic and a separate responsibility. At the current level, keep the rule: the client returns data/errors, and only the CLI prints.
Mistake #4: trying to parse body<T>() for any status code.
If the server returned 400/500, the body may be not the JSON you expect (or not JSON at all). Then you’ll get a deserialization exception and lose the meaning of the error. Keep discipline: check the status first, and only in the success branch read body<T>(), and in the error branch read bodyAsText().
Mistake #5: “catching everything” and turning it into one text like “something went wrong”.
When you do catch (Exception) and return the same phrase, you deprive yourself (and the user) of understanding: did the internet go down, was it a timeout, a wrong URL, or did the server honestly say “your data is invalid”. Even if you aren’t doing fine-grained classification yet, separate at least HTTP errors from network exceptions—this already sharply increases the program’s predictability.
Mistake #6: mixing scenario and CLI: the scenario starts calling readln() or printing.
Sometimes it feels convenient: “but the scenario knows what to ask the user”. But then the scenario stops being a scenario layer and turns into a second main(). Keep the boundary: input/output lives in the CLI, the scenario lives in the logic, the client lives in the network. It’s boring, but boring means reliable (in a good sense).
GO TO FULL VERSION