Skip to content
Open
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 @@ -5,12 +5,12 @@
"src/features_client/__init__.py": "32592f5725712e19c306d0c817040e64c67203ae420e3fac2a05301915b742aa",
"src/features_client/_private/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"src/features_client/_private/endpoints.py": "90e1bc94c3ca6338b495b94870e7b1e433c1c2da064e336a85ad94616953916a",
"src/features_client/_private/schemas.py": "dd35b878aad2db78ea2903357eb190b596acc7c7723d3b49488ca3e7b360f916",
"src/features_client/_private/schemas.py": "055fa4e576c89b166d54edb62d6356f3a4e5828a048d5227ee3f04b571d44e15",
"src/features_client/aio/__init__.py": "2d6c204a7bbceeed5b175fe0df922722d341068027b29287cfdcbb9cb29403af",
"src/features_client/aio/client.py": "72e1e989866364a4a80f36056e177d67d519d5cab966dbae870f0a10f9f4fb81",
"src/features_client/aio/client.py": "808c30f1ff3b07ac01e4f45a04a27b97eca06d7611eb5c52aad59c613c45b58b",
"src/features_client/aio/config.py": "0b8ab6e0648b479de7a9befe6742fbe72865bee2fab11a4533fbede79516e99a",
"src/features_client/aio/user_agent.py": "05cbb5932a96cb83d55ece172f9b9b5b8aeb625782140647e177d5d5cbd18dd5",
"src/features_client/client.py": "43ec0fe0d6616d2d8f3219e481997b773cbd3e93035f8a6965ce04b652b14448",
"src/features_client/client.py": "710e5944b26ad6504db092912aca631c5c4516cb1589d534c2c0f1a66befd0b7",
"src/features_client/config.py": "8852d5679ab46123361451298ec619ee6506c6d0488fad2f940a29972e1cc755",
"src/features_client/models.py": "27b38abaebb0f568b92052d2c42dee9f804fcb00f15b25ef2c2f226e2d89ec96",
"src/features_client/models.pyi": "b0c577481ee04ac11b91b85eee3cf94de8eebbd977d95025b87736f3bc8c33f3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@
_s65 = "Ping"
_s66 = "PutNote"
_s67 = "PutThing"
_s68 = "FieldNotes"
_s69 = "aws.api#service"
_s70 = "smithy.protocols#rpcv2Cbor"
_s68 = "smithy.api#requestCompression"
_s69 = "FieldNotes"
_s70 = "aws.api#service"
_s71 = "smithy.protocols#rpcv2Cbor"

