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 CBOR to always have an accept header #219

Merged
merged 9 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions codegen/projections/rpcv2_cbor/lib/rpcv2_cbor/builders.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public BuilderGeneratorBase(GenerationContext context) {
*
* @param operation the operation to generate for.
* @param inputShape the operation's input.
* @param isEventStream true when the operations input has an event stream.
* @param isEventStream true when the operations input has an event stream.
*/
protected abstract void renderOperationBuildMethod(OperationShape operation, Shape inputShape,
boolean isEventStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import java.util.stream.Stream;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.shapes.BlobShape;
import software.amazon.smithy.model.shapes.ListShape;
import software.amazon.smithy.model.shapes.MapShape;
Expand Down Expand Up @@ -69,9 +70,7 @@ protected void renderOperationBuildMethod(OperationShape operation, Shape inputS
.write("http_req.http_method = 'POST'")
.write("http_req.append_path('/service/$L/operation/$L')",
context.service().getId().getName(), operation.getId().getName())
.call(() -> {
renderContentTypeHeader(operation, isEventStream);
})
.call(() -> renderHeaders(operation, isEventStream))
.call(() -> {
if (isEventStream) {
renderEventStreamInitialRequestMessage(inputShape);
Expand Down Expand Up @@ -120,15 +119,27 @@ private void renderEventStreamInitialRequestMessage(Shape inputShape) {
}
}

private void renderContentTypeHeader(OperationShape operation, boolean isEventStream) {
if (isEventStream) {
writer.write("http_req.headers['Content-Type'] = 'application/vnd.amazon.eventstream'");
if (Streaming.isEventStreaming(model, model.expectShape(operation.getOutputShape()))) {
writer.write("http_req.headers['Accept'] = 'application/vnd.amazon.eventstream'");
private void renderHeaders(OperationShape operation, boolean isEventStream) {
writer.write("http_req.headers['Smithy-Protocol'] = 'rpc-v2-cbor'");

// Only modeled inputs should have this header
if (OperationIndex.of(model).getInput(operation).isPresent()) {
String contentTypeHeader;
if (isEventStream) {
contentTypeHeader = "application/vnd.amazon.eventstream";
} else {
contentTypeHeader = "application/cbor";
}
writer.write("http_req.headers['Content-Type'] = '$L'", contentTypeHeader);
}

String acceptHeader;
if (Streaming.isEventStreaming(model, model.expectShape(operation.getOutputShape()))) {
acceptHeader = "application/vnd.amazon.eventstream";
} else {
writer.write("http_req.headers['Smithy-Protocol'] = 'rpc-v2-cbor'");
acceptHeader = "application/cbor";
}
writer.write("http_req.headers['Accept'] = '$L'", acceptHeader);
}

@Override
Expand Down
Loading