Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.rustsdk

import software.amazon.smithy.aws.traits.HttpChecksumTrait
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.HttpHeaderTrait
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationSection
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.Visibility
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.util.getTrait
import software.amazon.smithy.rust.codegen.core.util.hasStreamingMember

class AwsChunkedContentEncodingDecorator : ClientCodegenDecorator {
override val name: String = "AwsChunkedContentEncoding"

// This decorator must decorate after any of the following:
// - HttpRequestChecksumDecorator
// - HttpRequestCompressionDecorator
override val order: Byte = (minOf(HttpRequestChecksumDecorator.ORDER, HttpRequestCompressionDecorator.ORDER) - 1).toByte()

override fun operationCustomizations(
codegenContext: ClientCodegenContext,
operation: OperationShape,
baseCustomizations: List<OperationCustomization>,
) = baseCustomizations + AwsChunkedOparationCustomization(codegenContext, operation)
}

private class AwsChunkedOparationCustomization(
private val codegenContext: ClientCodegenContext,
private val operation: OperationShape,
) : OperationCustomization() {
private val model = codegenContext.model
private val runtimeConfig = codegenContext.runtimeConfig

override fun section(section: OperationSection) =
writable {
when (section) {
is OperationSection.AdditionalInterceptors -> {
// TODO(https://github.com/smithy-lang/smithy-rs/issues/4382): Remove all of these early returns
// once we have the dedicated trait available in Smithy.
val checksumTrait = operation.getTrait<HttpChecksumTrait>() ?: return@writable
val requestAlgorithmMember =
checksumTrait.requestAlgorithmMemberShape(codegenContext, operation) ?: return@writable
requestAlgorithmMember.getTrait<HttpHeaderTrait>()?.value ?: return@writable
val input = model.expectShape(operation.inputShape, StructureShape::class.java)
if (!input.hasStreamingMember(model)) {
return@writable
}

section.registerInterceptor(runtimeConfig, this) {
rustTemplate(
"""
#{AwsChunkedContentEncodingInterceptor}
""",
"AwsChunkedContentEncodingInterceptor" to
runtimeConfig.awsChunked()
.resolve("AwsChunkedContentEncodingInterceptor"),
)
}
}

else -> emptySection
}
}
}

private fun RuntimeConfig.awsChunked() =
RuntimeType.forInlineDependency(
InlineAwsDependency.forRustFile(
"aws_chunked", visibility = Visibility.PUBCRATE,
CargoDependency.Bytes,
CargoDependency.Http,
CargoDependency.HttpBody,
CargoDependency.Tracing,
AwsCargoDependency.awsRuntime(this).withFeature("http-02x"),
CargoDependency.smithyRuntimeApiClient(this),
CargoDependency.smithyTypes(this),
AwsCargoDependency.awsSigv4(this),
CargoDependency.TempFile.toDevDependency(),
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ val DECORATORS: List<ClientCodegenDecorator> =
SdkConfigDecorator(),
ServiceConfigDecorator(),
AwsPresigningDecorator(),
AwsChunkedContentEncodingDecorator(),
AwsCrateDocsDecorator(),
AwsEndpointsStdLib(),
*PromotedBuiltInsDecorators,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ internal fun RuntimeConfig.awsInlineableHttpRequestChecksum() =
)

class HttpRequestChecksumDecorator : ClientCodegenDecorator {
companion object {
const val ORDER: Byte = 0
}

override val name: String = "HttpRequestChecksum"
override val order: Byte = 0
override val order: Byte = ORDER

override fun operationCustomizations(
codegenContext: ClientCodegenContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import software.amazon.smithy.rust.codegen.core.smithy.customize.adhocCustomizat
import software.amazon.smithy.rust.codegen.core.util.thenSingletonListOf

class HttpRequestCompressionDecorator : ClientCodegenDecorator {
companion object {
const val ORDER: Byte = 0
}

override val name: String = "HttpRequestCompression"
override val order: Byte = 0
override val order: Byte = ORDER

private fun usesRequestCompression(codegenContext: ClientCodegenContext): Boolean {
val index = TopDownIndex.of(codegenContext.model)
Expand Down
2 changes: 1 addition & 1 deletion aws/rust-runtime/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading