From f099278645c0a821ca21bea931a360d110400572 Mon Sep 17 00:00:00 2001 From: fossand Date: Wed, 12 Apr 2023 08:52:00 -0700 Subject: [PATCH] feat(client): update HTTP request header code generation --- codegen/gradle.properties | 2 +- .../smithy/aws/typescript/codegen/AwsEc2.java | 13 +++++-- .../typescript/codegen/AwsProtocolUtils.java | 10 +++--- .../aws/typescript/codegen/AwsQuery.java | 13 +++++-- .../codegen/JsonRpcProtocolGenerator.java | 34 ++++++++++++++----- 5 files changed, 53 insertions(+), 19 deletions(-) diff --git a/codegen/gradle.properties b/codegen/gradle.properties index 553d88779683..3c0538176572 100644 --- a/codegen/gradle.properties +++ b/codegen/gradle.properties @@ -1,2 +1,2 @@ -smithyVersion=1.28.1 +smithyVersion=1.30.0 smithyGradleVersion=0.6.0 \ No newline at end of file diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsEc2.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsEc2.java index 7fc611fa664b..a6ad384f9f1e 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsEc2.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsEc2.java @@ -117,9 +117,16 @@ protected String getErrorBodyLocation(GenerationContext context, String outputLo } @Override - protected void writeDefaultHeaders(GenerationContext context, OperationShape operation) { - super.writeDefaultHeaders(context, operation); - AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation); + protected void writeRequestHeaders(GenerationContext context, OperationShape operation) { + TypeScriptWriter writer = context.getWriter(); + if (AwsProtocolUtils.includeUnsignedPayloadSigV4Header(operation)) { + writer.openBlock("const headers: __HeaderBag = { ", " }", () -> { + AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation); + writer.write("...SHARED_HEADERS"); + }); + } else { + writer.write("const headers: __HeaderBag = SHARED_HEADERS;"); + } } @Override diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java index 2ff92f19d182..d78b684a2544 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java @@ -65,11 +65,13 @@ private AwsProtocolUtils() {} */ static void generateUnsignedPayloadSigV4Header(GenerationContext context, OperationShape operation) { TypeScriptWriter writer = context.getWriter(); + if (includeUnsignedPayloadSigV4Header(operation)) { + writer.write("'x-amz-content-sha256': 'UNSIGNED-PAYLOAD',"); + } + } - operation.getTrait(UnsignedPayloadTrait.class) - .ifPresent(trait -> { - writer.write("'x-amz-content-sha256': 'UNSIGNED-PAYLOAD',"); - }); + static boolean includeUnsignedPayloadSigV4Header(OperationShape operation) { + return operation.hasTrait(UnsignedPayloadTrait.ID); } /** diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsQuery.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsQuery.java index 837d012e6bcb..ba163fd8852a 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsQuery.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsQuery.java @@ -121,9 +121,16 @@ protected String getErrorBodyLocation(GenerationContext context, String outputLo } @Override - protected void writeDefaultHeaders(GenerationContext context, OperationShape operation) { - super.writeDefaultHeaders(context, operation); - AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation); + protected void writeRequestHeaders(GenerationContext context, OperationShape operation) { + TypeScriptWriter writer = context.getWriter(); + if (AwsProtocolUtils.includeUnsignedPayloadSigV4Header(operation)) { + writer.openBlock("const headers: __HeaderBag = { ", " }", () -> { + AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation); + writer.write("...SHARED_HEADERS"); + }); + } else { + writer.write("const headers: __HeaderBag = SHARED_HEADERS;"); + } } @Override diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java index c8d8e9457bdb..6f587f37bf7e 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java @@ -94,16 +94,34 @@ public void generateSharedComponents(GenerationContext context) { } @Override - protected void writeDefaultHeaders(GenerationContext context, OperationShape operation) { - super.writeDefaultHeaders(context, operation); - AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation); - - // AWS JSON RPC protocols use a combination of the service and operation shape names, - // separated by a '.' character, for the target header. + protected void writeRequestHeaders(GenerationContext context, OperationShape operation) { TypeScriptWriter writer = context.getWriter(); ServiceShape serviceShape = context.getService(); - String target = serviceShape.getId().getName(serviceShape) + "." + operation.getId().getName(serviceShape); - writer.write("'x-amz-target': $S,", target); + String operationName = operation.getId().getName(serviceShape); + if (AwsProtocolUtils.includeUnsignedPayloadSigV4Header(operation)) { + writer.openBlock("const headers: __HeaderBag = { ", " }", () -> { + AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation); + writer.write("...sharedHeaders($S)", operationName); + }); + } else { + writer.write("const headers: __HeaderBag = sharedHeaders($S)", operationName); + } + } + + @Override + protected void writeSharedRequestHeaders(GenerationContext context) { + ServiceShape serviceShape = context.getService(); + TypeScriptWriter writer = context.getWriter(); + writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types"); + String targetHeader = serviceShape.getId().getName(serviceShape) + ".${operation}"; + writer.openBlock("function sharedHeaders(operation: string): __HeaderBag { return {", "}};", + () -> { + writer.write("'content-type': $S,", getDocumentContentType()); + // AWS JSON RPC protocols use a combination of the service and operation shape names, + // separated by a '.' character, for the target header. + writer.write("'x-amz-target': `$L`,", targetHeader); + } + ); } @Override