Skip to content

Commit

Permalink
feat(client): update HTTP request header code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed Apr 12, 2023
1 parent b4ad1b5 commit f099278
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion codegen/gradle.properties
@@ -1,2 +1,2 @@
smithyVersion=1.28.1
smithyVersion=1.30.0
smithyGradleVersion=0.6.0
Expand Up @@ -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
Expand Down
Expand Up @@ -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);
}

/**
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit f099278

Please sign in to comment.