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

commonMain.aws.sdk.kotlin.services.transcribestreaming.serde.StartCallAnalyticsStreamTranscriptionOperationSerializer.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.AudioStream
import aws.sdk.kotlin.services.transcribestreaming.model.CallAnalyticsLanguageCode
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.StartCallAnalyticsStreamTranscriptionRequest
import aws.sdk.kotlin.services.transcribestreaming.model.VocabularyFilterMethod
import aws.smithy.kotlin.runtime.auth.awssigning.AwsSigningAttributes
import aws.smithy.kotlin.runtime.auth.awssigning.DefaultAwsSigner
import aws.smithy.kotlin.runtime.auth.awssigning.HashSpecification
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.HeaderValue
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.Message
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.asEventStreamHttpBody
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.buildMessage
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.encode
import aws.smithy.kotlin.runtime.awsprotocol.eventstream.sign
import aws.smithy.kotlin.runtime.http.HttpBody
import aws.smithy.kotlin.runtime.http.HttpMethod
import aws.smithy.kotlin.runtime.http.operation.HttpSerializer
import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder
import aws.smithy.kotlin.runtime.http.request.headers
import aws.smithy.kotlin.runtime.http.request.url
import aws.smithy.kotlin.runtime.operation.ExecutionContext
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.job


internal class StartCallAnalyticsStreamTranscriptionOperationSerializer: HttpSerializer.Streaming {
    override suspend fun serialize(context: ExecutionContext, input: StartCallAnalyticsStreamTranscriptionRequest): HttpRequestBuilder {
        val builder = HttpRequestBuilder()
        builder.method = HttpMethod.POST

        builder.url {
            path.encoded = "/call-analytics-stream-transcription"
        }

        builder.headers {
            if (input.contentIdentificationType != null) append("x-amzn-transcribe-content-identification-type", input.contentIdentificationType.value)
            if (input.contentRedactionType != null) append("x-amzn-transcribe-content-redaction-type", input.contentRedactionType.value)
            if (input.enablePartialResultsStabilization != null) append("x-amzn-transcribe-enable-partial-results-stabilization", input.enablePartialResultsStabilization.toString())
            if (input.languageCode != null) append("x-amzn-transcribe-language-code", input.languageCode.value)
            if (input.languageModelName?.isNotEmpty() == true) append("x-amzn-transcribe-language-model-name", input.languageModelName)
            if (input.mediaEncoding != null) append("x-amzn-transcribe-media-encoding", input.mediaEncoding.value)
            if (input.mediaSampleRateHertz != null) append("x-amzn-transcribe-sample-rate", input.mediaSampleRateHertz.toString())
            if (input.partialResultsStability != null) append("x-amzn-transcribe-partial-results-stability", input.partialResultsStability.value)
            if (input.piiEntityTypes?.isNotEmpty() == true) append("x-amzn-transcribe-pii-entity-types", input.piiEntityTypes)
            if (input.sessionId?.isNotEmpty() == true) append("x-amzn-transcribe-session-id", input.sessionId)
            if (input.vocabularyFilterMethod != null) append("x-amzn-transcribe-vocabulary-filter-method", input.vocabularyFilterMethod.value)
            if (input.vocabularyFilterName?.isNotEmpty() == true) append("x-amzn-transcribe-vocabulary-filter-name", input.vocabularyFilterName)
            if (input.vocabularyName?.isNotEmpty() == true) append("x-amzn-transcribe-vocabulary-name", input.vocabularyName)
        }

        builder.body = serializeStartCallAnalyticsStreamTranscriptionOperationBody(context, input)
        if (builder.body !is HttpBody.Empty) {
            builder.headers.setMissing("Content-Type", "application/json")
        }
        return builder
    }
}

internal suspend fun serializeStartCallAnalyticsStreamTranscriptionOperationBody(context: ExecutionContext, input: StartCallAnalyticsStreamTranscriptionRequest): HttpBody {
    val stream = input.audioStream ?: return HttpBody.Empty
    context[AwsSigningAttributes.HashSpecification] = HashSpecification.EmptyBody
    context[AwsSigningAttributes.Signer] = DefaultAwsSigner
    context[AwsSigningAttributes.RequestSignature] = CompletableDeferred(context.coroutineContext.job)

    val messages = stream
        .map(::encodeStartCallAnalyticsStreamTranscriptionAudioStreamEventMessage)
        .sign(context)
        .encode()

    return messages.asEventStreamHttpBody(context)
}

private fun encodeStartCallAnalyticsStreamTranscriptionAudioStreamEventMessage(input: AudioStream): Message = buildMessage {
    addHeader(":message-type", HeaderValue.String("event"))
    when(input) {
        is AudioStream.AudioEvent -> {
            addHeader(":event-type", HeaderValue.String("AudioEvent"))
            addHeader(":content-type", HeaderValue.String("application/octet-stream"))
            payload = input.value.audioChunk
        }
        is AudioStream.ConfigurationEvent -> {
            addHeader(":event-type", HeaderValue.String("ConfigurationEvent"))
            addHeader(":content-type", HeaderValue.String("application/json"))
            payload = serializeConfigurationEventPayload(input.value)
        }
        is AudioStream.SdkUnknown -> error("cannot serialize the unknown event type!")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy