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

commonMain.aws.sdk.kotlin.services.polly.presigners.Presigners.kt Maven / Gradle / Ivy

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

package aws.sdk.kotlin.services.polly.presigners

import aws.sdk.kotlin.runtime.ClientException
import aws.sdk.kotlin.runtime.auth.credentials.DefaultChainCredentialsProvider
import aws.sdk.kotlin.runtime.endpoint.asSigningContext
import aws.sdk.kotlin.runtime.endpoint.authScheme
import aws.sdk.kotlin.services.polly.PollyClient
import aws.sdk.kotlin.services.polly.endpoints.internal.EndpointResolverAdapter
import aws.sdk.kotlin.services.polly.model.SynthesizeSpeechRequest
import aws.sdk.kotlin.services.polly.transform.SynthesizeSpeechOperationSerializer
import aws.smithy.kotlin.runtime.auth.awscredentials.CredentialsProvider
import aws.smithy.kotlin.runtime.auth.awssigning.AwsSigner
import aws.smithy.kotlin.runtime.auth.awssigning.DefaultAwsSigner
import aws.smithy.kotlin.runtime.auth.awssigning.PresignedRequestConfig
import aws.smithy.kotlin.runtime.auth.awssigning.PresigningLocation
import aws.smithy.kotlin.runtime.auth.awssigning.ServicePresignConfig
import aws.smithy.kotlin.runtime.auth.awssigning.SigningContextualizedEndpoint
import aws.smithy.kotlin.runtime.auth.awssigning.SigningEndpointProvider
import aws.smithy.kotlin.runtime.auth.awssigning.createPresignedRequest
import aws.smithy.kotlin.runtime.client.SdkClientOption
import aws.smithy.kotlin.runtime.http.HttpMethod
import aws.smithy.kotlin.runtime.http.auth.AnonymousIdentity
import aws.smithy.kotlin.runtime.http.operation.HttpOperationContext
import aws.smithy.kotlin.runtime.http.operation.ResolveEndpointRequest
import aws.smithy.kotlin.runtime.http.request.HttpRequest
import aws.smithy.kotlin.runtime.http.request.HttpRequestBuilder
import aws.smithy.kotlin.runtime.http.util.quoteHeaderValue
import aws.smithy.kotlin.runtime.net.QueryParameters
import aws.smithy.kotlin.runtime.net.QueryParametersBuilder
import aws.smithy.kotlin.runtime.operation.ExecutionContext
import kotlin.time.Duration

/**
 * Presign a [SynthesizeSpeechRequest] using a [ServicePresignConfig].
 * @param presignConfig the configuration used to generate the presigned request
 * @param duration the amount of time from signing for which the request is valid, with seconds granularity.
 * @return The [HttpRequest] that can be invoked within the specified time window.
 */
public suspend fun SynthesizeSpeechRequest.presign(presignConfig: ServicePresignConfig, duration: Duration): HttpRequest {
    return createPresignedRequest(presignConfig, synthesizeSpeechPresignConfig(this, duration))
}

/**
 * Presign a [SynthesizeSpeechRequest] using a [PollyClient].
 * @param config the client configuration used to generate the presigned request.
 * @param duration the amount of time from signing for which the request is valid, with seconds granularity.
 * @return The [HttpRequest] that can be invoked within the specified time window.
 */
public suspend fun SynthesizeSpeechRequest.presign(config: PollyClient.Config, duration: Duration): HttpRequest {
    val presignConfig = PollyPresignConfig {
        credentialsProvider = config.credentialsProvider
        endpointProvider = EndpointResolverAdapter(config).asSigningProvider(this@presign, "SynthesizeSpeech")
        region = config.region
    }
    return createPresignedRequest(presignConfig, synthesizeSpeechPresignConfig(this, duration))
}

private suspend fun synthesizeSpeechPresignConfig(input: SynthesizeSpeechRequest, duration: Duration) : PresignedRequestConfig {
    require(duration.isPositive()) { "duration must be greater than zero" }
    val httpRequestBuilder = SynthesizeSpeechOperationSerializer().serialize(ExecutionContext.build { }, input)
    val queryStringBuilder = QueryParametersBuilder()
    with(queryStringBuilder) {
        if (input.engine != null) append("Engine", input.engine.value)
        if (input.languageCode != null) append("LanguageCode", input.languageCode.value)
        if (input.lexiconNames?.isNotEmpty() == true) appendAll("LexiconNames", input.lexiconNames.map { quoteHeaderValue(it) })
        if (input.outputFormat != null) append("OutputFormat", input.outputFormat.value)
        if (input.sampleRate?.isNotEmpty() == true) append("SampleRate", input.sampleRate)
        if (input.speechMarkTypes?.isNotEmpty() == true) appendAll("SpeechMarkTypes", input.speechMarkTypes.map { "$it" })
        if (input.text?.isNotEmpty() == true) append("Text", input.text)
        if (input.textType != null) append("TextType", input.textType.value)
        if (input.voiceId != null) append("VoiceId", input.voiceId.value)
    }
    return PresignedRequestConfig(
        HttpMethod.GET,
        httpRequestBuilder.url.path,
        queryStringBuilder.build(),
        duration,
        true,
        PresigningLocation.QUERY_STRING,
    )
}

/**
 * Provides a subset of the service client configuration necessary to presign a request.
 * This type can be used to presign requests in cases where an existing service client
 * instance is not available.
 */
public class PollyPresignConfig private constructor(builder: Builder) : ServicePresignConfig {
    override val credentialsProvider: CredentialsProvider = requireNotNull(builder.credentialsProvider) { "credentialsProvider is a required configuration property" }
    override val endpointProvider: SigningEndpointProvider = requireNotNull(builder.endpointProvider) { "endpointProvider is a required configuration property" }
    override val normalizeUriPath: Boolean = true
    override val region: String = requireNotNull(builder.region) { "region is a required configuration property" }
    override val serviceId: String = "Polly"
    override val signer: AwsSigner = builder.signer ?: DefaultAwsSigner
    override val signingName: String = "polly"
    override val useDoubleUriEncode: Boolean = true
    public companion object {
        public inline operator fun invoke(block: Builder.() -> kotlin.Unit): PollyPresignConfig = Builder().apply(block).build()
    }

    public fun toBuilder(): Builder = Builder().apply {
        credentialsProvider = [email protected]
        endpointProvider = [email protected]
        region = [email protected]
        signer = [email protected]
    }

    public class Builder {
        /**
         * The AWS credentials provider to use for authenticating requests. If not provided a [aws.sdk.kotlin.runtime.auth.credentials.DefaultChainCredentialsProvider] instance will be used.
         */
        public var credentialsProvider: CredentialsProvider? = null

        /**
         * Provides the endpoint (hostname) and signing context to make requests to.
         */
        public var endpointProvider: SigningEndpointProvider? = null

        /**
         * AWS region to make requests for
         */
        public var region: String? = null

        /**
         * The implementation of AWS signer to use for signing requests
         */
        public var signer: AwsSigner? = null

        @PublishedApi
        internal fun build(): PollyPresignConfig = PollyPresignConfig(this)
    }
}

internal fun EndpointResolverAdapter.asSigningProvider(input: Any, operationName: String): SigningEndpointProvider = {
    val execContext = ExecutionContext().apply {
        set(SdkClientOption.OperationName, operationName)
        set(HttpOperationContext.OperationInput, input)
    }
    val httpReq = HttpRequestBuilder().build()
    val request = ResolveEndpointRequest(execContext, httpReq, AnonymousIdentity)
    val endpoint = resolve(request)
    SigningContextualizedEndpoint(endpoint, endpoint.authScheme?.asSigningContext())
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy