commonMain.aws.smithy.kotlin.runtime.awsprotocol.eventstream.ResponseHeaders.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-event-stream-jvm Show documentation
Show all versions of aws-event-stream-jvm Show documentation
Support for the vnd.amazon.event-stream content type
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.smithy.kotlin.runtime.awsprotocol.eventstream
import aws.smithy.kotlin.runtime.InternalApi
/**
* Parse the protocol level headers into a concrete [MessageType]
*/
@InternalApi
public fun Message.type(): MessageType {
val headersByName = headers.associateBy { it.name }
val messageType: String = checkNotNull(headersByName[":message-type"]) { "`:message-type` header is required to deserialize an event stream message" }.value.expectString()
val eventType = headersByName[":event-type"]?.value?.expectString()
val exceptionType = headersByName[":exception-type"]?.value?.expectString()
val contentType = headersByName[":content-type"]?.value?.expectString()
return when (messageType) {
"event" -> MessageType.Event(
checkNotNull(eventType) { "Invalid `event` message: `:event-type` header is missing" },
contentType,
)
"exception" -> MessageType.Exception(
checkNotNull(exceptionType) { "Invalid `exception` message: `:exception-type` header is missing" },
contentType,
)
"error" -> {
val errorCode = headersByName[":error-code"]?.value?.expectString() ?: error("Invalid `error` message: `:error-code` header is missing")
val errorMessage = headersByName[":error-message"]?.value?.expectString()
MessageType.Error(errorCode, errorMessage)
}
else -> MessageType.SdkUnknown(messageType)
}
}
/**
* Common framework message information parsed from headers
*/
@InternalApi
public sealed class MessageType {
/**
* Corresponds to the `event` message type. All events include the headers:
*
* * `:message-type`: Always set to `event`
* * `:event-type`: (Required) Identifies the event shape from the event stream union. This is the member name from the union.
* * `:content-type`: (Optional) The content type for the payload
*
* ### Example message
*
* ```
* :message-type: event
* :event-type: MyStruct
* :content-type: application/json
*
* {...}
* ```
* @param shapeType the event type as identified by the `:event-type` header.
* @param contentType the content type of the payload (if present)
*/
@InternalApi
public data class Event(val shapeType: String, val contentType: String? = null) : MessageType()
/**
* Corresponds to the `exception` message type.
* NOTE: Exceptions are mapped directly to the payload. There is no way to map event headers for exceptions.
*
* ### Example message
*
* ```
* :message-type: exception
* :exception-type: FooException
* :content-type: application/json
*
* {...}
* ```
*
* @param shapeType the exception type as identified by the `:exception-type` header.
* @param contentType the content type of the payload (if present)
*/
@InternalApi
public data class Exception(val shapeType: String, val contentType: String? = null) : MessageType()
/**
* Corresponds to the `error` message type.
* Errors are like exceptions, but they are un-modeled and have a fixed set of fields:
* * `:message-type`: Always set to `error`
* * `:error-code`: (Required) UTF-8 string containing name, type, or category of the error.
* * `:error-message`: (Optional) UTF-* string containing an error message
*
* ### Example message
*
* ```
* :message-type: error
* :error-code: InternalServerError
* :error-message: An error occurred
* ```
*/
@InternalApi
public data class Error(val errorCode: String, val message: String? = null) : MessageType()
/**
* Catch all for unknown message types outside of `event`, `exception`, or `error`
*/
@InternalApi
public data class SdkUnknown(val messageType: String) : MessageType()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy