HTTP core functionality for AWS service clients in the AWS SDK for Kotlin, providing user agent management, retry policies, and HTTP middleware.
—
Request and response processing middleware components that handle cross-cutting concerns like user agent injection, retry headers, and recursion detection in the AWS SDK HTTP pipeline.
Middleware that sets User-Agent and x-amz-user-agent headers on outgoing requests.
/**
* HTTP middleware that sets User-Agent and x-amz-user-agent headers
*/
class UserAgent(private val staticMetadata: AwsUserAgentMetadata) : ModifyRequestMiddleware {
/**
* Install the middleware on an HTTP operation
* @param op The HTTP operation to install middleware on
*/
override fun install(op: SdkHttpOperation<*, *>)
/**
* Modify the request to add user agent headers
* @param req The HTTP request to modify
* @return Modified request with user agent headers
*/
override suspend fun modifyRequest(req: SdkHttpRequest): SdkHttpRequest
}Usage Examples:
import aws.sdk.kotlin.runtime.http.middleware.UserAgent
import aws.sdk.kotlin.runtime.http.AwsUserAgentMetadata
import aws.sdk.kotlin.runtime.http.ApiMetadata
// Create user agent metadata
val apiMetadata = ApiMetadata(serviceId = "DynamoDB", version = "2020-01-01")
val userAgentMetadata = AwsUserAgentMetadata.fromEnvironment(apiMetadata)
// Create and install middleware
val userAgentMiddleware = UserAgent(userAgentMetadata)
userAgentMiddleware.install(httpOperation)
// The middleware will automatically add headers:
// User-Agent: aws-sdk-kotlin/1.5.33 Linux/5.15.0 Kotlin/1.9.0 md/DynamoDB-2020-01-01
// x-amz-user-agent: aws-sdk-kotlin/1.5.33 api/DynamoDB-2020-01-01 os/linux lang/kotlin#1.9.0Middleware that adds AWS-specific retry headers to requests for tracking retry attempts.
/**
* Middleware that adds AWS-specific retry headers to requests
*/
class AwsRetryHeaderMiddleware : MutateMiddleware {
/**
* Install the middleware on an HTTP operation
* @param op The HTTP operation to install middleware on
*/
override fun install(op: SdkHttpOperation<*, *>)
/**
* Handle the operation request and add retry headers
* @param request The operation request to process
* @param handler The next handler in the chain
* @return The operation response
*/
suspend fun handle(
request: OperationRequest,
handler: Handler<OperationRequest, OperationResponse>
): OperationResponse
}Usage Examples:
import aws.sdk.kotlin.runtime.http.middleware.AwsRetryHeaderMiddleware
// Create and install retry header middleware
val retryHeaderMiddleware = AwsRetryHeaderMiddleware()
retryHeaderMiddleware.install(httpOperation)
// The middleware will add headers like:
// amz-sdk-invocation-id: unique-request-id
// amz-sdk-request: attempt=1; max=3Middleware that adds recursion detection headers to prevent infinite loops in AWS service calls.
/**
* HTTP middleware to add recursion detection header where required
*/
class RecursionDetection(env: EnvironmentProvider = PlatformProvider.System) : ModifyRequestMiddleware {
/**
* Install the middleware on an HTTP operation
* @param op The HTTP operation to install middleware on
*/
override fun install(op: SdkHttpOperation<*, *>)
/**
* Modify the request to add recursion detection header
* @param req The HTTP request to modify
* @return Modified request with trace header
*/
override suspend fun modifyRequest(req: SdkHttpRequest): SdkHttpRequest
}Usage Examples:
import aws.sdk.kotlin.runtime.http.middleware.RecursionDetection
import aws.smithy.kotlin.runtime.util.PlatformProvider
// Create with default environment provider
val recursionDetection = RecursionDetection()
// Create with custom environment provider
val customEnvProvider = // ... your environment provider
val customRecursionDetection = RecursionDetection(customEnvProvider)
// Modify request to add trace header
val modifiedRequest = recursionDetection.modifyRequest(originalRequest)
// The middleware adds header:
// X-Amzn-Trace-Id: Root=1-trace-id-from-environmentMiddleware components work together in a processing pipeline:
import aws.smithy.kotlin.runtime.http.operation.SdkHttpOperation
// Typical middleware installation order
val operation: SdkHttpOperation<Request, Response> = // ... create operation
// Install middleware in order
RecursionDetection().install(operation)
UserAgent(userAgentMetadata).install(operation)
AwsRetryHeaderMiddleware().install(operation)
// Execute operation with all middleware applied
val response = operation.execute(request)Middleware components integrate with the environment and platform:
Middleware handles various error scenarios:
Middleware components ensure secure handling of:
Install with Tessl CLI
npx tessl i tessl/maven-aws-sdk-kotlin--aws-http-jvm