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

commonMain.aws.sdk.kotlin.services.lexruntimev2.transform.StartConversationOperationDeserializer.kt Maven / Gradle / Ivy

// Code generated by smithy-kotlin-codegen. DO NOT EDIT!

package aws.sdk.kotlin.services.lexruntimev2.transform

import aws.sdk.kotlin.runtime.ClientException
import aws.sdk.kotlin.services.lexruntimev2.model.LexRuntimeV2Exception
import aws.sdk.kotlin.services.lexruntimev2.model.StartConversationResponse
import aws.sdk.kotlin.services.lexruntimev2.model.StartConversationResponseEventStream
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.MessageType
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.decodeFrames
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.type
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.http.HttpBody
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.response.HttpResponse
import aws.smithy.kotlin.runtime.http.toSdkByteReadChannel
import aws.smithy.kotlin.runtime.operation.ExecutionContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map


internal class StartConversationOperationDeserializer: HttpDeserialize {

    override suspend fun deserialize(context: ExecutionContext, response: HttpResponse): StartConversationResponse {
        if (!response.status.isSuccess()) {
            throwStartConversationError(context, response)
        }
        val builder = StartConversationResponse.Builder()

        deserializeStartConversationOperationBody(builder, response.body)
        return builder.build()
    }
}

private suspend fun throwStartConversationError(context: ExecutionContext, response: HttpResponse): kotlin.Nothing {
    val payload = response.body.readAll()
    val wrappedResponse = response.withPayload(payload)

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

    val ex = when(errorDetails.code) {
        "AccessDeniedException" -> AccessDeniedExceptionDeserializer().deserialize(context, wrappedResponse)
        "InternalServerException" -> InternalServerExceptionDeserializer().deserialize(context, wrappedResponse)
        "ThrottlingException" -> ThrottlingExceptionDeserializer().deserialize(context, wrappedResponse)
        "ValidationException" -> ValidationExceptionDeserializer().deserialize(context, wrappedResponse)
        else -> LexRuntimeV2Exception(errorDetails.message)
    }

    setAseErrorMetadata(ex, wrappedResponse, errorDetails)
    throw ex
}

internal suspend fun deserializeStartConversationOperationBody(builder: StartConversationResponse.Builder, body: HttpBody) {
    val chan = body.toSdkByteReadChannel() ?: return
    val events = decodeFrames(chan)
        .map { message ->
            when(val mt = message.type()) {
                is MessageType.Event -> when(mt.shapeType) {
                    "PlaybackInterruptionEvent" -> {
                        val e = deserializePlaybackInterruptionEventPayload(message.payload)
                        StartConversationResponseEventStream.PlaybackInterruptionEvent(e)
                    }
                    "TranscriptEvent" -> {
                        val e = deserializeTranscriptEventPayload(message.payload)
                        StartConversationResponseEventStream.TranscriptEvent(e)
                    }
                    "IntentResultEvent" -> {
                        val e = deserializeIntentResultEventPayload(message.payload)
                        StartConversationResponseEventStream.IntentResultEvent(e)
                    }
                    "TextResponseEvent" -> {
                        val e = deserializeTextResponseEventPayload(message.payload)
                        StartConversationResponseEventStream.TextResponseEvent(e)
                    }
                    "AudioResponseEvent" -> {
                        val e = deserializeAudioResponseEventPayload(message.payload)
                        StartConversationResponseEventStream.AudioResponseEvent(e)
                    }
                    "HeartbeatEvent" -> {
                        val e = deserializeHeartbeatEventPayload(message.payload)
                        StartConversationResponseEventStream.HeartbeatEvent(e)
                    }
                    else -> StartConversationResponseEventStream.SdkUnknown
                }
                is MessageType.Exception -> when(mt.shapeType){
                    "AccessDeniedException" -> {
                        val err = deserializeAccessDeniedExceptionPayload(message.payload)
                        throw err
                    }
                    "ResourceNotFoundException" -> {
                        val err = deserializeResourceNotFoundExceptionPayload(message.payload)
                        throw err
                    }
                    "ValidationException" -> {
                        val err = deserializeValidationExceptionPayload(message.payload)
                        throw err
                    }
                    "ThrottlingException" -> {
                        val err = deserializeThrottlingExceptionPayload(message.payload)
                        throw err
                    }
                    "InternalServerException" -> {
                        val err = deserializeInternalServerExceptionPayload(message.payload)
                        throw err
                    }
                    "ConflictException" -> {
                        val err = deserializeConflictExceptionPayload(message.payload)
                        throw err
                    }
                    "DependencyFailedException" -> {
                        val err = deserializeDependencyFailedExceptionPayload(message.payload)
                        throw err
                    }
                    "BadGatewayException" -> {
                        val err = deserializeBadGatewayExceptionPayload(message.payload)
                        throw err
                    }
                    else -> throw LexRuntimeV2Exception("error processing event stream, unrecognized errorType: ${mt.shapeType}")
                }
                is MessageType.Error -> throw LexRuntimeV2Exception("error processing event stream: errorCode=${mt.errorCode}; message=${mt.message}")
                is MessageType.SdkUnknown -> throw ClientException("unrecognized event stream message `:message-type`: ${mt.messageType}")
            }
        }
    builder.responseEventStream = events
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy