From e53d51d817cf814f3e127d8aeddd1922f5fb53f3 Mon Sep 17 00:00:00 2001 From: Sean O'Brien <60306702+stobrien89@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:40:57 -0500 Subject: [PATCH] Generate AWS configs and user agents for Python clients --- .../.smithy-python-manifest.json | 21 + codegen-output-py/aws-config-client/README.md | 5 + .../aws-config-client/pyproject.toml | 38 ++ .../src/aws_config_client/__init__.py | 30 ++ .../aws_config_client/_private/__init__.py | 0 .../aws_config_client/_private/endpoints.py | 27 ++ .../src/aws_config_client/_private/schemas.py | 53 +++ .../src/aws_config_client/aio/__init__.py | 19 + .../src/aws_config_client/aio/client.py | 53 +++ .../src/aws_config_client/aio/config.py | 109 +++++ .../src/aws_config_client/aio/user_agent.py | 16 + .../src/aws_config_client/client.py | 53 +++ .../src/aws_config_client/config.py | 104 +++++ .../src/aws_config_client/models.py | 61 +++ .../src/aws_config_client/models.pyi | 56 +++ .../src/aws_config_client/py.typed | 0 .../src/aws_config_client/user_agent.py | 16 + designs/codegen/client.md | 61 ++- ...ture-0a9753270aa945d28a3e6d026738d1c5.json | 4 + .../src/smithy_python/_client_emitter.py | 171 +++++--- .../src/smithy_python/_endpoint_tests.py | 8 +- .../src/smithy_python/aws/__init__.py | 20 + .../src/smithy_python/aws/_config.py | 396 ++++++++++++++++++ .../src/smithy_python/aws/_dependencies.py | 19 + .../smithy-python/src/smithy_python/cli.py | 3 +- .../src/smithy_python/clients.py | 20 +- .../src/smithy_python/integrations.py | 134 ++++++ .../tests/unit/test_client_generation.py | 69 +++ scripts/api-snapshot/api-rex.json | 8 + scripts/api-snapshot/api.json | 9 + .../codegen-output-py/client-projections.json | 7 + .../codegen-output-py/models/aws-config.json | 132 ++++++ .../models/aws-config.smithy | 39 ++ scripts/codegen-output-py/pyrightconfig.json | 1 + .../tests/test_client_snapshots.py | 91 ++++ scripts/verifications/import-cost.json | 2 +- 36 files changed, 1787 insertions(+), 68 deletions(-) create mode 100644 codegen-output-py/aws-config-client/.smithy-python-manifest.json create mode 100644 codegen-output-py/aws-config-client/README.md create mode 100644 codegen-output-py/aws-config-client/pyproject.toml create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/__init__.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/_private/__init__.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/_private/endpoints.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/_private/schemas.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/aio/__init__.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/aio/client.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/aio/config.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/aio/user_agent.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/client.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/config.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/models.py create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/models.pyi create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/py.typed create mode 100644 codegen-output-py/aws-config-client/src/aws_config_client/user_agent.py create mode 100644 packages/smithy-python/.changes/next-release/smithy-python-feature-0a9753270aa945d28a3e6d026738d1c5.json create mode 100644 packages/smithy-python/src/smithy_python/aws/__init__.py create mode 100644 packages/smithy-python/src/smithy_python/aws/_config.py create mode 100644 packages/smithy-python/src/smithy_python/aws/_dependencies.py create mode 100644 packages/smithy-python/src/smithy_python/integrations.py create mode 100644 scripts/codegen-output-py/models/aws-config.json create mode 100644 scripts/codegen-output-py/models/aws-config.smithy diff --git a/codegen-output-py/aws-config-client/.smithy-python-manifest.json b/codegen-output-py/aws-config-client/.smithy-python-manifest.json new file mode 100644 index 000000000..0c11ee822 --- /dev/null +++ b/codegen-output-py/aws-config-client/.smithy-python-manifest.json @@ -0,0 +1,21 @@ +{ + "files": { + "README.md": "fd9e871f95d184d149ce23c2a4cd0eb77dffebac24d83f19fd0333b38329f665", + "pyproject.toml": "2d25ffb28e4599769791f7453a05fd7665cd5544850add05903dd701516253ab", + "src/aws_config_client/__init__.py": "316c15daeb5ec5c2087ab41af28a36fa149a117ec0f145ed4f0f836bae84cfe6", + "src/aws_config_client/_private/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/aws_config_client/_private/endpoints.py": "7b7fc7e93046c5849dadc9002eccd8a71dca2f0e194a2dbd1158ecca174f6d85", + "src/aws_config_client/_private/schemas.py": "c215a5e2bceb78db677a60e607358cd9e58ca68f379df23c7c2771733b6ee5b7", + "src/aws_config_client/aio/__init__.py": "0a179e5a12e1d0aa9e7d4307ac0c0c00526cebb5145cc9b0b35e69d41aaada67", + "src/aws_config_client/aio/client.py": "e0939941df6eef91aca4b5b33fbd84b9cf68b4bc903d786d96e1678e47abb796", + "src/aws_config_client/aio/config.py": "d17750691ea420996bb2ddfbb5f09d5bd8397346b0c0ceb7ea5bf6b14185bd10", + "src/aws_config_client/aio/user_agent.py": "36d50d3e1ef1b524f6c137eb377b7d7a6905e069b5f784074017664a3c65852e", + "src/aws_config_client/client.py": "6bff2d04a812609e3d12fc36a06fb80f4fb94686328b40ca88ba71694509f9e5", + "src/aws_config_client/config.py": "f652b5f8975bab09486a0d862d6c5c5d2df9b20816798f13c9e7f987d965e4c9", + "src/aws_config_client/models.py": "63766a511b14b4fdb2dba4a5db834016aa14001cde495a4960ac0ab6f4f601b0", + "src/aws_config_client/models.pyi": "a378f899f5dd91d9353081c0d312306b411c3e926b26ace7cdb46908f2af2b70", + "src/aws_config_client/py.typed": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "src/aws_config_client/user_agent.py": "d07f98802cecab19abca8bde1853ff9d8fa8ce9ef471abf73b7b4d56abad7a36" + }, + "version": 1 +} diff --git a/codegen-output-py/aws-config-client/README.md b/codegen-output-py/aws-config-client/README.md new file mode 100644 index 000000000..1d8cdf0a0 --- /dev/null +++ b/codegen-output-py/aws-config-client/README.md @@ -0,0 +1,5 @@ + + +## aws_config_client Client Package + +Python client for the AWS config fixture diff --git a/codegen-output-py/aws-config-client/pyproject.toml b/codegen-output-py/aws-config-client/pyproject.toml new file mode 100644 index 000000000..228725111 --- /dev/null +++ b/codegen-output-py/aws-config-client/pyproject.toml @@ -0,0 +1,38 @@ +# Code generated by smithy-python-codegen DO NOT EDIT. + +[project] +name = "aws_config_client" +version = "0.0.1" +description = "Python client for the AWS config fixture" +readme = "README.md" +requires-python = ">=3.12" +keywords = ["smithy", "aws_config_client"] +license = {text = "Apache-2.0"} +dependencies = [ + "smithy-aws-core~=0.16.0", + "smithy-core~=0.13.0", + "smithy-http[aiohttp,cbor,urllib3]~=0.10.0", +] + +[build-system] +requires = [ + "hatchling", +] +build-backend = "hatchling.build" + +[tool.hatch.build.targets.wheel] +packages = [ + "src/aws_config_client", +] + +[tool.pyright] +typeCheckingMode = "strict" + +[tool.ruff] +target-version = "py312" + +[tool.ruff.lint] +ignore = ["F841"] + +[tool.ruff.format] +skip-magic-trailing-comma = true diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/__init__.py b/codegen-output-py/aws-config-client/src/aws_config_client/__init__.py new file mode 100644 index 000000000..1886830e7 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/__init__.py @@ -0,0 +1,30 @@ +from importlib import import_module +from typing import TYPE_CHECKING, Any + +__version__: str = "0.0.1" + +if TYPE_CHECKING: + from .aio.client import AsyncWeatherStationClient + from .aio.config import AsyncWeatherStationConfig + from .client import WeatherStationClient + from .config import WeatherStationConfig + +__all__ = [ + "AsyncWeatherStationClient", + "AsyncWeatherStationConfig", + "WeatherStationClient", + "WeatherStationConfig", +] + +_EXPORTS = { + "WeatherStationClient": ".client", + "WeatherStationConfig": ".config", + "AsyncWeatherStationClient": ".aio.client", + "AsyncWeatherStationConfig": ".aio.config", +} + + +def __getattr__(name: str) -> Any: + if name in _EXPORTS: + return getattr(import_module(_EXPORTS[name], __name__), name) + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/_private/__init__.py b/codegen-output-py/aws-config-client/src/aws_config_client/_private/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/_private/endpoints.py b/codegen-output-py/aws-config-client/src/aws_config_client/_private/endpoints.py new file mode 100644 index 000000000..973ba8ee5 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/_private/endpoints.py @@ -0,0 +1,27 @@ +from smithy_core.endpoints import EndpointBdd + +# fmt: off +# Tags: ref=0, attr=1, call=2, list=3, map=4, error=5, endpoint=6. +BDD = EndpointBdd( + # (name, type, required, default, builtin, client_context_attribute) + parameters=( + ("Endpoint", "string", False, None, "SDK::Endpoint", None), + ), + functions=("isSet",), + conditions=( + ((2, "isSet", (0, "Endpoint")), None), # 0: isSet(Endpoint) + ), + results=( + None, # 0: no match + (6, (0, "Endpoint"), None, ()), # 1 + (5, "Endpoint is not set"), # 2 + ), + root=2, + # (condition, high, low); 1 and -1 are terminals, a negative reference is a + # complement edge, and 100_000_000 + k is result k. + nodes=( + -1, 1, -1, + 0, 100_000_001, 100_000_002, + ), +) +# fmt: on diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/_private/schemas.py b/codegen-output-py/aws-config-client/src/aws_config_client/_private/schemas.py new file mode 100644 index 000000000..711777f98 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/_private/schemas.py @@ -0,0 +1,53 @@ +from types import MappingProxyType + +from smithy_core._schema_compact import StaticOperationSchema, StaticSchema + +n0 = "example.awsconfig" +_s0 = "GetForecastInput" +_s1 = "smithy.synthetic#originalShapeId" +_s2 = "example.awsconfig#GetForecastInput" +_s3 = "smithy.api#input" +_s4 = "city" +_s5 = "GetForecastOutput" +_s6 = "example.awsconfig#GetForecastOutput" +_s7 = "smithy.api#output" +_s8 = "summary" +_s9 = "GetForecast" +_s10 = "WeatherStation" +_s11 = "aws.api#service" +_s12 = "smithy.protocols#rpcv2Cbor" + +GET_FORECAST_INPUT: StaticSchema = ( + 18, + n0, + _s0, + {_s1: _s2, _s3: None}, + (_s4,), + ((3, 0),), +) + +GET_FORECAST_OUTPUT: StaticSchema = ( + 18, + n0, + _s5, + {_s1: _s6, _s7: None}, + (_s8,), + ((3, 0),), +) + +GET_FORECAST: StaticOperationSchema = ( + 23, + n0, + _s9, + 0, + GET_FORECAST_INPUT, + GET_FORECAST_OUTPUT, + (), +) + +WEATHER_STATION: StaticSchema = ( + 21, + n0, + _s10, + {_s11: MappingProxyType({"sdkId": "Weather Station"}), _s12: None}, +) diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/aio/__init__.py b/codegen-output-py/aws-config-client/src/aws_config_client/aio/__init__.py new file mode 100644 index 000000000..c70a641a6 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/aio/__init__.py @@ -0,0 +1,19 @@ +from importlib import import_module +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from .client import AsyncWeatherStationClient + from .config import AsyncWeatherStationConfig + +__all__ = ["AsyncWeatherStationClient", "AsyncWeatherStationConfig"] + +_EXPORTS = { + "AsyncWeatherStationClient": ".client", + "AsyncWeatherStationConfig": ".config", +} + + +def __getattr__(name: str) -> Any: + if name in _EXPORTS: + return getattr(import_module(_EXPORTS[name], __name__), name) + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/aio/client.py b/codegen-output-py/aws-config-client/src/aws_config_client/aio/client.py new file mode 100644 index 000000000..ace077e17 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/aio/client.py @@ -0,0 +1,53 @@ +from __future__ import annotations + +from typing import Unpack, overload + +from smithy_core.aio.client import AsyncClient +from smithy_http.plugins import user_agent_plugin + +from aws_config_client import models +from aws_config_client.aio.config import AsyncWeatherStationConfig, Plugin +from aws_config_client.aio.user_agent import aws_user_agent_plugin + + +class AsyncWeatherStationClient(AsyncClient[AsyncWeatherStationConfig]): + "An AWS service that reads its config from the environment and profiles." + + _client_plugins = (aws_user_agent_plugin, user_agent_plugin) + + @overload + async def get_forecast( + self, + request: models.GetForecastInput | models.GetForecastInputDict, + /, + *, + plugins: list[Plugin] | None = None, + ) -> models.GetForecastOutput: ... + + @overload + async def get_forecast( + self, + /, + *, + plugins: list[Plugin] | None = None, + **kwargs: Unpack[models.GetForecastInputDict], + ) -> models.GetForecastOutput: + """ + Gets the forecast for a city. + + :param plugins: Callables that change the configuration for this call + only. + """ + + async def get_forecast( + self, /, *args: object, plugins: list[Plugin] | None = None, **kwargs: object + ) -> models.GetForecastOutput: + return await self._invoke( + self._select_input(models.GET_FORECAST, args, kwargs), + models.GET_FORECAST, + [], + plugins, + ) + + async def _new_config(self) -> AsyncWeatherStationConfig: + return await AsyncWeatherStationConfig.resolve() diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/aio/config.py b/codegen-output-py/aws-config-client/src/aws_config_client/aio/config.py new file mode 100644 index 000000000..4347c321d --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/aio/config.py @@ -0,0 +1,109 @@ +from collections.abc import Callable +from dataclasses import dataclass, field +from typing import Any, ClassVar, Self, Unpack + +from smithy_aws_core.aio.config.aws_config import ( + AsyncAwsConfig, + AsyncAwsConfigOverrides, +) +from smithy_aws_core.aio.config.filesystem import AsyncFileSystem +from smithy_aws_core.config.resolvers import EndpointUriResolver +from smithy_aws_core.config.types import FieldSpec +from smithy_core.aio.interfaces import AsyncEndpointResolver +from smithy_core.aio.interfaces.auth import AsyncAuthScheme +from smithy_core.auth import DefaultAuthResolver +from smithy_core.endpoints import BddEndpointResolver +from smithy_core.interceptors import Interceptor +from smithy_core.interfaces import ( + ClientProtocol, + EndpointResolver, + ProtocolConstructor, + ProtocolSettings, +) +from smithy_core.interfaces.auth import AuthSchemeResolver +from smithy_core.shapes import ShapeID +from smithy_http.aio.aiohttp import AIOHTTPClient +from smithy_http.aio.protocols import AsyncRpcV2CborClientProtocol + +from aws_config_client._private.endpoints import BDD +from aws_config_client._private.schemas import WEATHER_STATION + +_PROTOCOL_SETTINGS = ProtocolSettings( + namespace="example.awsconfig", + service_target="WeatherStation", + version="2026-11-19", + service_schema=WEATHER_STATION, +) + + +class _AsyncWeatherStationConfigOverrides(AsyncAwsConfigOverrides, total=False): + "Explicit field values AsyncWeatherStationConfig.resolve accepts." + + endpoint_resolver: EndpointResolver | AsyncEndpointResolver | None + protocol: ( + ClientProtocol[Any, Any] | ProtocolConstructor[ClientProtocol[Any, Any]] | None + ) + auth_schemes: dict[ShapeID, AsyncAuthScheme[Any, Any, Any, Any]] | None + auth_scheme_resolver: AuthSchemeResolver | None + + +@dataclass(kw_only=True, repr=False, init=False) +class AsyncWeatherStationConfig(AsyncAwsConfig): + "Configuration for AsyncWeatherStationClient." + + interceptors: list[Interceptor[Any, Any, Any, Any]] = field( + default_factory=list[Interceptor[Any, Any, Any, Any]] + ) + "Hooks called during the execution of a request." + + endpoint_resolver: EndpointResolver | AsyncEndpointResolver | None = None + "Resolves the endpoint of each operation." + + protocol: ClientProtocol[Any, Any] | None = None + "Serializes requests and deserializes responses." + + auth_schemes: dict[ShapeID, AsyncAuthScheme[Any, Any, Any, Any]] | None = None + "Auth schemes by auth scheme ID." + + auth_scheme_resolver: AuthSchemeResolver | None = None + "Determines the auth scheme for each operation." + + _FIELDS: ClassVar[dict[str, FieldSpec]] = { + **AsyncAwsConfig._FIELDS, + "endpoint_uri": FieldSpec( + default=None, resolver=EndpointUriResolver("weather_station").program + ), + "endpoint_resolver": FieldSpec( + default_factory=lambda: BddEndpointResolver(BDD) + ), + "protocol": FieldSpec( + default_factory=lambda: AsyncRpcV2CborClientProtocol(_PROTOCOL_SETTINGS), + converter=lambda p: p(_PROTOCOL_SETTINGS) if isinstance(p, type) else p, + ), + "auth_schemes": FieldSpec(default_factory=dict), + "auth_scheme_resolver": FieldSpec(default_factory=DefaultAuthResolver), + "transport": FieldSpec(default_factory=lambda: AIOHTTPClient()), + } + + @classmethod + async def resolve( # pyright: ignore[reportIncompatibleMethodOverride] + cls, + *, + profile: str | None = None, + fs: AsyncFileSystem | None = None, + config_file_path: str | None = None, + credentials_file_path: str | None = None, + **overrides: Unpack[_AsyncWeatherStationConfigOverrides], + ) -> Self: + "Resolve the config from overrides, the environment, and config files." + return await cls._resolve( + profile=profile, + fs=fs, + config_file_path=config_file_path, + credentials_file_path=credentials_file_path, + overrides=overrides, + ) + + +type Plugin = Callable[[AsyncWeatherStationConfig], None] +"A callable that customizes the client configuration." diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/aio/user_agent.py b/codegen-output-py/aws-config-client/src/aws_config_client/aio/user_agent.py new file mode 100644 index 000000000..ec484d1b4 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/aio/user_agent.py @@ -0,0 +1,16 @@ +from smithy_aws_core.interceptors.user_agent import UserAgentInterceptor + +from aws_config_client import __version__ +from aws_config_client.aio.config import AsyncWeatherStationConfig + + +def aws_user_agent_plugin(config: AsyncWeatherStationConfig) -> None: + "Send the AWS user agent with every request." + config.interceptors.append( + UserAgentInterceptor( + ua_suffix=config.user_agent_extra, + ua_app_id=config.sdk_ua_app_id, + sdk_version=__version__, + service_id="Weather_Station", + ) + ) diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/client.py b/codegen-output-py/aws-config-client/src/aws_config_client/client.py new file mode 100644 index 000000000..713796674 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/client.py @@ -0,0 +1,53 @@ +from __future__ import annotations + +from typing import Unpack, overload + +from smithy_core.client import Client +from smithy_http.plugins import user_agent_plugin + +from aws_config_client import models +from aws_config_client.config import Plugin, WeatherStationConfig +from aws_config_client.user_agent import aws_user_agent_plugin + + +class WeatherStationClient(Client[WeatherStationConfig]): + "An AWS service that reads its config from the environment and profiles." + + _client_plugins = (aws_user_agent_plugin, user_agent_plugin) + + @overload + def get_forecast( + self, + request: models.GetForecastInput | models.GetForecastInputDict, + /, + *, + plugins: list[Plugin] | None = None, + ) -> models.GetForecastOutput: ... + + @overload + def get_forecast( + self, + /, + *, + plugins: list[Plugin] | None = None, + **kwargs: Unpack[models.GetForecastInputDict], + ) -> models.GetForecastOutput: + """ + Gets the forecast for a city. + + :param plugins: Callables that change the configuration for this call + only. + """ + + def get_forecast( + self, /, *args: object, plugins: list[Plugin] | None = None, **kwargs: object + ) -> models.GetForecastOutput: + return self._invoke( + self._select_input(models.GET_FORECAST, args, kwargs), + models.GET_FORECAST, + [], + plugins, + ) + + def _new_config(self) -> WeatherStationConfig: + return WeatherStationConfig.resolve() diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/config.py b/codegen-output-py/aws-config-client/src/aws_config_client/config.py new file mode 100644 index 000000000..a389c5ea8 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/config.py @@ -0,0 +1,104 @@ +from collections.abc import Callable +from dataclasses import dataclass, field +from typing import Any, ClassVar, Self, Unpack + +from smithy_aws_core.config.aws_config import AwsConfig, AwsConfigOverrides +from smithy_aws_core.config.filesystem import FileSystem +from smithy_aws_core.config.resolvers import EndpointUriResolver +from smithy_aws_core.config.types import FieldSpec +from smithy_core.auth import DefaultAuthResolver +from smithy_core.endpoints import BddEndpointResolver +from smithy_core.interceptors import Interceptor +from smithy_core.interfaces import ( + ClientProtocol, + EndpointResolver, + ProtocolConstructor, + ProtocolSettings, +) +from smithy_core.interfaces.auth import AuthScheme, AuthSchemeResolver +from smithy_core.shapes import ShapeID +from smithy_http.protocols import RpcV2CborClientProtocol +from smithy_http.urllib3 import Urllib3HTTPClient + +from aws_config_client._private.endpoints import BDD +from aws_config_client._private.schemas import WEATHER_STATION + +_PROTOCOL_SETTINGS = ProtocolSettings( + namespace="example.awsconfig", + service_target="WeatherStation", + version="2026-11-19", + service_schema=WEATHER_STATION, +) + + +class _WeatherStationConfigOverrides(AwsConfigOverrides, total=False): + "Explicit field values WeatherStationConfig.resolve accepts." + + endpoint_resolver: EndpointResolver | None + protocol: ( + ClientProtocol[Any, Any] | ProtocolConstructor[ClientProtocol[Any, Any]] | None + ) + auth_schemes: dict[ShapeID, AuthScheme[Any, Any, Any, Any]] | None + auth_scheme_resolver: AuthSchemeResolver | None + + +@dataclass(kw_only=True, repr=False, init=False) +class WeatherStationConfig(AwsConfig): + "Configuration for WeatherStationClient." + + interceptors: list[Interceptor[Any, Any, Any, Any]] = field( + default_factory=list[Interceptor[Any, Any, Any, Any]] + ) + "Hooks called during the execution of a request." + + endpoint_resolver: EndpointResolver | None = None + "Resolves the endpoint of each operation." + + protocol: ClientProtocol[Any, Any] | None = None + "Serializes requests and deserializes responses." + + auth_schemes: dict[ShapeID, AuthScheme[Any, Any, Any, Any]] | None = None + "Auth schemes by auth scheme ID." + + auth_scheme_resolver: AuthSchemeResolver | None = None + "Determines the auth scheme for each operation." + + _FIELDS: ClassVar[dict[str, FieldSpec]] = { + **AwsConfig._FIELDS, + "endpoint_uri": FieldSpec( + default=None, resolver=EndpointUriResolver("weather_station").program + ), + "endpoint_resolver": FieldSpec( + default_factory=lambda: BddEndpointResolver(BDD) + ), + "protocol": FieldSpec( + default_factory=lambda: RpcV2CborClientProtocol(_PROTOCOL_SETTINGS), + converter=lambda p: p(_PROTOCOL_SETTINGS) if isinstance(p, type) else p, + ), + "auth_schemes": FieldSpec(default_factory=dict), + "auth_scheme_resolver": FieldSpec(default_factory=DefaultAuthResolver), + "transport": FieldSpec(default_factory=lambda: Urllib3HTTPClient()), + } + + @classmethod + def resolve( # pyright: ignore[reportIncompatibleMethodOverride] + cls, + *, + profile: str | None = None, + fs: FileSystem | None = None, + config_file_path: str | None = None, + credentials_file_path: str | None = None, + **overrides: Unpack[_WeatherStationConfigOverrides], + ) -> Self: + "Resolve the config from overrides, the environment, and config files." + return cls._resolve( + profile=profile, + fs=fs, + config_file_path=config_file_path, + credentials_file_path=credentials_file_path, + overrides=overrides, + ) + + +type Plugin = Callable[[WeatherStationConfig], None] +"A callable that customizes the client configuration." diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/models.py b/codegen-output-py/aws-config-client/src/aws_config_client/models.py new file mode 100644 index 000000000..c620e949e --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/models.py @@ -0,0 +1,61 @@ +from smithy_core._dynamic_models import ModelRegistry +from smithy_core.exceptions import ModeledError + +from ._private import schemas + + +class ServiceError(ModeledError): + """ + Base error for all errors in the service. + + Some exceptions do not extend from this class, including synthetic, + implicit, and shared exception types. + """ + + +_MODEL_INDEX = { + "GetForecastInput": "GET_FORECAST_INPUT", + "GetForecastOutput": "GET_FORECAST_OUTPUT", +} + +_RENAMES: dict[str, str] = {} + +_TARGET_NAMES: dict[str, str] = {} + +_OPERATIONS: dict[str, tuple[tuple[str, ...], tuple[str, ...]]] = { + "GET_FORECAST": ( + (), + ("smithy.api#noAuth",), + ), +} + +_MODEL_NAMES = tuple(_MODEL_INDEX) +__all__ = ("ServiceError", *_MODEL_NAMES, *_OPERATIONS) # pyright: ignore[reportUnsupportedDunderAll] + + +_registry = ModelRegistry( + schemas, + renames=_RENAMES, + index=_MODEL_INDEX, + target_names=_TARGET_NAMES, + shape_names=frozenset( + { + "GetForecastInput", + "GetForecastOutput", + } + ), + service_error="ServiceError", +) +del schemas, ModelRegistry, ModeledError + + +def __getattr__(name: str): + if (data := _OPERATIONS.get(name)) is not None: + return globals().setdefault(name, _registry.build_operation(name, *data)) + value = _registry.build(name) + globals()[name] = value + return value + + +def __dir__() -> list[str]: + return sorted({*globals(), *__all__}) diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/models.pyi b/codegen-output-py/aws-config-client/src/aws_config_client/models.pyi new file mode 100644 index 000000000..a06ebc7da --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/models.pyi @@ -0,0 +1,56 @@ +from dataclasses import dataclass +from typing import Self, TypedDict + +from smithy_core.deserializers import ShapeDeserializer +from smithy_core.exceptions import ModeledError +from smithy_core.response import ResponseMetadata +from smithy_core.schemas import APIOperation +from smithy_core.serializers import ShapeSerializer + +__all__ = [ + "GET_FORECAST", + "GetForecastInput", + "GetForecastOutput", + "ServiceError", +] + +class _SerdeShape: + def serialize(self, serializer: ShapeSerializer) -> None: ... + def serialize_members(self, serializer: ShapeSerializer) -> None: ... + @classmethod + def deserialize(cls, deserializer: ShapeDeserializer) -> Self: ... + +class ServiceError(ModeledError): + """ + Base error for all errors in the service. + + Some exceptions do not extend from this class, including synthetic, + implicit, and shared exception types. + """ + +@dataclass(kw_only=True) +class GetForecastInput(_SerdeShape): + "Dataclass for GetForecastInput structure." + + city: str | None = ... + +class GetForecastInputDict(TypedDict, total=False): + "Dictionary form of `GetForecastInput`." + + city: str | None + +@dataclass(kw_only=True) +class GetForecastOutput(_SerdeShape): + "Dataclass for GetForecastOutput structure." + + summary: str | None = ... + response_metadata: ResponseMetadata = ... + """ + Metadata about the response that produced this output. + + Use this to recover the request identifiers a service's support team + needs in order to investigate a call. Members of the metadata are + individually optional. + """ + +GET_FORECAST: APIOperation[GetForecastInput, GetForecastOutput] diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/py.typed b/codegen-output-py/aws-config-client/src/aws_config_client/py.typed new file mode 100644 index 000000000..e69de29bb diff --git a/codegen-output-py/aws-config-client/src/aws_config_client/user_agent.py b/codegen-output-py/aws-config-client/src/aws_config_client/user_agent.py new file mode 100644 index 000000000..14c9fb3f4 --- /dev/null +++ b/codegen-output-py/aws-config-client/src/aws_config_client/user_agent.py @@ -0,0 +1,16 @@ +from smithy_aws_core.interceptors.user_agent import UserAgentInterceptor + +from aws_config_client import __version__ +from aws_config_client.config import WeatherStationConfig + + +def aws_user_agent_plugin(config: WeatherStationConfig) -> None: + "Send the AWS user agent with every request." + config.interceptors.append( + UserAgentInterceptor( + ua_suffix=config.user_agent_extra, + ua_app_id=config.sdk_ua_app_id, + sdk_version=__version__, + service_id="Weather_Station", + ) + ) diff --git a/designs/codegen/client.md b/designs/codegen/client.md index c51505fc1..84aedbe0c 100644 --- a/designs/codegen/client.md +++ b/designs/codegen/client.md @@ -5,8 +5,8 @@ This document describes how the Python generator produces client packages. It defines client settings, operation discovery, I/O normalization, client symbols, the client plan, operation and schema emission, protocol selection, -endpoint rules, endpoint tests, auth, and the generated client and config -modules. +endpoint rules, endpoint tests, auth, integrations, AWS clients, and the +generated client and config modules. ## Goals @@ -283,6 +283,51 @@ its first supported scheme. Generation fails for an operation whose effective schemes include neither `noAuth` nor a supported scheme. +## Integrations + +A `ClientIntegration` changes the clients of the services it matches. The +generator takes integrations in order and applies those that match. Each hook +defaults to no change: + +| Hook | Changes | +| --- | --- | +| `client_names` | The client and config class names | +| `config_fields` | Config fields a client context field must not take | +| `write_config` | The config class, written in place of the core one | +| `new_config` | The callable that creates a default config | +| `client_plugins` | Plugins every client applies, before the core ones | +| `write_files` | Extra modules | + +A hook gets a `ClientMode` for the sync or async variant and, for configs, a +`ConfigPlan` with the endpoint plan, the `BDD` constant, and the supported auth +schemes. The core generator imports no integration. The CLI passes +`smithy_python.aws.AWS_INTEGRATIONS`. + +## AWS Clients + +`smithy_python.aws` holds the AWS integrations. `AwsConfigIntegration` matches a +service with the `aws.api#service` trait and follows the Java generator's AWS +config: + +* The clients are `Client` and `AsyncClient`, and the configs are + `Config` and `AsyncConfig`, with spaces removed from the SDK + ID. +* The configs subclass `AwsConfig` and `AsyncAwsConfig` from `smithy-aws-core`. + They resolve from overrides, environment variables, shared config files, and + defaults through `resolve()`, and direct construction fails. A client without + a config creates one with `resolve()`. +* Each config adds the fields the base lacks, with a `FieldSpec` for each: + `endpoint_resolver`, `protocol`, `auth_schemes`, `auth_scheme_resolver`, auth + scheme fields, and client context fields. `endpoint_uri` also reads + `AWS_ENDPOINT_URL_` and the profile's service section. +* A typed `Unpack` overrides dictionary covers the added fields. +* Clients apply `aws_user_agent_plugin`, which adds `UserAgentInterceptor` + with the SDK ID, the package version, `sdk_ua_app_id`, and + `user_agent_extra`. + +The generated package depends on `smithy-aws-core`. A client context field may +not take an `AwsConfig` field name. + ## Clients and Configuration Service classes are thin subclasses of `Client[Config]` and @@ -345,9 +390,9 @@ are unsigned or use API key auth, and whose endpoint rules use the standard library. Models that require other auth schemes or event or data streams fail generation with a diagnostic naming the operation and feature. -Authentication and endpoint extension points are provider-neutral. AWS -credentials, region defaults, SigV4, query compatibility, AWS endpoint -functions, and AWS user agents are separate integrations. +Authentication and endpoint extension points are provider-neutral. AWS config +resolution and the AWS user agent are AWS integrations. SigV4, AWS endpoint +functions, and AWS protocols are not supported yet. ## Validation @@ -371,6 +416,12 @@ assembled by Smithy into `models/api-key-auth.json`. Its operations use the service's API key auth, `@auth([])`, and `@optionalAuth`. Tests check which requests carry the key and that a missing key fails the call. +The `aws-config-client` snapshot comes from `models/aws-config.smithy`, +assembled by Smithy into `models/aws-config.json` with its ruleset compiled by +`CompileBdd`. Its tests resolve the endpoint from `AWS_ENDPOINT_URL_` +and from a profile, check the AWS user agent, and check that unknown fields and +direct construction fail. + Client snapshots sit alongside the types snapshots and cover the provider-neutral RPCv2 CBOR and RPCv2 JSON projections. Checks cover deterministic regeneration, Ruff, strict Pyright consumer usage, lazy imports, diff --git a/packages/smithy-python/.changes/next-release/smithy-python-feature-0a9753270aa945d28a3e6d026738d1c5.json b/packages/smithy-python/.changes/next-release/smithy-python-feature-0a9753270aa945d28a3e6d026738d1c5.json new file mode 100644 index 000000000..3522bfa50 --- /dev/null +++ b/packages/smithy-python/.changes/next-release/smithy-python-feature-0a9753270aa945d28a3e6d026738d1c5.json @@ -0,0 +1,4 @@ +{ + "type": "feature", + "description": "Generate AWS clients for services with the aws.api#service trait. Their configs resolve from the environment and shared config files, and their clients send the AWS user agent. Add ClientIntegration so integrations can change generated clients." +} \ No newline at end of file diff --git a/packages/smithy-python/src/smithy_python/_client_emitter.py b/packages/smithy-python/src/smithy_python/_client_emitter.py index 690927595..046d89f1b 100644 --- a/packages/smithy-python/src/smithy_python/_client_emitter.py +++ b/packages/smithy-python/src/smithy_python/_client_emitter.py @@ -4,7 +4,7 @@ from __future__ import annotations -from dataclasses import dataclass +from collections.abc import Sequence from typing import TYPE_CHECKING, Final from . import _auth, _endpoint_tests, _endpoints, _package @@ -13,6 +13,7 @@ from . import model as _model from . import models as _models from . import protocols as _protocols +from .integrations import ClientIntegration, ClientMode, ClientNames, ConfigPlan from .symbols import TypeReference if TYPE_CHECKING: @@ -50,20 +51,26 @@ def check_supported(plan: ClientPlan) -> None: ) -def plan_endpoints(plan: ClientPlan) -> _endpoints.EndpointPlan: +def plan_endpoints( + plan: ClientPlan, integrations: Sequence[ClientIntegration] = () +) -> _endpoints.EndpointPlan: """Lower the service's endpoint rules for its client. + :param integrations: The integrations that match the service. :raises CodegenError: If the rules are unsupported, or a client context parameter's config field collides with another config field. """ service = plan.selection.service assert service is not None # noqa: S101 - ClientPlan requires a service - auth_fields = { + reserved = set(_CONFIG_FIELDS) + reserved.update( field.name for scheme in _auth.supported_schemes(service) for field in scheme.fields - } - return _endpoints.plan_endpoints(service, reserved=_CONFIG_FIELDS | auth_fields) + ) + for integration in integrations: + reserved.update(integration.config_fields(service)) + return _endpoints.plan_endpoints(service, reserved=reserved) def write_client( @@ -71,8 +78,12 @@ def write_client( plan: ClientPlan, protocol: _protocols.ProtocolContribution, endpoints: _endpoints.EndpointPlan, + integrations: Sequence[ClientIntegration] = (), ) -> None: - """Register client, config, endpoints, and package modules for both modes.""" + """Register client, config, endpoints, and package modules for both modes. + + :param integrations: The integrations that match the service. + """ import_name = context.settings.import_name directory = import_name.replace(".", "/") service = plan.selection.service @@ -84,18 +95,31 @@ def write_client( ), endpoints, ) - sync, aio = _modes(plan, protocol) + names = ClientNames( + client=plan.symbols.service_name, + config="Config", + async_client=plan.symbols.aio_service_name, + async_config="AsyncConfig", + ) + for integration in integrations: + names = integration.client_names(service, names) + sync, aio = _modes(names, protocol) for mode in (sync, aio): module = f"{import_name}.{mode.directory.replace('/', '.')}" _write_config( context.files.python_module( f"src/{directory}/{mode.directory}config.py", f"{module}config" ), - plan, - mode, - service, - endpoints, - TypeReference("BDD", endpoints_module), + ConfigPlan( + plan=plan, + service=service, + mode=mode, + endpoints=endpoints, + bdd=TypeReference("BDD", endpoints_module), + auth_schemes=_auth.supported_schemes(service), + protocol_settings="_PROTOCOL_SETTINGS", + ), + integrations, ) _write_client( context.files.python_module( @@ -107,13 +131,18 @@ def write_client( mode, f"{module}config", service, + integrations, ) + for integration in integrations: + integration.write_files(context, plan, (sync, aio)) + aio_config = TypeReference(aio.config, f"{import_name}.aio.config") _endpoint_tests.write_endpoint_tests( context, plan, endpoints, TypeReference(aio.client, f"{import_name}.aio.client"), - TypeReference(aio.config, f"{import_name}.aio.config"), + aio_config, + _new_config(aio, aio_config, integrations), ) aio_init = context.files.python_module( f"src/{directory}/aio/__init__.py", f"{import_name}.aio" @@ -180,31 +209,26 @@ def _generic(reference: TypeReference, *arguments: TypeReference) -> TypeReferen ) -@dataclass(frozen=True, slots=True) -class _Mode: - """Names and runtime types that differ between the sync and async client.""" - - directory: str - config: str - client: str - client_base: TypeReference - protocol: TypeReference - transport: TypeReference - transport_type: TypeReference - auth_scheme: TypeReference - endpoint_resolver: tuple[TypeReference, ...] - is_async: bool +def _new_config( + mode: ClientMode, + config: TypeReference, + integrations: Sequence[ClientIntegration], +) -> tuple[str | TypeReference, ...]: + parts: tuple[str | TypeReference, ...] = (config,) + for integration in integrations: + parts = integration.new_config(mode, config) + return parts def _modes( - plan: ClientPlan, protocol: _protocols.ProtocolContribution -) -> tuple[_Mode, _Mode]: + names: ClientNames, protocol: _protocols.ProtocolContribution +) -> tuple[ClientMode, ClientMode]: endpoint_resolver = _core("EndpointResolver", "interfaces") return ( - _Mode( + ClientMode( directory="", - config="Config", - client=plan.symbols.service_name, + config=names.config, + client=names.client, client_base=_core("Client", "client"), protocol=protocol.protocol, transport=protocol.transport, @@ -213,10 +237,10 @@ def _modes( endpoint_resolver=(endpoint_resolver,), is_async=False, ), - _Mode( + ClientMode( directory="aio/", - config="AsyncConfig", - client=plan.symbols.aio_service_name, + config=names.async_config, + client=names.async_client, client_base=_core("AsyncClient", "aio.client"), protocol=protocol.async_protocol, transport=protocol.async_transport, @@ -282,16 +306,12 @@ def _write_exports(writer: PythonWriter, exports: dict[str, str]) -> None: def _write_config( writer: PythonWriter, - plan: ClientPlan, - mode: _Mode, - service: _model.ServiceShape, - endpoints: _endpoints.EndpointPlan, - bdd: TypeReference, + config: ConfigPlan, + integrations: Sequence[ClientIntegration], ) -> None: - client_protocol = _generic(_core("ClientProtocol", "interfaces"), _ANY, _ANY) - interceptor = _generic(_core("Interceptor", "interceptors"), *_ANY4) + plan, service, mode = config.plan, config.service, config.mode schemas = f"{plan.settings.import_name}._private.schemas" - writer.line("_PROTOCOL_SETTINGS = ", _PROTOCOL_SETTINGS, "(") + writer.line(config.protocol_settings, " = ", _PROTOCOL_SETTINGS, "(") with writer.indent(): writer.line("namespace=", _models._quote(service.id.namespace), ",") # pyright: ignore[reportPrivateUsage] writer.line("service_target=", _models._quote(service.id.name), ",") # pyright: ignore[reportPrivateUsage] @@ -305,9 +325,32 @@ def _write_config( writer.line(")") writer.line() writer.line() + if not any( + integration.write_config(writer, config) for integration in integrations + ): + _write_core_config(writer, config) + writer.line() + writer.line() + writer.line("type Plugin = ", _CALLABLE, "[[", mode.config, "], None]") + writer.line( + _models._quote( # pyright: ignore[reportPrivateUsage] + "A callable that customizes the client configuration." + ) + ) + + +def _write_core_config(writer: PythonWriter, config: ConfigPlan) -> None: + service, mode, endpoints, bdd = ( + config.service, + config.mode, + config.endpoints, + config.bdd, + ) + client_protocol = _generic(_core("ClientProtocol", "interfaces"), _ANY, _ANY) + interceptor = _generic(_core("Interceptor", "interceptors"), *_ANY4) endpoint_resolver = _join(mode.endpoint_resolver) auth_schemes = ("dict[", _SHAPE_ID, ", ", mode.auth_scheme, "]") - schemes = _auth.supported_schemes(service) + schemes = config.auth_schemes auth_fields = [field for scheme in schemes for field in scheme.fields] retry = (_RETRY_STRATEGY, " | ", _RETRY_STRATEGY_OPTIONS) fields: tuple[tuple[str, tuple[str | TypeReference, ...], str], ...] = ( @@ -468,14 +511,18 @@ def _write_config( ) ) writer.line("self.auth_schemes[scheme.scheme_id] = scheme") - writer.line() - writer.line() - writer.line("type Plugin = ", _CALLABLE, "[[", mode.config, "], None]") - writer.line( - _models._quote( # pyright: ignore[reportPrivateUsage] - "A callable that customizes the client configuration." - ) - ) + + +def _items(references: tuple[TypeReference, ...]) -> tuple[str | TypeReference, ...]: + # The items of a tuple literal, with the trailing comma a single item needs. + parts: list[str | TypeReference] = [] + for reference in references: + if parts: + parts.append(", ") + parts.append(reference) + if len(references) == 1: + parts.append(",") + return tuple(parts) def _join(references: tuple[TypeReference, ...]) -> tuple[str | TypeReference, ...]: @@ -490,9 +537,10 @@ def _join(references: tuple[TypeReference, ...]) -> tuple[str | TypeReference, . def _write_client( writer: PythonWriter, plan: ClientPlan, - mode: _Mode, + mode: ClientMode, config_module: str, service: _model.ServiceShape, + integrations: Sequence[ClientIntegration], ) -> None: config = TypeReference(mode.config, config_module) plugin = TypeReference("Plugin", config_module) @@ -505,21 +553,30 @@ def _write_client( or f"Client for {service.id.name}.", ) writer.line() - writer.line("_client_plugins = (", _USER_AGENT_PLUGIN, ",)") + plugins = [ + reference + for integration in integrations + for reference in integration.client_plugins( + plan, mode, plan.settings.import_name + ) + ] + writer.line("_client_plugins = (", *_items((*plugins, _USER_AGENT_PLUGIN)), ")") for operation, export in zip( plan.operations, plan.models.operations, strict=True ): _write_operation(writer, plan, mode, operation, export.name, models, plugin) writer.line() - writer.line("def _new_config(self) -> ", config, ":") + create = _new_config(mode, config, integrations) + define = "async def " if create[0] == "await " else "def " + writer.line(define, "_new_config(self) -> ", config, ":") with writer.indent(): - writer.line("return ", config, "()") + writer.line("return ", *create, "()") def _write_operation( writer: PythonWriter, plan: ClientPlan, - mode: _Mode, + mode: ClientMode, operation: OperationPlan, constant: str, models: TypeReference, diff --git a/packages/smithy-python/src/smithy_python/_endpoint_tests.py b/packages/smithy-python/src/smithy_python/_endpoint_tests.py index 79bce3e32..510cfcd6e 100644 --- a/packages/smithy-python/src/smithy_python/_endpoint_tests.py +++ b/packages/smithy-python/src/smithy_python/_endpoint_tests.py @@ -35,11 +35,14 @@ def write_endpoint_tests( endpoints: _endpoints.EndpointPlan, client: TypeReference, config: TypeReference, + new_config: tuple[str | TypeReference, ...] | None = None, ) -> None: """Register ``tests/test_endpoints.py`` when the service has endpoint tests. :param client: The async client class. :param config: The async config class. + :param new_config: The callable that creates an async config from keyword + fields. Defaults to the config class. :raises CodegenError: If a case names an operation the service doesn't contain, expects neither an endpoint nor an error, or gives an operation an input member it doesn't have. @@ -64,6 +67,7 @@ def write_endpoint_tests( endpoints, client, config, + new_config or (config,), ).write(service, tuple(_object(case, "test case") for case in cases)) @@ -137,8 +141,10 @@ def __init__( endpoints: _endpoints.EndpointPlan, client: TypeReference, config: TypeReference, + new_config: tuple[str | TypeReference, ...], ) -> None: self._writer = writer + self._new_config = new_config self._plan = plan self._client = client self._config = config @@ -174,7 +180,7 @@ def _write_factories(self) -> None: with w.indent(): w.line( "return ", - self._config, + *self._new_config, "(**fields, retry_strategy=", _RETRY, "(max_attempts=1))", diff --git a/packages/smithy-python/src/smithy_python/aws/__init__.py b/packages/smithy-python/src/smithy_python/aws/__init__.py new file mode 100644 index 000000000..a6962672b --- /dev/null +++ b/packages/smithy-python/src/smithy_python/aws/__init__.py @@ -0,0 +1,20 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +"""Client generation for AWS services, as integrations of the core generator. + +A service with the ``aws.api#service`` trait gets clients named for its SDK ID and +configs that resolve from the environment and shared config files. Its clients also +send the AWS user agent. +""" + +from __future__ import annotations + +from typing import Final + +from ._config import AwsConfigIntegration +from ._dependencies import SMITHY_AWS_CORE + +__all__ = ["AWS_INTEGRATIONS", "SMITHY_AWS_CORE", "AwsConfigIntegration"] + +AWS_INTEGRATIONS: Final = (AwsConfigIntegration(),) +"""The integrations AWS services get, in order.""" diff --git a/packages/smithy-python/src/smithy_python/aws/_config.py b/packages/smithy-python/src/smithy_python/aws/_config.py new file mode 100644 index 000000000..188592344 --- /dev/null +++ b/packages/smithy-python/src/smithy_python/aws/_config.py @@ -0,0 +1,396 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +"""AWS client names, config classes, and the AWS user agent plugin.""" + +from __future__ import annotations + +from collections.abc import Collection, Mapping +from typing import TYPE_CHECKING, Final + +from .. import dependencies as _dependencies +from .. import exceptions as _exceptions +from .. import model as _model +from .. import models as _models +from ..integrations import ClientIntegration, ClientMode, ClientNames, ConfigPlan +from ..symbols import TypeReference +from ._dependencies import SMITHY_AWS_CORE + +if TYPE_CHECKING: + from ..clients import ClientPlan + from ..generation import ClientSettings, GenerationContext + from ..integrations import Part + from ..writer import PythonWriter + +__all__ = ["AwsConfigIntegration"] + + +class AwsConfigIntegration(ClientIntegration): + """Clients and configs of services with the ``aws.api#service`` trait. + + The configs subclass ``AwsConfig`` and ``AsyncAwsConfig``, so they resolve + from environment variables and shared config files, and a client creates its + default config with ``resolve()``. Clients and configs are named for the + service's SDK ID, and clients send the AWS user agent. + """ + + def matches(self, service: _model.ServiceShape) -> bool: + return service.has_trait(_SERVICE) + + def client_names( + self, service: _model.ServiceShape, names: ClientNames + ) -> ClientNames: + name = _sdk_id(service).replace(" ", "") + name = name[:1].upper() + name[1:] + return ClientNames( + client=f"{name}Client", + config=f"{name}Config", + async_client=f"Async{name}Client", + async_config=f"Async{name}Config", + ) + + def config_fields(self, service: _model.ServiceShape) -> Collection[str]: + del service + return _BASE_FIELDS + + def write_config(self, writer: PythonWriter, config: ConfigPlan) -> bool: + _write_config(writer, config) + return True + + def new_config(self, mode: ClientMode, config: TypeReference) -> tuple[Part, ...]: + return ("await ", config, ".resolve") if mode.is_async else (config, ".resolve") + + def client_plugins( + self, plan: ClientPlan, mode: ClientMode, package: str + ) -> tuple[TypeReference, ...]: + del plan + return (TypeReference(_PLUGIN, _user_agent_module(package, mode)),) + + def write_files( + self, + context: GenerationContext[ClientSettings], + plan: ClientPlan, + modes: tuple[ClientMode, ClientMode], + ) -> None: + package = context.settings.import_name + service = plan.selection.service + assert service is not None # noqa: S101 - ClientPlan requires a service + for mode in modes: + module = _user_agent_module(package, mode) + writer = context.files.python_module( + f"src/{module.replace('.', '/')}.py", module + ) + config = TypeReference( + mode.config, f"{package}.{mode.directory.replace('/', '.')}config" + ) + writer.line("def ", _PLUGIN, "(config: ", config, ") -> None:") + with writer.indent(): + writer.line(_quote("Send the AWS user agent with every request.")) + writer.line("config.interceptors.append(") + with writer.indent(): + writer.line(_USER_AGENT_INTERCEPTOR, "(") + with writer.indent(): + writer.line("ua_suffix=config.user_agent_extra,") + writer.line("ua_app_id=config.sdk_ua_app_id,") + writer.line( + "sdk_version=", TypeReference("__version__", package), "," + ) + writer.line( + "service_id=", + _quote(_sdk_id(service).replace(" ", "_")), + ",", + ) + writer.line(")") + writer.line(")") + + +def _aws(name: str, module: str) -> TypeReference: + return TypeReference( + name, f"smithy_aws_core.{module}", dependencies=(SMITHY_AWS_CORE,) + ) + + +def _core(name: str, module: str) -> TypeReference: + return TypeReference( + name, f"smithy_core.{module}", dependencies=(_dependencies.SMITHY_CORE,) + ) + + +def _generic(reference: TypeReference, *arguments: TypeReference) -> TypeReference: + return TypeReference( + reference.name, + reference.module, + arguments=arguments, + dependencies=reference.dependencies, + ) + + +def _quote(value: str) -> str: + return _models._quote(value) # pyright: ignore[reportPrivateUsage] + + +def _sdk_id(service: _model.ServiceShape) -> str: + trait = service.get_trait(_SERVICE) + sdk_id = trait.get("sdkId") if isinstance(trait, Mapping) else None + if not isinstance(sdk_id, str) or not sdk_id.strip(): + raise _exceptions.CodegenError( + f"Service {service.id} has an aws.api#service trait without an sdkId" + ) + return sdk_id + + +def _user_agent_module(package: str, mode: ClientMode) -> str: + return f"{package}.{mode.directory.replace('/', '.')}user_agent" + + +def _write_config(writer: PythonWriter, config: ConfigPlan) -> None: + service, mode, endpoints = config.service, config.mode, config.endpoints + sync = not mode.is_async + base = ( + _aws("AwsConfig", "config.aws_config") + if sync + else _aws("AsyncAwsConfig", "aio.config.aws_config") + ) + overrides_base = ( + _aws("AwsConfigOverrides", "config.aws_config") + if sync + else _aws("AsyncAwsConfigOverrides", "aio.config.aws_config") + ) + file_system = ( + _aws("FileSystem", "config.filesystem") + if sync + else _aws("AsyncFileSystem", "aio.config.filesystem") + ) + settings = config.protocol_settings + client_protocol = _generic(_core("ClientProtocol", "interfaces"), _ANY, _ANY) + protocol_input = ( + client_protocol, + " | ", + _generic(_core("ProtocolConstructor", "interfaces"), client_protocol), + ) + endpoint_resolver = _join(mode.endpoint_resolver) + auth_schemes = ("dict[", _SHAPE_ID, ", ", mode.auth_scheme, "]") + auth_fields = [field for scheme in config.auth_schemes for field in scheme.fields] + # Fields the base config lacks: (name, annotation, documentation, FieldSpec). + fields: list[tuple[str, tuple[Part, ...], str, tuple[Part, ...]]] = [ + ( + "endpoint_resolver", + (*endpoint_resolver, " | None"), + "Resolves the endpoint of each operation.", + ( + "default_factory=lambda: ", + _BDD_ENDPOINT_RESOLVER, + "(", + config.bdd, + ")", + ), + ), + ( + "protocol", + (client_protocol, " | None"), + "Serializes requests and deserializes responses.", + ( + "default_factory=lambda: ", + mode.protocol, + f"({settings}), converter=lambda p: p({settings}) " + "if isinstance(p, type) else p", + ), + ), + ( + "auth_schemes", + (*auth_schemes, " | None"), + "Auth schemes by auth scheme ID.", + ("default_factory=lambda: ", *_default_auth_schemes(config)), + ), + ( + "auth_scheme_resolver", + (_AUTH_SCHEME_RESOLVER, " | None"), + "Determines the auth scheme for each operation.", + # The default resolver orders each operation's effective auth schemes, + # so an operation with noAuth first is sent unsigned. + ("default_factory=", _DEFAULT_AUTH_RESOLVER), + ), + *( + ( + field.name, + field.annotation if sync else field.async_annotation, + field.documentation, + _field_default(field.default if sync else field.async_default), + ) + for field in auth_fields + ), + *( + ( + field.attribute, + (field.type, " | None"), + field.documentation, + ("default=None",), + ) + for field in endpoints.client_context_fields + ), + ] + overrides = f"_{mode.config}Overrides" + writer.line("class ", overrides, "(", overrides_base, ", total=False):") + with writer.indent(): + writer.line(_quote(f"Explicit field values {mode.config}.resolve accepts.")) + writer.line() + for name, annotation, _docs, _spec in fields: + if name == "protocol": + writer.line(name, ": ", *protocol_input, " | None") + else: + writer.line(name, ": ", *annotation) + writer.line() + writer.line() + # repr=False keeps the base __repr__, which leaves out credentials. + writer.line("@", _DATACLASS, "(kw_only=True, repr=False, init=False)") + writer.line("class ", mode.config, "(", base, "):") + with writer.indent(): + writer.line(_quote(f"Configuration for {mode.client}.")) + writer.line() + interceptor = _generic(_core("Interceptor", "interceptors"), *_ANY4) + writer.line( + "interceptors: list[", + interceptor, + "] = ", + _FIELD, + "(default_factory=list[", + interceptor, + "])", + ) + writer.line(_quote("Hooks called during the execution of a request.")) + for name, annotation, docs, _spec in fields: + writer.line() + writer.line(name, ": ", *annotation, " = None") + writer.line(_quote(docs)) + writer.line() + writer.line("_FIELDS: ", _CLASS_VAR, "[dict[str, ", _FIELD_SPEC, "]] = {") + with writer.indent(): + writer.line("**", base, "._FIELDS,") + writer.line( + '"endpoint_uri": ', + _FIELD_SPEC, + "(default=None, resolver=", + _ENDPOINT_URI_RESOLVER, + "(", + _quote(_sdk_id(service).replace(" ", "_").lower()), + ").program),", + ) + for name, _annotation, _docs, spec in fields: + writer.line(_quote(name), ": ", _FIELD_SPEC, "(", *spec, "),") + writer.line( + '"transport": ', + _FIELD_SPEC, + "(default_factory=lambda: ", + mode.transport, + "()),", + ) + writer.line("}") + if config.auth_schemes: + writer.line() + writer.line( + "def set_auth_scheme(self, scheme: ", mode.auth_scheme, ") -> None:" + ) + with writer.indent(): + writer.line(_quote("Set an auth scheme under its scheme ID.")) + writer.line("auth_schemes = dict(self.auth_schemes or {})") + writer.line("auth_schemes[scheme.scheme_id] = scheme") + writer.line("self.auth_schemes = auth_schemes") + writer.line() + writer.line("@classmethod") + define = "def " if sync else "async def " + writer.line( + define, "resolve( # pyright: ignore[reportIncompatibleMethodOverride]" + ) + with writer.indent(): + writer.line("cls,") + writer.line("*,") + writer.line("profile: str | None = None,") + writer.line("fs: ", file_system, " | None = None,") + writer.line("config_file_path: str | None = None,") + writer.line("credentials_file_path: str | None = None,") + writer.line("**overrides: ", _UNPACK, "[", overrides, "],") + writer.line(") -> ", _SELF, ":") + with writer.indent(): + writer.line( + _quote( + "Resolve the config from overrides, the environment, and " + "config files." + ) + ) + writer.line("return ", "" if sync else "await ", "cls._resolve(") + with writer.indent(): + writer.line("profile=profile,") + writer.line("fs=fs,") + writer.line("config_file_path=config_file_path,") + writer.line("credentials_file_path=credentials_file_path,") + writer.line("overrides=overrides,") + writer.line(")") + + +def _default_auth_schemes(config: ConfigPlan) -> tuple[Part, ...]: + parts: list[Part] = ["{"] + for scheme in config.auth_schemes: + build = scheme.async_scheme if config.mode.is_async else scheme.scheme + if len(parts) > 1: + parts.append(", ") + parts.extend( + ( + _SHAPE_ID, + "(", + _quote(str(scheme.scheme_id)), + "): ", + *build(config.service.get_trait(scheme.scheme_id)), + ) + ) + parts.append("}") + return tuple(parts) + + +def _field_default(default: tuple[Part, ...]) -> tuple[Part, ...]: + return ("default_factory=lambda: ", *default) if default else ("default=None",) + + +def _join(references: tuple[TypeReference, ...]) -> tuple[Part, ...]: + parts: list[Part] = [] + for reference in references: + if parts: + parts.append(" | ") + parts.append(reference) + return tuple(parts) + + +_SERVICE: Final = _model.ShapeId("aws.api", "service") +_PLUGIN: Final = "aws_user_agent_plugin" +# The fields AwsConfig and AsyncAwsConfig declare, and those every generated AWS +# config adds. +_BASE_FIELDS: Final = frozenset( + { + "aws_access_key_id", + "aws_credentials_identity_resolver", + "aws_secret_access_key", + "aws_session_token", + "endpoint_uri", + "http_request_config", + "interceptors", + "max_attempts", + "region", + "retry_mode", + "retry_strategy", + "sdk_ua_app_id", + "transport", + "user_agent_extra", + } +) +_ANY: Final = TypeReference("Any", "typing") +_ANY4: Final = (_ANY, _ANY, _ANY, _ANY) +_CLASS_VAR: Final = TypeReference("ClassVar", "typing") +_SELF: Final = TypeReference("Self", "typing") +_UNPACK: Final = TypeReference("Unpack", "typing") +_DATACLASS: Final = TypeReference("dataclass", "dataclasses") +_FIELD: Final = TypeReference("field", "dataclasses") +_FIELD_SPEC: Final = _aws("FieldSpec", "config.types") +_ENDPOINT_URI_RESOLVER: Final = _aws("EndpointUriResolver", "config.resolvers") +_USER_AGENT_INTERCEPTOR: Final = _aws("UserAgentInterceptor", "interceptors.user_agent") +_SHAPE_ID: Final = _core("ShapeID", "shapes") +_AUTH_SCHEME_RESOLVER: Final = _core("AuthSchemeResolver", "interfaces.auth") +_DEFAULT_AUTH_RESOLVER: Final = _core("DefaultAuthResolver", "auth") +_BDD_ENDPOINT_RESOLVER: Final = _core("BddEndpointResolver", "endpoints") diff --git a/packages/smithy-python/src/smithy_python/aws/_dependencies.py b/packages/smithy-python/src/smithy_python/aws/_dependencies.py new file mode 100644 index 000000000..423b03187 --- /dev/null +++ b/packages/smithy-python/src/smithy_python/aws/_dependencies.py @@ -0,0 +1,19 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +"""Runtime dependencies of generated AWS clients.""" + +from __future__ import annotations + +from typing import Final + +from ..dependencies import Dependency + +SMITHY_AWS_CORE: Final = Dependency( + package="smithy-aws-core", + specifier="~=0.16.0", + group="runtime", +) +"""Compatible smithy-aws-core runtime for generated AWS clients. + +CAUTION: Keep the specifier at the package's current minor version. +""" diff --git a/packages/smithy-python/src/smithy_python/cli.py b/packages/smithy-python/src/smithy_python/cli.py index f3cb66d07..df62dc35c 100644 --- a/packages/smithy-python/src/smithy_python/cli.py +++ b/packages/smithy-python/src/smithy_python/cli.py @@ -13,6 +13,7 @@ from typing import BinaryIO, Final from . import __version__ +from . import aws as _aws from . import clients as _clients from . import dependencies as _dependencies from . import exceptions as _exceptions @@ -287,7 +288,7 @@ def _generate_client( plan = _clients.ClientPlan.from_model(model, settings, service_id=args.service) except ValueError as error: raise InvalidInvocationError(str(error)) from error - generator = _clients.ClientGenerator(plan) + generator = _clients.ClientGenerator(plan, integrations=_aws.AWS_INTEGRATIONS) context = generator.context() generator.generate(context) context.files.commit(output_dir) diff --git a/packages/smithy-python/src/smithy_python/clients.py b/packages/smithy-python/src/smithy_python/clients.py index b1030b3c7..3a5a5d691 100644 --- a/packages/smithy-python/src/smithy_python/clients.py +++ b/packages/smithy-python/src/smithy_python/clients.py @@ -5,7 +5,7 @@ from __future__ import annotations from collections import Counter -from collections.abc import Mapping +from collections.abc import Mapping, Sequence from dataclasses import dataclass, replace from types import MappingProxyType @@ -13,6 +13,7 @@ from . import dependencies as _dependencies from . import files as _files from . import generation as _generation +from . import integrations as _integrations from . import model as _model from . import models as _models from . import protocols as _protocols @@ -33,9 +34,17 @@ def __init__( protocols: Mapping[ _model.ShapeId, _protocols.ProtocolContribution ] = _protocols.DEFAULT_PROTOCOLS, + integrations: Sequence[_integrations.ClientIntegration] = (), ) -> None: + """Create a generator for a planned client. + + :param protocols: The protocol contributions by protocol ID. + :param integrations: Integrations that may change the generated clients. + Those that match the service apply, in order. + """ self._plan = plan self._protocols = protocols + self._integrations = tuple(integrations) def generate( self, context: _generation.GenerationContext[_generation.ClientSettings] @@ -52,11 +61,16 @@ def generate( protocol = _protocols.select_protocol( service, context.settings.protocol, self._protocols ) + integrations = tuple( + integration + for integration in self._integrations + if integration.matches(service) + ) _client_emitter.check_supported(plan) - endpoints = _client_emitter.plan_endpoints(plan) + endpoints = _client_emitter.plan_endpoints(plan, integrations) _schemas.SchemaGenerator().generate(context) _models.ModelGenerator().generate(context) - _client_emitter.write_client(context, plan, protocol, endpoints) + _client_emitter.write_client(context, plan, protocol, endpoints, integrations) def context( self, diff --git a/packages/smithy-python/src/smithy_python/integrations.py b/packages/smithy-python/src/smithy_python/integrations.py new file mode 100644 index 000000000..ea0254e1a --- /dev/null +++ b/packages/smithy-python/src/smithy_python/integrations.py @@ -0,0 +1,134 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +"""Extension points that let a provider change the clients a service generates. + +A :class:`ClientIntegration` applies to the services it matches. It can rename the +clients and configs, replace the config classes, add client plugins and modules, and +change how a client creates its default config. Integrations keep provider code, +such as AWS config resolution, out of the core generator. +""" + +from __future__ import annotations + +from collections.abc import Collection +from dataclasses import dataclass +from typing import TYPE_CHECKING + +from .symbols import TypeReference + +if TYPE_CHECKING: + from . import _auth, _endpoints + from . import model as _model + from .clients import ClientPlan + from .generation import ClientSettings, GenerationContext + from .writer import PythonWriter + +__all__ = ["ClientIntegration", "ClientMode", "ClientNames", "ConfigPlan", "Part"] + +type Part = str | TypeReference +"""A piece of a generated line.""" + + +@dataclass(frozen=True, slots=True) +class ClientNames: + """The client and config class names of a generated package.""" + + client: str + config: str + async_client: str + async_config: str + + +@dataclass(frozen=True, slots=True) +class ClientMode: + """Names and runtime types that differ between the sync and async client.""" + + directory: str + """The package-relative directory of the mode's modules, ``""`` or ``"aio/"``.""" + + config: str + client: str + client_base: TypeReference + protocol: TypeReference + transport: TypeReference + transport_type: TypeReference + auth_scheme: TypeReference + endpoint_resolver: tuple[TypeReference, ...] + is_async: bool + + +@dataclass(frozen=True, slots=True) +class ConfigPlan: + """Everything a config class for one mode is built from.""" + + plan: ClientPlan + service: _model.ServiceShape + mode: ClientMode + endpoints: _endpoints.EndpointPlan + bdd: TypeReference + """The ``BDD`` constant of the generated endpoints module.""" + + auth_schemes: tuple[_auth.AuthContribution, ...] + """The supported auth schemes the service models, in shape ID order.""" + + protocol_settings: str + """The name of the module-level ``ProtocolSettings`` constant.""" + + +class ClientIntegration: + """Changes the clients generated for the services it matches. + + Every method has a default that changes nothing, so an integration overrides + only what it needs. + """ + + def matches(self, service: _model.ServiceShape) -> bool: + """Whether the integration applies to a service.""" + del service + return False + + def client_names( + self, service: _model.ServiceShape, names: ClientNames + ) -> ClientNames: + """The class names of the service's clients and configs.""" + del service + return names + + def config_fields(self, service: _model.ServiceShape) -> Collection[str]: + """Config fields the integration declares, which a client context + parameter's field must not take.""" + del service + return () + + def write_config(self, writer: PythonWriter, config: ConfigPlan) -> bool: + """Write the mode's config class in place of the core one. + + :returns: Whether the integration wrote the class. + """ + del writer, config + return False + + def new_config(self, mode: ClientMode, config: TypeReference) -> tuple[Part, ...]: + """The callable that creates a default config, called with keyword fields. + + A client calls it with no arguments when it has no config. An async mode + may begin the parts with ``"await "``. + """ + del mode + return (config,) + + def client_plugins( + self, plan: ClientPlan, mode: ClientMode, package: str + ) -> tuple[TypeReference, ...]: + """Plugins every client of the mode applies, before the core ones.""" + del plan, mode, package + return () + + def write_files( + self, + context: GenerationContext[ClientSettings], + plan: ClientPlan, + modes: tuple[ClientMode, ClientMode], + ) -> None: + """Register any extra modules, such as the modules of client plugins.""" + del context, plan, modes diff --git a/packages/smithy-python/tests/unit/test_client_generation.py b/packages/smithy-python/tests/unit/test_client_generation.py index 4428462d2..ea8ce15cf 100644 --- a/packages/smithy-python/tests/unit/test_client_generation.py +++ b/packages/smithy-python/tests/unit/test_client_generation.py @@ -21,6 +21,7 @@ from smithy_core.retries import SimpleRetryStrategy from smithy_http import HTTPResponse, tuples_to_fields from smithy_http.interfaces import HTTPRequest, HTTPRequestConfiguration +from smithy_python import aws as _aws from smithy_python.clients import ClientGenerator, ClientPlan from smithy_python.exceptions import CodegenError from smithy_python.generation import ClientSettings @@ -643,3 +644,71 @@ def test_api_key_auth_trait_must_name_a_location(tmp_path: Path) -> None: _add_api_key(shapes, name="key", **{"in": "body"}) with pytest.raises(CodegenError, match="Invalid httpApiKeyAuth trait"): _generate(tmp_path, "greeter_bad_api_key", shapes) + + +def _aws_generate(output: Path, package: str, shapes: dict[str, Any]) -> None: + generator = ClientGenerator( + _plan(shapes, package), integrations=_aws.AWS_INTEGRATIONS + ) + context = generator.context() + generator.generate(context) + context.files.commit(output) + + +def test_aws_services_get_named_resolving_clients(tmp_path: Path) -> None: + shapes = _shapes() + shapes["example#Greeter"]["traits"]["aws.api#service"] = {"sdkId": "Hello World"} + _aws_generate(tmp_path, "greeter_aws", shapes) + source = tmp_path / "src/greeter_aws" + config = (source / "config.py").read_text() + assert "class HelloWorldConfig(AwsConfig):" in config + assert 'EndpointUriResolver("hello_world")' in config + client = (source / "aio/client.py").read_text() + assert "class AsyncHelloWorldClient(AsyncClient[AsyncHelloWorldConfig]):" in client + assert "return await AsyncHelloWorldConfig.resolve()" in client + assert "_client_plugins = (aws_user_agent_plugin, user_agent_plugin)" in client + assert 'service_id="Hello_World"' in (source / "user_agent.py").read_text() + assert '"smithy-aws-core~=0.16.0",' in (tmp_path / "pyproject.toml").read_text() + + +def test_integrations_apply_only_to_services_they_match(tmp_path: Path) -> None: + _aws_generate(tmp_path, "greeter_plain", _shapes()) + assert "class Config:" in (tmp_path / "src/greeter_plain/config.py").read_text() + assert not (tmp_path / "src/greeter_plain/user_agent.py").exists() + + +def test_aws_services_need_an_sdk_id(tmp_path: Path) -> None: + shapes = _shapes() + shapes["example#Greeter"]["traits"]["aws.api#service"] = {} + with pytest.raises(CodegenError, match="without an sdkId"): + _aws_generate(tmp_path, "greeter_no_sdk_id", shapes) + + +def test_client_context_fields_must_not_take_aws_config_fields(tmp_path: Path) -> None: + shapes = _shapes() + _add_endpoint_rules(shapes) + traits = shapes["example#Greeter"]["traits"] + traits["aws.api#service"] = {"sdkId": "Greeter"} + traits["smithy.rules#clientContextParams"] = {"Region": {"type": "string"}} + with pytest.raises(CodegenError, match="which the config already has"): + _aws_generate(tmp_path, "greeter_region", shapes) + + +def test_aws_clients_with_dashed_package_names_import( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + shapes = _shapes() + shapes["example#Greeter"]["traits"]["aws.api#service"] = {"sdkId": "Greeter"} + _aws_generate(tmp_path, "aws-greeter-dashed", shapes) + source = tmp_path / "src/aws_greeter_dashed" + assert ( + "from aws_greeter_dashed import __version__" + in (source / "user_agent.py").read_text() + ) + monkeypatch.syspath_prepend( # pyright: ignore[reportUnknownMemberType] + str(tmp_path / "src") + ) + package = importlib.import_module("aws_greeter_dashed") + assert package.AsyncGreeterClient._client_plugins[0].__module__ == ( + "aws_greeter_dashed.aio.user_agent" + ) diff --git a/scripts/api-snapshot/api-rex.json b/scripts/api-snapshot/api-rex.json index fe92a8951..aba525c51 100644 --- a/scripts/api-snapshot/api-rex.json +++ b/scripts/api-snapshot/api-rex.json @@ -1328,6 +1328,10 @@ "smithy_json.settings": { "TimestampFormat": "rex-class" }, + "smithy_python.aws": { + "AwsConfigIntegration": "rex-class", + "SMITHY_AWS_CORE": "rex-Dependency" + }, "smithy_python.cli": { "CodegenError": "rex-exception", "InvalidInvocationError": "rex-exception", @@ -1338,6 +1342,10 @@ "smithy_python.dependencies": { "DependencyGroup": "rex-type_alias" }, + "smithy_python.integrations": { + "Part": "rex-type_alias", + "TypeReference": "rex-class" + }, "smithy_python.model": { "EnumShape": "rex-class", "IntEnumShape": "rex-class", diff --git a/scripts/api-snapshot/api.json b/scripts/api-snapshot/api.json index 1b89da49d..c647b1cf0 100644 --- a/scripts/api-snapshot/api.json +++ b/scripts/api-snapshot/api.json @@ -836,6 +836,9 @@ "JSONSettings": "class" }, "smithy_python": {}, + "smithy_python.aws": { + "AWS_INTEGRATIONS": "tuple" + }, "smithy_python.cli": { "main": "function" }, @@ -881,6 +884,12 @@ "OperationModel": "class", "TypesSettings": "class" }, + "smithy_python.integrations": { + "ClientIntegration": "class", + "ClientMode": "class", + "ClientNames": "class", + "ConfigPlan": "class" + }, "smithy_python.model": {}, "smithy_python.models": { "ModelGenerator": "class" diff --git a/scripts/codegen-output-py/client-projections.json b/scripts/codegen-output-py/client-projections.json index ccaf9ffcc..ce2d31581 100644 --- a/scripts/codegen-output-py/client-projections.json +++ b/scripts/codegen-output-py/client-projections.json @@ -6,6 +6,13 @@ "service": "example.apikey#ApiKeyService", "version": "0.0.1" }, + "aws-config-client": { + "description": "Python client for the AWS config fixture", + "model": "models/aws-config.json", + "package": "aws_config_client", + "service": "example.awsconfig#WeatherStation", + "version": "0.0.1" + }, "endpoint-rules-client": { "description": "Python client for the endpoint rules fixture", "model": "models/endpoint-rules.json", diff --git a/scripts/codegen-output-py/models/aws-config.json b/scripts/codegen-output-py/models/aws-config.json new file mode 100644 index 000000000..d87bc62a6 --- /dev/null +++ b/scripts/codegen-output-py/models/aws-config.json @@ -0,0 +1,132 @@ +{ + "smithy": "2.0", + "shapes": { + "example.awsconfig#GetForecast": { + "type": "operation", + "input": { + "target": "example.awsconfig#GetForecastInput" + }, + "output": { + "target": "example.awsconfig#GetForecastOutput" + }, + "traits": { + "smithy.api#documentation": "Gets the forecast for a city." + } + }, + "example.awsconfig#GetForecastInput": { + "type": "structure", + "members": { + "city": { + "target": "smithy.api#String" + } + }, + "traits": { + "smithy.api#input": {} + } + }, + "example.awsconfig#GetForecastOutput": { + "type": "structure", + "members": { + "summary": { + "target": "smithy.api#String" + } + }, + "traits": { + "smithy.api#output": {} + } + }, + "example.awsconfig#WeatherStation": { + "type": "service", + "version": "2026-11-19", + "operations": [ + { + "target": "example.awsconfig#GetForecast" + } + ], + "traits": { + "aws.api#service": { + "sdkId": "Weather Station" + }, + "smithy.api#documentation": "An AWS service that reads its config from the environment and profiles.", + "smithy.protocols#rpcv2Cbor": {}, + "smithy.rules#endpointBdd": { + "version": "1.1", + "parameters": { + "Endpoint": { + "builtIn": "SDK::Endpoint", + "required": false, + "documentation": "A custom endpoint.", + "type": "string" + } + }, + "conditions": [ + { + "fn": "isSet", + "argv": [ + { + "ref": "Endpoint" + } + ] + } + ], + "results": [ + { + "conditions": [], + "endpoint": { + "url": { + "ref": "Endpoint" + }, + "properties": {}, + "headers": {} + }, + "type": "endpoint" + }, + { + "conditions": [], + "error": "Endpoint is not set", + "type": "error" + } + ], + "root": 2, + "nodeCount": 2, + "nodes": "/////wAAAAH/////AAAAAAX14QEF9eEC" + }, + "smithy.rules#endpointRuleSet": { + "version": "1.0", + "parameters": { + "Endpoint": { + "type": "string", + "builtIn": "SDK::Endpoint", + "documentation": "A custom endpoint." + } + }, + "rules": [ + { + "conditions": [ + { + "fn": "isSet", + "argv": [ + { + "ref": "Endpoint" + } + ] + } + ], + "endpoint": { + "url": { + "ref": "Endpoint" + } + }, + "type": "endpoint" + }, + { + "conditions": [], + "error": "Endpoint is not set", + "type": "error" + } + ] + } + } + } + } +} diff --git a/scripts/codegen-output-py/models/aws-config.smithy b/scripts/codegen-output-py/models/aws-config.smithy new file mode 100644 index 000000000..198840642 --- /dev/null +++ b/scripts/codegen-output-py/models/aws-config.smithy @@ -0,0 +1,39 @@ +$version: "2.0" + +namespace example.awsconfig + +use aws.api#service +use smithy.protocols#rpcv2Cbor +use smithy.rules#endpointRuleSet + +/// An AWS service that reads its config from the environment and profiles. +@service(sdkId: "Weather Station") +@rpcv2Cbor +@endpointRuleSet({ + version: "1.0" + parameters: { + Endpoint: { type: "string", builtIn: "SDK::Endpoint", documentation: "A custom endpoint." } + } + rules: [ + { + conditions: [{ fn: "isSet", argv: [{ ref: "Endpoint" }] }] + endpoint: { url: { ref: "Endpoint" } } + type: "endpoint" + } + { conditions: [], error: "Endpoint is not set", type: "error" } + ] +}) +service WeatherStation { + version: "2026-11-19" + operations: [GetForecast] +} + +/// Gets the forecast for a city. +operation GetForecast { + input := { + city: String + } + output := { + summary: String + } +} diff --git a/scripts/codegen-output-py/pyrightconfig.json b/scripts/codegen-output-py/pyrightconfig.json index f5bf90172..2edc0da59 100644 --- a/scripts/codegen-output-py/pyrightconfig.json +++ b/scripts/codegen-output-py/pyrightconfig.json @@ -9,6 +9,7 @@ ], "extraPaths": [ "../../codegen-output-py/api-key-auth-client/src", + "../../codegen-output-py/aws-config-client/src", "../../codegen-output-py/aws-json-1-0/src", "../../codegen-output-py/aws-json-1-1/src", "../../codegen-output-py/aws-query/src", diff --git a/scripts/codegen-output-py/tests/test_client_snapshots.py b/scripts/codegen-output-py/tests/test_client_snapshots.py index f21f3ef35..5a265edeb 100644 --- a/scripts/codegen-output-py/tests/test_client_snapshots.py +++ b/scripts/codegen-output-py/tests/test_client_snapshots.py @@ -16,6 +16,7 @@ from typing import Any import pytest +from smithy_aws_core.config.exceptions import ConfigError from smithy_cbor import loads from smithy_core.aio.types import AsyncBytesReader from smithy_core.exceptions import SmithyIdentityError @@ -215,3 +216,93 @@ def test_api_key_auth_requires_a_key(api_key_package: ModuleType, aio: bool) -> _call(aio, client.get_thing(name="a")) _call(aio, client.health()) assert len(transport.requests) == 1 + + +@pytest.fixture +def aws_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> pytest.MonkeyPatch: + """An environment with no AWS settings and empty shared config files.""" + for name in list(os.environ): + if name.startswith("AWS_"): + monkeypatch.delenv(name) + monkeypatch.setenv("AWS_CONFIG_FILE", str(tmp_path / "config")) + monkeypatch.setenv("AWS_SHARED_CREDENTIALS_FILE", str(tmp_path / "credentials")) + # The base AWS config requires a region. + monkeypatch.setenv("AWS_REGION", "us-west-2") + return monkeypatch + + +@pytest.fixture(scope="module") +def aws_package() -> Iterator[ModuleType]: + source = _ROOT / "codegen-output-py/aws-config-client/src" + name = _PROJECTIONS["aws-config-client"]["package"] + sys.path.insert(0, str(source)) + try: + yield importlib.import_module(name) + finally: + sys.path.remove(str(source)) + for module in tuple(sys.modules): + if module == name or module.startswith(name + "."): + del sys.modules[module] + + +def _aws_client(package: ModuleType, aio: bool, **overrides: Any) -> Any: + transport = (_AsyncTransport if aio else _Transport)(b"\xa1\x67summary\x63sun") + config_class = ( + package.AsyncWeatherStationConfig if aio else package.WeatherStationConfig + ) + config = config_class.resolve( + transport=transport, + retry_strategy=SimpleRetryStrategy(max_attempts=1), + **overrides, + ) + if aio: + config = asyncio.run(config) + client = package.AsyncWeatherStationClient if aio else package.WeatherStationClient + return client(config), transport + + +@pytest.mark.parametrize("aio", [False, True]) +def test_aws_config_resolves_the_endpoint_from_the_environment( + aws_package: ModuleType, aio: bool, aws_env: pytest.MonkeyPatch +) -> None: + aws_env.setenv("AWS_ENDPOINT_URL_WEATHER_STATION", "https://weather.example.com") + client, transport = _aws_client(aws_package, aio, sdk_ua_app_id="forecaster") + output = _call(aio, client.get_forecast(city="Lisbon")) + assert output.summary == "sun" + (request,) = transport.requests + assert request.destination.host == "weather.example.com" + user_agent = request.fields["User-Agent"].as_string() + assert "api/Weather_Station#0.0.1" in user_agent + assert "app/forecaster" in user_agent + + +@pytest.mark.parametrize("aio", [False, True]) +def test_aws_config_rejects_unknown_fields( + aws_package: ModuleType, aio: bool, aws_env: pytest.MonkeyPatch +) -> None: + config_class = ( + aws_package.AsyncWeatherStationConfig + if aio + else aws_package.WeatherStationConfig + ) + with pytest.raises(Exception, match="nope"): + _call(aio, config_class.resolve(nope=1)) + with pytest.raises(ConfigError, match="cannot be constructed directly"): + config_class() + + +def test_aws_client_resolves_its_own_config( + aws_package: ModuleType, aws_env: pytest.MonkeyPatch +) -> None: + (Path(os.environ["AWS_CONFIG_FILE"])).write_text( + "[default]\nendpoint_url = https://profile.example.com\n" + ) + transport = _Transport(b"\xa0") + + def use_transport(config: Any) -> None: + config.transport = transport + + client = aws_package.WeatherStationClient(plugins=[use_transport]) + client.get_forecast(city="Lisbon") + (request,) = transport.requests + assert request.destination.host == "profile.example.com" diff --git a/scripts/verifications/import-cost.json b/scripts/verifications/import-cost.json index 3d55bef32..fc8cf3def 100644 --- a/scripts/verifications/import-cost.json +++ b/scripts/verifications/import-cost.json @@ -16,7 +16,7 @@ "smithy_json": 21, "smithy_json (all public modules)": 21, "smithy_python": 1, - "smithy_python (all public modules)": 27, + "smithy_python (all public modules)": 31, "smithy_test": 44, "smithy_test (all public modules)": 60, "smithy_xml": 20,