All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.aws.sdk.kotlin.services.lexruntimeservice.serde.PutSessionOperationDeserializer.kt Maven / Gradle / Ivy

There is a newer version: 1.3.99
Show newest version
// Code generated by smithy-kotlin-codegen. DO NOT EDIT!

package aws.sdk.kotlin.services.lexruntimeservice.serde

import aws.sdk.kotlin.services.lexruntimeservice.model.DialogState
import aws.sdk.kotlin.services.lexruntimeservice.model.LexRuntimeException
import aws.sdk.kotlin.services.lexruntimeservice.model.MessageFormatType
import aws.sdk.kotlin.services.lexruntimeservice.model.PutSessionResponse
import aws.smithy.kotlin.runtime.awsprotocol.json.RestJsonErrorDeserializer
import aws.smithy.kotlin.runtime.awsprotocol.setAseErrorMetadata
import aws.smithy.kotlin.runtime.awsprotocol.withPayload
import aws.smithy.kotlin.runtime.content.ByteStream
import aws.smithy.kotlin.runtime.http.HttpCall
import aws.smithy.kotlin.runtime.http.isSuccess
import aws.smithy.kotlin.runtime.http.operation.HttpDeserialize
import aws.smithy.kotlin.runtime.http.readAll
import aws.smithy.kotlin.runtime.http.toByteStream
import aws.smithy.kotlin.runtime.operation.ExecutionContext
import aws.smithy.kotlin.runtime.text.encoding.decodeBase64


internal class PutSessionOperationDeserializer: HttpDeserialize {

    override suspend fun deserialize(context: ExecutionContext, call: HttpCall): PutSessionResponse {
        val response = call.response
        if (!response.status.isSuccess()) {
            throwPutSessionError(context, call)
        }
        val builder = PutSessionResponse.Builder()

        builder.activeContexts = response.headers["x-amz-lex-active-contexts"]?.decodeBase64()
        builder.contentType = response.headers["Content-Type"]
        builder.dialogState = response.headers["x-amz-lex-dialog-state"]?.let { DialogState.fromValue(it) }
        builder.encodedMessage = response.headers["x-amz-lex-encoded-message"]
        builder.intentName = response.headers["x-amz-lex-intent-name"]
        builder.message = response.headers["x-amz-lex-message"]
        builder.messageFormat = response.headers["x-amz-lex-message-format"]?.let { MessageFormatType.fromValue(it) }
        builder.sessionAttributes = response.headers["x-amz-lex-session-attributes"]?.decodeBase64()
        builder.sessionId = response.headers["x-amz-lex-session-id"]
        builder.slotToElicit = response.headers["x-amz-lex-slot-to-elicit"]
        builder.slots = response.headers["x-amz-lex-slots"]?.decodeBase64()

        builder.audioStream = response.body.toByteStream()

        builder.correctErrors()
        return builder.build()
    }
}

private suspend fun throwPutSessionError(context: ExecutionContext, call: HttpCall): kotlin.Nothing {
    val payload = call.response.body.readAll()
    val wrappedResponse = call.response.withPayload(payload)
    val wrappedCall = call.copy(response = wrappedResponse)

    val errorDetails = try {
        RestJsonErrorDeserializer.deserialize(call.response.headers, payload)
    } catch (ex: Exception) {
        throw LexRuntimeException("Failed to parse response as 'restJson1' error", ex).also {
            setAseErrorMetadata(it, wrappedCall.response, null)
        }
    }

    val ex = when(errorDetails.code) {
        "BadRequestException" -> BadRequestExceptionDeserializer().deserialize(context, wrappedCall)
        "DependencyFailedException" -> DependencyFailedExceptionDeserializer().deserialize(context, wrappedCall)
        "ConflictException" -> ConflictExceptionDeserializer().deserialize(context, wrappedCall)
        "InternalFailureException" -> InternalFailureExceptionDeserializer().deserialize(context, wrappedCall)
        "LimitExceededException" -> LimitExceededExceptionDeserializer().deserialize(context, wrappedCall)
        "NotAcceptableException" -> NotAcceptableExceptionDeserializer().deserialize(context, wrappedCall)
        "BadGatewayException" -> BadGatewayExceptionDeserializer().deserialize(context, wrappedCall)
        "NotFoundException" -> NotFoundExceptionDeserializer().deserialize(context, wrappedCall)
        else -> LexRuntimeException(errorDetails.message)
    }

    setAseErrorMetadata(ex, wrappedResponse, errorDetails)
    throw ex
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy