Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HttpRpcProtocolGenerator to allow request header code generati… #729

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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