|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.rustsdk |
| 7 | + |
| 8 | +import software.amazon.smithy.aws.traits.HttpChecksumTrait |
| 9 | +import software.amazon.smithy.model.shapes.OperationShape |
| 10 | +import software.amazon.smithy.model.shapes.StructureShape |
| 11 | +import software.amazon.smithy.model.traits.HttpHeaderTrait |
| 12 | +import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext |
| 13 | +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator |
| 14 | +import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization |
| 15 | +import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationSection |
| 16 | +import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency |
| 17 | +import software.amazon.smithy.rust.codegen.core.rustlang.Visibility |
| 18 | +import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate |
| 19 | +import software.amazon.smithy.rust.codegen.core.rustlang.writable |
| 20 | +import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig |
| 21 | +import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType |
| 22 | +import software.amazon.smithy.rust.codegen.core.util.getTrait |
| 23 | +import software.amazon.smithy.rust.codegen.core.util.hasStreamingMember |
| 24 | + |
| 25 | +class AwsChunkedContentEncodingDecorator : ClientCodegenDecorator { |
| 26 | + override val name: String = "AwsChunkedContentEncoding" |
| 27 | + |
| 28 | + // This decorator must decorate after any of the following: |
| 29 | + // - HttpRequestChecksumDecorator |
| 30 | + // - HttpRequestCompressionDecorator |
| 31 | + override val order: Byte = (minOf(HttpRequestChecksumDecorator.ORDER, HttpRequestCompressionDecorator.ORDER) - 1).toByte() |
| 32 | + |
| 33 | + override fun operationCustomizations( |
| 34 | + codegenContext: ClientCodegenContext, |
| 35 | + operation: OperationShape, |
| 36 | + baseCustomizations: List<OperationCustomization>, |
| 37 | + ) = baseCustomizations + AwsChunkedOparationCustomization(codegenContext, operation) |
| 38 | +} |
| 39 | + |
| 40 | +private class AwsChunkedOparationCustomization( |
| 41 | + private val codegenContext: ClientCodegenContext, |
| 42 | + private val operation: OperationShape, |
| 43 | +) : OperationCustomization() { |
| 44 | + private val model = codegenContext.model |
| 45 | + private val runtimeConfig = codegenContext.runtimeConfig |
| 46 | + |
| 47 | + override fun section(section: OperationSection) = |
| 48 | + writable { |
| 49 | + when (section) { |
| 50 | + is OperationSection.AdditionalInterceptors -> { |
| 51 | + // TODO(https://github.com/smithy-lang/smithy-rs/issues/4382): Remove all of these early returns |
| 52 | + // once we have the dedicated trait available in Smithy. |
| 53 | + val checksumTrait = operation.getTrait<HttpChecksumTrait>() ?: return@writable |
| 54 | + val requestAlgorithmMember = |
| 55 | + checksumTrait.requestAlgorithmMemberShape(codegenContext, operation) ?: return@writable |
| 56 | + requestAlgorithmMember.getTrait<HttpHeaderTrait>()?.value ?: return@writable |
| 57 | + val input = model.expectShape(operation.inputShape, StructureShape::class.java) |
| 58 | + if (!input.hasStreamingMember(model)) { |
| 59 | + return@writable |
| 60 | + } |
| 61 | + |
| 62 | + section.registerInterceptor(runtimeConfig, this) { |
| 63 | + rustTemplate( |
| 64 | + """ |
| 65 | + #{AwsChunkedContentEncodingInterceptor} |
| 66 | + """, |
| 67 | + "AwsChunkedContentEncodingInterceptor" to |
| 68 | + runtimeConfig.awsChunked() |
| 69 | + .resolve("AwsChunkedContentEncodingInterceptor"), |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + else -> emptySection |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +private fun RuntimeConfig.awsChunked() = |
| 80 | + RuntimeType.forInlineDependency( |
| 81 | + InlineAwsDependency.forRustFile( |
| 82 | + "aws_chunked", visibility = Visibility.PUBCRATE, |
| 83 | + CargoDependency.Bytes, |
| 84 | + CargoDependency.Http, |
| 85 | + CargoDependency.HttpBody, |
| 86 | + CargoDependency.Tracing, |
| 87 | + AwsCargoDependency.awsRuntime(this).withFeature("http-02x"), |
| 88 | + CargoDependency.smithyRuntimeApiClient(this), |
| 89 | + CargoDependency.smithyTypes(this), |
| 90 | + AwsCargoDependency.awsSigv4(this), |
| 91 | + CargoDependency.TempFile.toDevDependency(), |
| 92 | + ), |
| 93 | + ) |
0 commit comments