Skip to content

Define headers while declaring #175

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

Merged
merged 4 commits into from
May 11, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -353,40 +353,46 @@ private void writeHeaders(
SymbolProvider symbolProvider = context.getSymbolProvider();

// Headers are always present either from the default document or the payload.
writer.write("const headers: any = {};");
writer.write("headers['Content-Type'] = $S;", bindingIndex.determineRequestContentType(
operation, getDocumentContentType()));
writeDefaultHeaders(context, operation);

operation.getInput().ifPresent(outputId -> {
Model model = context.getModel();
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.HEADER)) {
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
writer.openBlock("if (isSerializableHeaderValue($1L)) {", "}", memberLocation, () -> {
Shape target = model.expectShape(binding.getMember().getTarget());
String headerValue = getInputValue(context, binding.getLocation(), memberLocation + "!",
binding.getMember(), target);
writer.write("headers[$S] = $L;", binding.getLocationName(), headerValue);
});
}

// Handle assembling prefix headers.
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.PREFIX_HEADERS)) {
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
writer.openBlock("if ($L !== undefined) {", "}", memberLocation, () -> {
MapShape prefixMap = model.expectShape(binding.getMember().getTarget()).asMapShape().get();
Shape target = model.expectShape(prefixMap.getValue().getTarget());
// Iterate through each entry in the member.
writer.openBlock("Object.keys($L).forEach(suffix => {", "});", memberLocation, () -> {
// Use a ! since we already validated the input member is defined above.
String headerValue = getInputValue(context, binding.getLocation(),
memberLocation + "![suffix]", binding.getMember(), target);
// Append the suffix to the defined prefix and serialize the value in to that key.
writer.write("headers[$S + suffix] = $L;", binding.getLocationName(), headerValue);
});
writer.openBlock("const headers: any = {", "};",
() -> {
writer.write("'Content-Type': $S,", bindingIndex.determineRequestContentType(
operation, getDocumentContentType()));
writeDefaultHeaders(context, operation);

operation.getInput().ifPresent(outputId -> {
Model model = context.getModel();
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.HEADER)) {
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
Shape target = model.expectShape(binding.getMember().getTarget());
String headerValue = getInputValue(context, binding.getLocation(), memberLocation + "!",
binding.getMember(), target);
writer.write("...isSerializableHeaderValue($L) && { $S: $L },",
memberLocation, binding.getLocationName(), headerValue);
}

// Handle assembling prefix headers.
for (HttpBinding binding : bindingIndex.getRequestBindings(operation, Location.PREFIX_HEADERS)) {
String memberLocation = "input." + symbolProvider.toMemberName(binding.getMember());
MapShape prefixMap = model.expectShape(binding.getMember().getTarget()).asMapShape().get();
Shape target = model.expectShape(prefixMap.getValue().getTarget());
// Iterate through each entry in the member.
writer.openBlock("...($1L !== undefined) && Object.keys($1L).reduce(", "),", memberLocation,
() -> {
writer.openBlock("(acc: any, suffix: string) => {", "}, {}",
() -> {
// Use a ! since we already validated the input member is defined above.
String headerValue = getInputValue(context, binding.getLocation(),
memberLocation + "![suffix]", binding.getMember(), target);
// Append the prefix to key.
writer.write("acc[$S + suffix] = $L;", binding.getLocationName(), headerValue);
writer.write("return acc;");
});
}
);
}
});
}
});
);
}

private List<HttpBinding> writeRequestBody(
Expand Down Expand Up @@ -602,7 +608,7 @@ private String getTimestampInputParam(
* <p>For example:
*
* <pre>{@code
* headers['foo'] = "This is a custom header";
* "foo": "This is a custom header",
* }</pre>
*
* @param context The generation context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@ private void writeRequestHeaders(GenerationContext context, OperationShape opera

// The Content-Type header is always present.
writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types");
writer.write("const headers: __HeaderBag = {};");
writer.write("headers['Content-Type'] = $S;", getDocumentContentType());
writeDefaultHeaders(context, operation);
writer.openBlock("const headers: __HeaderBag = {", "};",
() -> {
writer.write("'Content-Type': $S,", getDocumentContentType());
writeDefaultHeaders(context, operation);
}
);
}

private boolean writeRequestBody(GenerationContext context, OperationShape operation) {
Expand Down Expand Up @@ -237,7 +240,7 @@ private boolean writeRequestBody(GenerationContext context, OperationShape opera
* <p>For example:
*
* <pre>{@code
* headers['foo'] = "This is a custom header";
* "foo": "This is a custom header",
* }</pre>
*
* @param context The generation context.
Expand Down