DESCRIBE_NOTE_INPUT: StaticSchema = (
18,
Expand Down Expand Up @@ -401,7 +402,10 @@
23,
n0,
_s67,
{_s64: MappingProxyType({"Kind": MappingProxyType({"value": "put"})})},
{
_s68: MappingProxyType({"encodings": ("gzip",)}),
_s64: MappingProxyType({"Kind": MappingProxyType({"value": "put"})}),
},
PUT_THING_INPUT,
PUT_THING_OUTPUT,
(),
Expand All @@ -410,6 +414,6 @@
FIELD_NOTES: StaticSchema = (
21,
n0,
_s68,
{_s69: MappingProxyType({"sdkId": "Field Notes"}), _s70: None},
_s69,
{_s70: MappingProxyType({"sdkId": "Field Notes"}), _s71: None},
)
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ async def put_thing(
**kwargs: Unpack[models.PutThingInputDict],
) -> models.PutThingOutput:
"""
Needs no auth.
Needs no auth. Compresses its request body with gzip.

Args:
plugins:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def put_thing(
**kwargs: Unpack[models.PutThingInputDict],
) -> models.PutThingOutput:
"""
Needs no auth.
Needs no auth. Compresses its request body with gzip.

Args:
plugins:
Expand Down
7 changes: 7 additions & 0 deletions designs/codegen/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ When an operation has the `smithy.api#endpoint` trait, the config gets
`HostPrefixIntegration`. The runtime prepends each operation's expanded host
prefix to its endpoint host unless the field is true.

When an operation has the `smithy.api#requestCompression` trait, a non-AWS
config gets `disable_request_compression: bool`, default `False`, and
`request_min_compression_size_bytes: int`, default `10240`, as in the Java
generator's `RequestCompressionIntegration`. AWS configs inherit both from
`AwsConfig`, which also resolves them from the environment and profile. The
runtime compresses requests; the generator only declares the fields.

Schemas retain `smithy.rules#contextParam`, `smithy.rules#staticContextParams`,
and `smithy.rules#operationContextParams`, which the runtime parameter plan
reads to bind input members and operation values.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "feature",
"description": "Generated non-AWS clients whose operations have the smithy.api#requestCompression trait get the disable_request_compression and request_min_compression_size_bytes config fields."
}
43 changes: 43 additions & 0 deletions packages/smithy-python/src/smithy_python/_client_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def write_client(
operation.operation.has_trait(_ENDPOINT)
for operation in plan.operations
),
request_compression=any(
operation.operation.has_trait(_REQUEST_COMPRESSION)
for operation in plan.operations
),
),
integrations,
)
Expand Down Expand Up @@ -311,11 +315,30 @@ def _modes(
"endpoint host.",
)
"""The name and documentation of the config field that turns off host prefixes."""
_REQUEST_COMPRESSION: Final = _model.ShapeId("smithy.api", "requestCompression")
# The name, type, default and documentation of each request compression field. AWS
# configs inherit them from AwsConfig, which also resolves them from the
# environment and profile.
REQUEST_COMPRESSION_FIELDS: Final = (
(
"disable_request_compression",
"bool",
"False",
"Whether to skip compressing requests of operations that support it.",
),
(
"request_min_compression_size_bytes",
"int",
"10240",
"The smallest request body, in bytes, that is compressed.",
),
)
_CONFIG_FIELDS: Final = frozenset(
{
"auth_scheme_resolver",
"auth_schemes",
HOST_PREFIX_FIELD[0],
*(name for name, *_ in REQUEST_COMPRESSION_FIELDS),
"endpoint_resolver",
"endpoint_uri",
"interceptors",
Expand Down Expand Up @@ -465,6 +488,11 @@ def _write_core_config(writer: PythonWriter, config: ConfigPlan) -> None:
if config.host_prefix
else ()
),
*(
(name, (kind,), docs)
for name, kind, _, docs in REQUEST_COMPRESSION_FIELDS
if config.request_compression
),
)
writer.line("@", _DATACLASS, "(init=False)")
writer.line("class ", mode.config, ":")
Expand Down Expand Up @@ -504,6 +532,9 @@ def _write_core_config(writer: PythonWriter, config: ConfigPlan) -> None:
writer.line(field.attribute, ": ", field.type, " | None = None,")
if config.host_prefix:
writer.line(HOST_PREFIX_FIELD[0], ": bool | None = None,")
if config.request_compression:
for name, kind, _, _ in REQUEST_COMPRESSION_FIELDS:
writer.line(name, ": ", kind, " | None = None,")
writer.line(") -> None:")
with writer.indent():
for field in auth_fields:
Expand Down Expand Up @@ -563,6 +594,18 @@ def _write_core_config(writer: PythonWriter, config: ConfigPlan) -> None:
writer.line("self.", field.attribute, " = ", field.attribute)
if config.host_prefix:
writer.line("self.", HOST_PREFIX_FIELD[0], " = ", HOST_PREFIX_FIELD[0])
if config.request_compression:
for name, _, default, _ in REQUEST_COMPRESSION_FIELDS:
writer.line(
"self.",
name,
" = ",
default,
" if ",
name,
" is None else ",
name,
)
if schemes:
writer.line()
writer.line(
Expand Down
4 changes: 4 additions & 0 deletions packages/smithy-python/src/smithy_python/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class ConfigPlan:
"""Whether an operation has an endpoint host prefix, so the config has
``disable_host_prefix_injection``."""

request_compression: bool = False
"""Whether an operation has the ``requestCompression`` trait, so the config has
``disable_request_compression`` and ``request_min_compression_size_bytes``."""


class ClientIntegration:
"""Changes the clients generated for the services it matches.
Expand Down
27 changes: 27 additions & 0 deletions packages/smithy-python/tests/unit/test_client_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,33 @@ def test_configs_can_turn_off_modeled_host_prefixes(
assert hosts == ["data.example.com", "example.com"]


def test_configs_get_request_compression_fields_when_an_operation_compresses(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
_generate(tmp_path / "plain", "greeter_uncompressed")
config = (tmp_path / "plain/src/greeter_uncompressed/config.py").read_text()
assert "request_compression" not in config

shapes = _shapes()
shapes["example#Ping"].setdefault("traits", {})["smithy.api#requestCompression"] = {
"encodings": ["gzip"]
}
_generate(tmp_path / "gzip", "greeter_compressed", shapes)
monkeypatch.syspath_prepend( # pyright: ignore[reportUnknownMemberType]
str(tmp_path / "gzip/src")
)
package = importlib.import_module("greeter_compressed")
for aio in (False, True):
config = _config(package, _Transport(), aio=aio)
assert config.disable_request_compression is False
assert config.request_min_compression_size_bytes == 10240
config = package.Config(
disable_request_compression=True, request_min_compression_size_bytes=0
)
assert config.disable_request_compression is True
assert config.request_min_compression_size_bytes == 0


def test_rpcv2_json_service_sends_json_and_requires_the_json_extra(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
Expand Down
7 changes: 6 additions & 1 deletion scripts/codegen-output-py/models/features.json
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,12 @@
},
"traits": {
"smithy.api#auth": [],
"smithy.api#documentation": "Needs no auth.",
"smithy.api#documentation": "Needs no auth. Compresses its request body with gzip.",
"smithy.api#requestCompression": {
"encodings": [
"gzip"
]
},
"smithy.rules#staticContextParams": {
"Kind": {
"value": "put"
Expand Down
3 changes: 2 additions & 1 deletion scripts/codegen-output-py/models/features.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ operation PutNote {
}
}

/// Needs no auth.
/// Needs no auth. Compresses its request body with gzip.
@auth([])
@requestCompression(encodings: ["gzip"])
@staticContextParams(Kind: { value: "put" })
operation PutThing {
input := {
Expand Down
Loading