CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-client-core-jvm

Ktor HTTP client core library providing asynchronous HTTP client capabilities for Kotlin multiplatform applications

Pending
Overview
Eval results
Files

http-statement.mddocs/

HTTP Statement

Prepared statement functionality for reusable HTTP requests with lazy execution, response handling, and type-safe body parsing. HTTP statements provide a way to prepare requests and execute them multiple times with different execution contexts.

Capabilities

HttpStatement Class

Core statement class for preparing and executing HTTP requests.

/**
 * Prepared HTTP request statement for lazy execution
 * @param builder - Request builder configuration
 * @param client - HTTP client for execution
 */
class HttpStatement(
    val builder: HttpRequestBuilder,
    val client: HttpClient
) {
    /** Execute request and return response */
    suspend fun execute(): HttpResponse
    
    /** Execute request with response handler */
    suspend fun <T> execute(block: suspend (response: HttpResponse) -> T): T
    
    /** Execute request and receive typed body */
    suspend fun <T> body(): T
    
    /** Execute request, receive typed body, and process it */
    suspend fun <T, R> body(block: suspend (response: T) -> R): R
}

Usage Examples:

import io.ktor.client.*
import io.ktor.client.statement.*

val client = HttpClient()

// Create a prepared statement
val statement = client.prepareGet("https://api.example.com/users")

// Execute multiple times
val response1 = statement.execute()
val response2 = statement.execute()

// Execute with handler
val processedData = statement.execute { response ->
    if (response.status.isSuccess()) {
        response.bodyAsText()
    } else {
        "Error: ${response.status}"
    }
}

// Execute and get typed body
val users: List<User> = statement.body()

// Execute, get typed body, and process
val userCount = statement.body<List<User>> { users ->
    users.size
}

Statement Execution

Execute prepared statements with various response handling strategies.

/**
 * Execute request and return response
 * @return HttpResponse for manual processing
 */
suspend fun execute(): HttpResponse

/**
 * Execute request with response handler
 * @param block - Response processing block
 * @return Processed result from block
 */
suspend fun <T> execute(block: suspend (response: HttpResponse) -> T): T

Typed Body Handling

Type-safe response body parsing with optional processing.

/**
 * Execute request and receive typed body
 * @return Deserialized response body
 */
suspend fun <T> body(): T

/**
 * Execute request, receive typed body, and process it
 * @param block - Body processing block
 * @return Processed result from block
 */
suspend fun <T, R> body(block: suspend (response: T) -> R): R

Statement Creation

Extension functions for creating prepared statements from HTTP clients.

// Prepare request methods
suspend fun HttpClient.prepareRequest(
    builder: HttpRequestBuilder
): HttpStatement

suspend fun HttpClient.prepareRequest(
    block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement

suspend fun HttpClient.prepareRequest(
    urlString: String,
    block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement

// HTTP method-specific preparation
suspend fun HttpClient.prepareGet(urlString: String): HttpStatement
suspend fun HttpClient.preparePost(
    urlString: String,
    block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement
suspend fun HttpClient.preparePut(
    urlString: String,
    block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement
suspend fun HttpClient.prepareDelete(urlString: String): HttpStatement
suspend fun HttpClient.prepareHead(urlString: String): HttpStatement
suspend fun HttpClient.prepareOptions(urlString: String): HttpStatement
suspend fun HttpClient.preparePatch(
    urlString: String,
    block: HttpRequestBuilder.() -> Unit = {}
): HttpStatement

Advanced Usage Examples:

import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*

val client = HttpClient()

// Prepare POST request with body
val postStatement = client.preparePost("https://api.example.com/users") {
    header("Content-Type", "application/json")
    setBody("""{"name": "John", "email": "john@example.com"}""")
}

// Execute with error handling
val result = postStatement.execute { response ->
    when {
        response.status.isSuccess() -> {
            val user: User = response.body()
            "Created user: ${user.name}"
        }
        response.status.value == 409 -> {
            "User already exists"
        }
        else -> {
            "Error: ${response.status.description}"
        }
    }
}

// Reuse statement with different processing
val userId = postStatement.body<User> { user -> user.id }
val userName = postStatement.body<User> { user -> user.name }

Benefits of HTTP Statements

  • Reusability: Prepare once, execute multiple times
  • Lazy Execution: Request is only sent when execute() is called
  • Type Safety: Generic type parameters ensure type-safe response handling
  • Memory Efficiency: Avoids recreating request builders for repeated requests
  • Error Handling: Centralized error handling in execution blocks
  • Flexible Processing: Multiple ways to handle responses based on use case

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-client-core-jvm

docs

client-configuration.md

content-handling.md

engine-architecture.md

events-monitoring.md

http-statement.md

index.md

plugin-system.md

request-building.md

response-handling.md

websocket-support.md

tile.json