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

commonMain.aws.sdk.kotlin.services.transcribestreaming.serde.StartCallAnalyticsStreamTranscriptionOperationDeserializer.kt Maven / Gradle / Ivy

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

package aws.sdk.kotlin.services.transcribestreaming.serde

import aws.sdk.kotlin.services.transcribestreaming.model.CallAnalyticsLanguageCode
import aws.sdk.kotlin.services.transcribestreaming.model.CallAnalyticsTranscriptResultStream
import aws.sdk.kotlin.services.transcribestreaming.model.ContentIdentificationType
import aws.sdk.kotlin.services.transcribestreaming.model.ContentRedactionType
import aws.sdk.kotlin.services.transcribestreaming.model.MediaEncoding
import aws.sdk.kotlin.services.transcribestreaming.model.PartialResultsStability
import aws.sdk.kotlin.services.transcribestreaming.model.StartCallAnalyticsStreamTranscriptionResponse
import aws.sdk.kotlin.services.transcribestreaming.model.TranscribeStreamingException
import aws.sdk.kotlin.services.transcribestreaming.model.VocabularyFilterMethod
import aws.smithy.kotlin.runtime.ClientException
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.HttpCall
import aws.smithy.kotlin.runtime.http.isSuccess
import aws.smithy.kotlin.runtime.http.operation.HttpDeserializer
import aws.smithy.kotlin.runtime.http.readAll
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 StartCallAnalyticsStreamTranscriptionOperationDeserializer: HttpDeserializer.Streaming {

    override suspend fun deserialize(context: ExecutionContext, call: HttpCall): StartCallAnalyticsStreamTranscriptionResponse {
        val response = call.response
        if (!response.status.isSuccess()) {
            val payload = response.body.readAll()
            throwStartCallAnalyticsStreamTranscriptionError(context, call, payload)
        }
        val builder = StartCallAnalyticsStreamTranscriptionResponse.Builder()

        builder.contentIdentificationType = response.headers["x-amzn-transcribe-content-identification-type"]?.let { ContentIdentificationType.fromValue(it) }
        builder.contentRedactionType = response.headers["x-amzn-transcribe-content-redaction-type"]?.let { ContentRedactionType.fromValue(it) }
        builder.enablePartialResultsStabilization = response.headers["x-amzn-transcribe-enable-partial-results-stabilization"]?.toBoolean() ?: false
        builder.languageCode = response.headers["x-amzn-transcribe-language-code"]?.let { CallAnalyticsLanguageCode.fromValue(it) }
        builder.languageModelName = response.headers["x-amzn-transcribe-language-model-name"]
        builder.mediaEncoding = response.headers["x-amzn-transcribe-media-encoding"]?.let { MediaEncoding.fromValue(it) }
        builder.mediaSampleRateHertz = response.headers["x-amzn-transcribe-sample-rate"]?.toInt()
        builder.partialResultsStability = response.headers["x-amzn-transcribe-partial-results-stability"]?.let { PartialResultsStability.fromValue(it) }
        builder.piiEntityTypes = response.headers["x-amzn-transcribe-pii-entity-types"]
        builder.requestId = response.headers["x-amzn-request-id"]
        builder.sessionId = response.headers["x-amzn-transcribe-session-id"]
        builder.vocabularyFilterMethod = response.headers["x-amzn-transcribe-vocabulary-filter-method"]?.let { VocabularyFilterMethod.fromValue(it) }
        builder.vocabularyFilterName = response.headers["x-amzn-transcribe-vocabulary-filter-name"]
        builder.vocabularyName = response.headers["x-amzn-transcribe-vocabulary-name"]

        deserializeStartCallAnalyticsStreamTranscriptionOperationBody(builder, call)
        builder.correctErrors()
        return builder.build()
    }
}

private fun throwStartCallAnalyticsStreamTranscriptionError(context: ExecutionContext, call: HttpCall, payload: ByteArray?): kotlin.Nothing {
    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 TranscribeStreamingException("Failed to parse response as 'restJson1' error", ex).also {
            setAseErrorMetadata(it, wrappedCall.response, null)
        }
    }

    val ex = when(errorDetails.code) {
        "ServiceUnavailableException" -> ServiceUnavailableExceptionDeserializer().deserialize(context, wrappedCall, payload)
        "BadRequestException" -> BadRequestExceptionDeserializer().deserialize(context, wrappedCall, payload)
        "InternalFailureException" -> InternalFailureExceptionDeserializer().deserialize(context, wrappedCall, payload)
        "ConflictException" -> ConflictExceptionDeserializer().deserialize(context, wrappedCall, payload)
        "LimitExceededException" -> LimitExceededExceptionDeserializer().deserialize(context, wrappedCall, payload)
        else -> TranscribeStreamingException(errorDetails.message)
    }

    setAseErrorMetadata(ex, wrappedResponse, errorDetails)
    throw ex
}

internal suspend fun deserializeStartCallAnalyticsStreamTranscriptionOperationBody(builder: StartCallAnalyticsStreamTranscriptionResponse.Builder, call: HttpCall) {
    val chan = call.response.body.toSdkByteReadChannel(call) ?: return
    val frames = decodeFrames(chan)
    val events = frames
        .map { message ->
            when (val mt = message.type()) {
                is MessageType.Event -> when (mt.shapeType) {
                    "UtteranceEvent" -> {
                        val e = deserializeUtteranceEventPayload(message.payload)
                        CallAnalyticsTranscriptResultStream.UtteranceEvent(e)
                    }
                    "CategoryEvent" -> {
                        val e = deserializeCategoryEventPayload(message.payload)
                        CallAnalyticsTranscriptResultStream.CategoryEvent(e)
                    }
                    else -> CallAnalyticsTranscriptResultStream.SdkUnknown
                }
                is MessageType.Exception -> when (mt.shapeType) {
                    "BadRequestException" -> {
                        val err = deserializeBadRequestExceptionPayload(message.payload)
                        throw err
                    }
                    "LimitExceededException" -> {
                        val err = deserializeLimitExceededExceptionPayload(message.payload)
                        throw err
                    }
                    "InternalFailureException" -> {
                        val err = deserializeInternalFailureExceptionPayload(message.payload)
                        throw err
                    }
                    "ConflictException" -> {
                        val err = deserializeConflictExceptionPayload(message.payload)
                        throw err
                    }
                    "ServiceUnavailableException" -> {
                        val err = deserializeServiceUnavailableExceptionPayload(message.payload)
                        throw err
                    }
                    else -> throw TranscribeStreamingException("error processing event stream, unrecognized errorType: ${mt.shapeType}")
                }
                is MessageType.Error -> throw TranscribeStreamingException("error processing event stream: errorCode=${mt.errorCode}; message=${mt.message}")
                is MessageType.SdkUnknown -> throw ClientException("unrecognized event stream message `:message-type`: ${mt.messageType}")
            }
        }

    builder.callAnalyticsTranscriptResultStream = events
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy