Skip to content

Commit

Permalink
Request compression as application transport
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp committed Aug 15, 2023
1 parent f5c5592 commit 107e4a3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 100 deletions.
2 changes: 1 addition & 1 deletion codegen/projections/white_label/spec/compression_spec.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 @@ -24,10 +24,15 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.HttpChecksumRequiredTrait;
import software.amazon.smithy.model.traits.HttpTrait;
import software.amazon.smithy.model.traits.RequestCompressionTrait;
import software.amazon.smithy.ruby.codegen.config.ClientConfig;
import software.amazon.smithy.ruby.codegen.config.RangeConstraint;
import software.amazon.smithy.ruby.codegen.middleware.Middleware;
import software.amazon.smithy.ruby.codegen.middleware.MiddlewareStackStep;
import software.amazon.smithy.ruby.codegen.util.Streaming;
Expand Down Expand Up @@ -145,6 +150,71 @@ public static ApplicationTransport createDefaultHttpApplicationTransport() {
.build()
);

String disableRequestCompressionDocumentation = """
When set to 'true' the request body will not be compressed for supported operations.
""";

ClientConfig disableRequestCompression = ClientConfig.builder()
.name("disable_request_compression")
.type("Boolean")
.defaultPrimitiveValue("false")
.documentation(disableRequestCompressionDocumentation)
.allowOperationOverride()
.build();

String minCompressionDocumentation = """
The minimum size bytes that triggers compression for request bodies.
The value must be non-negative integer value between 0 and 10485780 bytes inclusive.
""";

ClientConfig requestMinCompressionSizeBytes = ClientConfig.builder()
.name("request_min_compression_size_bytes")
.type("Integer")
.documentation(minCompressionDocumentation)
.allowOperationOverride()
.defaultPrimitiveValue("10240")
.constraint(new RangeConstraint(0, 10485760))
.build();

Middleware.Builder compressionBuilder = Middleware.builder()
.operationPredicate(
((model, service, operation) -> operation.hasTrait(RequestCompressionTrait.class)))
.operationParams((ctx, operation) -> {
Map<String, String> params = new HashMap<>();
RequestCompressionTrait requestCompression =
operation.expectTrait(RequestCompressionTrait.class);
Shape inputShape = ctx.model().expectShape(operation.getInputShape());

params.put("encodings", "[" + requestCompression
.getEncodings()
.stream()
.map((s) -> "'" + s + "'")
.collect(Collectors.joining(", ")) + "]");

params.put("streaming",
Streaming.isStreaming(ctx.model(), inputShape) ? "true" : "false");

return params;
})
.klass("Hearth::HTTP::Middleware::RequestCompression")
.step(MiddlewareStackStep.AFTER_BUILD);
// commented out since Middleware Relative needs an update to handle this case
// .relative(new Middleware.Relative(Middleware.Relative.Type.BEFORE,
// "Hearth::HTTP::Middleware::ContentMD5"))


TopDownIndex topDownIndex = TopDownIndex.of(context.model());
Set<OperationShape> containedOperations = topDownIndex.getContainedOperations(context.service());
boolean hasCompression =
containedOperations.stream().anyMatch((o) -> o.hasTrait(RequestCompressionTrait.class));

if (hasCompression) {
compressionBuilder.addConfig(disableRequestCompression);
compressionBuilder.addConfig(requestMinCompressionSizeBytes);
}

middleware.add(compressionBuilder.build());

middleware.add(Middleware.builder()
.klass(Hearth.PARSE_MIDDLEWARE)
.step(MiddlewareStackStep.PARSE)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
software.amazon.smithy.ruby.codegen.integrations.TestIntegration
software.amazon.smithy.ruby.codegen.integrations.RequestCompression
software.amazon.smithy.ruby.codegen.integrations.TestIntegration

0 comments on commit 107e4a3

Please sign in to comment.