Skip to content

Commit

Permalink
Update HttpRpcProtocolGenerator to allow request header code generati…
Browse files Browse the repository at this point in the history
…on customization
  • Loading branch information
AndrewFossAWS committed Apr 12, 2023
1 parent bbd7ecc commit f61e7e9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
smithyVersion=1.27.2
smithyVersion=1.30.0
smithyGradleVersion=0.6.0
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public void generateSharedComponents(GenerationContext context) {
writer.write("return new $T(contents);", requestType);
}
);
// Write common request header to be shared by all requests
writeSharedRequestHeaders(context);
writer.write("");
}

Expand Down Expand Up @@ -251,17 +253,74 @@ private void generateOperationSerializer(GenerationContext context, OperationSha
writer.write("");
}

private void writeRequestHeaders(GenerationContext context, OperationShape operation) {
/**
* Writes HTTP request headers required by the protocol implementation.
*
* <p>By default, headers are configured to use {@code SHARED_HEADERS}. See {@link #writeSharedRequestHeaders}
*
* <pre>{@code
* const headers: __HeaderBag = SHARED_HEADERS;
* }</pre>
*
* <p>This method can be overridden to customize headers generation. For example:
*
* <pre>{@code
* const headers: __HeaderBag = {
* "foo": "This is a custom header",
* ...SHARED_HEADERS
* };
* }</pre>
*
* @param context The generation context.
* @param operation The operation being generated.
*/
protected void writeRequestHeaders(GenerationContext context, OperationShape operation) {
TypeScriptWriter writer = context.getWriter();
writer.write("const headers: __HeaderBag = SHARED_HEADERS;");
}

// The Content-Type header is always present.
/**
* Writes headers to be shared for all HTTP requests by the protocol implementation.
*
* <p>To reduce generated code size, we should put all common headers into single location.
* <p>For example, most request headers contain {@code content-type}.
*
* <pre>{@code
* const SHARED_HEADERS: __HeaderBag = {
* "content-type": "application/x-www-form-urlencoded",
* };
* }</pre>
*
* <p>{@code SHARED_HEADERS} can then be used as follows:
*
* <pre>{@code
* const headers: __HeaderBag = {
* "foo": "This is a custom header",
* ...SHARED_HEADERS
* };
* }</pre>
*
* <p>This method can be overridden for customization. For example:
*
* <pre>{@code
* function sharedHeaders(operationName): __HeaderBag = {
* "custom-header": "xyz-service:${operationName}",
* };
*
* const headers: __HeaderBag = {
* "foo": "This is a custom header",
* ...sharedHeaders(operationName)
* };
* }</pre>
*
* @param context The generation context.
*/
protected void writeSharedRequestHeaders(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();
writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types");
writer.openBlock("const headers: __HeaderBag = {", "};",
() -> {
writer.write("'content-type': $S,", getDocumentContentType());
writeDefaultHeaders(context, operation);
}
);
writer.openBlock("const SHARED_HEADERS: __HeaderBag = {", "};", () -> {
writer.write("'content-type': $S,", getDocumentContentType());
});
}

private boolean writeRequestBody(GenerationContext context, OperationShape operation) {
Expand Down Expand Up @@ -300,27 +359,6 @@ private boolean writeRequestBody(GenerationContext context, OperationShape opera
*/
protected abstract String getOperationPath(GenerationContext context, OperationShape operation);

/**
* Writes any additional HTTP headers required by the protocol implementation.
*
* <p>Two parameters will be available in scope:
* <ul>
* <li>{@code input: <T>}: the type generated for the operation's input.</li>
* <li>{@code context: SerdeContext}: a TypeScript type containing context and tools for type serde.</li>
* </ul>
*
* <p>For example:
*
* <pre>{@code
* "foo": "This is a custom header",
* }</pre>
*
* @param context The generation context.
* @param operation The operation being generated.
*/
protected void writeDefaultHeaders(GenerationContext context, OperationShape operation) {
}

/**
* Writes the code needed to serialize the input document of a request.
*
Expand Down

0 comments on commit f61e7e9

Please sign in to comment.