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

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

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

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

import aws.sdk.kotlin.services.lexruntimev2.model.LexRuntimeV2Exception
import aws.sdk.kotlin.services.lexruntimev2.model.RecognizeUtteranceResponse
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.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.toByteStream
import aws.smithy.kotlin.runtime.operation.ExecutionContext


internal class RecognizeUtteranceOperationDeserializer: HttpDeserialize {

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

        builder.contentType = response.headers["Content-Type"]
        builder.inputMode = response.headers["x-amz-lex-input-mode"]
        builder.inputTranscript = response.headers["x-amz-lex-input-transcript"]
        builder.interpretations = response.headers["x-amz-lex-interpretations"]
        builder.messages = response.headers["x-amz-lex-messages"]
        builder.recognizedBotMember = response.headers["x-amz-lex-recognized-bot-member"]
        builder.requestAttributes = response.headers["x-amz-lex-request-attributes"]
        builder.sessionId = response.headers["x-amz-lex-session-id"]
        builder.sessionState = response.headers["x-amz-lex-session-state"]

        builder.audioStream = response.body.toByteStream()

        return builder.build()
    }
}

private suspend fun throwRecognizeUtteranceError(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)
        "BadGatewayException" -> BadGatewayExceptionDeserializer().deserialize(context, wrappedResponse)
        "ConflictException" -> ConflictExceptionDeserializer().deserialize(context, wrappedResponse)
        "DependencyFailedException" -> DependencyFailedExceptionDeserializer().deserialize(context, wrappedResponse)
        "InternalServerException" -> InternalServerExceptionDeserializer().deserialize(context, wrappedResponse)
        "ResourceNotFoundException" -> ResourceNotFoundExceptionDeserializer().deserialize(context, wrappedResponse)
        "ThrottlingException" -> ThrottlingExceptionDeserializer().deserialize(context, wrappedResponse)
        "ValidationException" -> ValidationExceptionDeserializer().deserialize(context, wrappedResponse)
        else -> LexRuntimeV2Exception(errorDetails.message)
    }

    setAseErrorMetadata(ex, wrappedResponse, errorDetails)
    throw ex
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy