commonMain.aws.sdk.kotlin.services.transcribestreaming.serde.StartStreamTranscriptionOperationDeserializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of transcribestreaming-jvm Show documentation
Show all versions of transcribestreaming-jvm Show documentation
The AWS SDK for Kotlin client for Transcribe Streaming
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.ContentIdentificationType
import aws.sdk.kotlin.services.transcribestreaming.model.ContentRedactionType
import aws.sdk.kotlin.services.transcribestreaming.model.LanguageCode
import aws.sdk.kotlin.services.transcribestreaming.model.MediaEncoding
import aws.sdk.kotlin.services.transcribestreaming.model.PartialResultsStability
import aws.sdk.kotlin.services.transcribestreaming.model.StartStreamTranscriptionResponse
import aws.sdk.kotlin.services.transcribestreaming.model.TranscribeStreamingException
import aws.sdk.kotlin.services.transcribestreaming.model.TranscriptResultStream
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 StartStreamTranscriptionOperationDeserializer: HttpDeserializer.Streaming {
override suspend fun deserialize(context: ExecutionContext, call: HttpCall): StartStreamTranscriptionResponse {
val response = call.response
if (!response.status.isSuccess()) {
val payload = response.body.readAll()
throwStartStreamTranscriptionError(context, call, payload)
}
val builder = StartStreamTranscriptionResponse.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.enableChannelIdentification = response.headers["x-amzn-transcribe-enable-channel-identification"]?.toBoolean() ?: false
builder.enablePartialResultsStabilization = response.headers["x-amzn-transcribe-enable-partial-results-stabilization"]?.toBoolean() ?: false
builder.identifyLanguage = response.headers["x-amzn-transcribe-identify-language"]?.toBoolean() ?: false
builder.identifyMultipleLanguages = response.headers["x-amzn-transcribe-identify-multiple-languages"]?.toBoolean() ?: false
builder.languageCode = response.headers["x-amzn-transcribe-language-code"]?.let { LanguageCode.fromValue(it) }
builder.languageModelName = response.headers["x-amzn-transcribe-language-model-name"]
builder.languageOptions = response.headers["x-amzn-transcribe-language-options"]
builder.mediaEncoding = response.headers["x-amzn-transcribe-media-encoding"]?.let { MediaEncoding.fromValue(it) }
builder.mediaSampleRateHertz = response.headers["x-amzn-transcribe-sample-rate"]?.toInt()
builder.numberOfChannels = response.headers["x-amzn-transcribe-number-of-channels"]?.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.preferredLanguage = response.headers["x-amzn-transcribe-preferred-language"]?.let { LanguageCode.fromValue(it) }
builder.requestId = response.headers["x-amzn-request-id"]
builder.sessionId = response.headers["x-amzn-transcribe-session-id"]
builder.showSpeakerLabel = response.headers["x-amzn-transcribe-show-speaker-label"]?.toBoolean() ?: false
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.vocabularyFilterNames = response.headers["x-amzn-transcribe-vocabulary-filter-names"]
builder.vocabularyName = response.headers["x-amzn-transcribe-vocabulary-name"]
builder.vocabularyNames = response.headers["x-amzn-transcribe-vocabulary-names"]
deserializeStartStreamTranscriptionOperationBody(builder, call)
builder.correctErrors()
return builder.build()
}
}
private fun throwStartStreamTranscriptionError(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 deserializeStartStreamTranscriptionOperationBody(builder: StartStreamTranscriptionResponse.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) {
"TranscriptEvent" -> {
val e = deserializeTranscriptEventPayload(message.payload)
TranscriptResultStream.TranscriptEvent(e)
}
else -> TranscriptResultStream.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.transcriptResultStream = events
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy