Skip to content

Commit

Permalink
Moving request checksum mutator to a closure
Browse files Browse the repository at this point in the history
  • Loading branch information
landonxjames committed Sep 30, 2024
1 parent 1f25cb4 commit d682c8d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 206 deletions.
2 changes: 1 addition & 1 deletion aws/rust-runtime/aws-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aws-config"
version = "1.5.7"
version = "1.5.8"
authors = [
"AWS Rust SDK Team <aws-sdk-rust@amazon.com>",
"Russell Cohen <rcoh@amazon.com>",
Expand Down
24 changes: 15 additions & 9 deletions aws/rust-runtime/aws-inlineable/src/http_request_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use aws_smithy_checksums::ChecksumAlgorithm;
use aws_smithy_checksums::{body::calculate, http::HttpChecksum};
use aws_smithy_runtime_api::box_error::BoxError;
use aws_smithy_runtime_api::client::interceptors::context::{
BeforeSerializationInterceptorContextRef, BeforeTransmitInterceptorContextMut, Input,
BeforeSerializationInterceptorContextMut, BeforeTransmitInterceptorContextMut, Input,
};
use aws_smithy_runtime_api::client::interceptors::Intercept;
use aws_smithy_runtime_api::client::orchestrator::HttpRequest;
Expand Down Expand Up @@ -102,36 +102,42 @@ impl DefaultRequestChecksumOverride {
}
}

pub(crate) struct RequestChecksumInterceptor<AP> {
pub(crate) struct RequestChecksumInterceptor<AP, CM> {
algorithm_provider: AP,
checksum_mutator: CM,
}

impl<AP> fmt::Debug for RequestChecksumInterceptor<AP> {
impl<AP, CM> fmt::Debug for RequestChecksumInterceptor<AP, CM> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RequestChecksumInterceptor").finish()
}
}

impl<AP> RequestChecksumInterceptor<AP> {
pub(crate) fn new(algorithm_provider: AP) -> Self {
Self { algorithm_provider }
impl<AP, CM> RequestChecksumInterceptor<AP, CM> {
pub(crate) fn new(algorithm_provider: AP, checksum_mutator: CM) -> Self {
Self {
algorithm_provider,
checksum_mutator,
}
}
}

impl<AP> Intercept for RequestChecksumInterceptor<AP>
impl<AP, CM> Intercept for RequestChecksumInterceptor<AP, CM>
where
AP: Fn(&Input) -> Result<(Option<ChecksumAlgorithm>, bool), BoxError> + Send + Sync,
CM: Fn(&mut Input, &ConfigBag) -> Result<(), BoxError> + Send + Sync,
{
fn name(&self) -> &'static str {
"RequestChecksumInterceptor"
}

fn read_before_serialization(
fn modify_before_serialization(
&self,
context: &BeforeSerializationInterceptorContextRef<'_>,
context: &mut BeforeSerializationInterceptorContextMut<'_>,
_runtime_components: &RuntimeComponents,
cfg: &mut ConfigBag,
) -> Result<(), BoxError> {
let _ = (self.checksum_mutator)(context.input_mut(), cfg);
let checksum_algorithm = (self.algorithm_provider)(context.input())?;
let mut layer = Layer::new("RequestChecksumInterceptor");
layer.store_put(RequestChecksumInterceptorState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ val DECORATORS: List<ClientCodegenDecorator> =
UserAgentDecorator(),
SigV4AuthDecorator(),
HttpRequestChecksumDecorator(),
HttpRequestChecksumMutationInterceptorDecorator(),
HttpResponseChecksumDecorator(),
HttpResponseChecksumMutationInterceptorDecorator(),
IntegrationTestDecorator(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,29 +160,71 @@ class HttpRequestChecksumCustomization(
writable {
// Get the `HttpChecksumTrait`, returning early if this `OperationShape` doesn't have one
val checksumTrait = operationShape.getTrait<HttpChecksumTrait>() ?: return@writable
val requestAlgorithmMember = checksumTrait.requestAlgorithmMember(codegenContext, operationShape)
val requestAlgorithmMember =
checksumTrait.requestAlgorithmMemberShape(codegenContext, operationShape) ?: return@writable
val requestAlgorithmMemberName = checksumTrait.requestAlgorithmMember(codegenContext, operationShape)
val inputShape = codegenContext.model.expectShape(operationShape.inputShape)
val requestChecksumRequired = checksumTrait.isRequestChecksumRequired
val operationName = codegenContext.symbolProvider.toSymbol(operationShape).name
val requestAlgorithmMemberInner =
if (requestAlgorithmMember.isOptional) {
codegenContext.model.expectShape(requestAlgorithmMember.target)
} else {
requestAlgorithmMember
}

when (section) {
is OperationSection.AdditionalInterceptors -> {
if (requestAlgorithmMember != null) {
if (requestAlgorithmMemberName != null) {
section.registerInterceptor(runtimeConfig, this) {
val runtimeApi = RuntimeType.smithyRuntimeApiClient(runtimeConfig)
rustTemplate(
"""
#{RequestChecksumInterceptor}::new(|input: &#{Input}| {
#{RequestChecksumInterceptor}::new(
|input: &#{Input}| {
let input: &#{OperationInput} = input.downcast_ref().expect("correct type");
let checksum_algorithm = input.$requestAlgorithmMember();
let checksum_algorithm = input.$requestAlgorithmMemberName();
#{checksum_algorithm_to_str}
#{Result}::<_, #{BoxError}>::Ok((checksum_algorithm, $requestChecksumRequired))
})
},
|input: &mut #{Input}, cfg: &#{ConfigBag}| {
let input = input
.downcast_mut::<#{OperationInputType}>()
.ok_or("failed to downcast to #{OperationInputType}")?;
// This value is set by the user on the SdkConfig to indicate their preference
let request_checksum_calculation = cfg
.load::<#{RequestChecksumCalculation}>()
.unwrap_or(&#{RequestChecksumCalculation}::WhenSupported);
// From the httpChecksum trait
let http_checksum_required = $requestChecksumRequired;
// If the RequestChecksumCalculation is WhenSupported and the user has not set a checksum we
// default to Crc32. If it is WhenRequired and a checksum is required by the trait we also set the
// default. In all other cases we do nothing.
match (
request_checksum_calculation,
http_checksum_required,
input.checksum_algorithm(),
) {
(#{RequestChecksumCalculation}::WhenSupported, _, None)
| (#{RequestChecksumCalculation}::WhenRequired, true, None) => {
input.checksum_algorithm = Some(#{ChecksumAlgoShape}::Crc32);
}
_ => {},
}
Ok(())
}
)
""",
*preludeScope,
"BoxError" to RuntimeType.boxError(runtimeConfig),
"Input" to runtimeApi.resolve("client::interceptors::context::Input"),
"OperationInput" to codegenContext.symbolProvider.toSymbol(inputShape),
"ConfigBag" to RuntimeType.configBag(runtimeConfig),
"RequestChecksumInterceptor" to
runtimeConfig.awsInlineableHttpRequestChecksum()
.resolve("RequestChecksumInterceptor"),
Expand All @@ -191,14 +233,19 @@ class HttpRequestChecksumCustomization(
codegenContext,
operationShape,
),
)
}
section.registerInterceptor(codegenContext.runtimeConfig, this) {
val interceptorName = "${operationName}HttpRequestChecksumMutationInterceptor"
rustTemplate(
"""
$interceptorName
""",
"RequestChecksumCalculation" to
CargoDependency.smithyTypes(runtimeConfig).toType()
.resolve("checksum_config::RequestChecksumCalculation"),
"ChecksumAlgoShape" to
codegenContext.symbolProvider.toSymbol(
requestAlgorithmMemberInner,
),
"OperationInputType" to
codegenContext.symbolProvider.toSymbol(
operationShape.inputShape(
codegenContext.model,
),
),
)
}
}
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion rust-runtime/aws-smithy-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aws-smithy-types"
version = "1.2.7"
version = "1.2.8"
authors = [
"AWS Rust SDK Team <aws-sdk-rust@amazon.com>",
"Russell Cohen <rcoh@amazon.com>",
Expand Down

0 comments on commit d682c8d

Please sign in to comment.