Skip to content

Commit fd6d81a

Browse files
authored
Add support for sdk_ua_app_id. (#653)
* Add support for sdk_ua_app_id
1 parent 6c4b348 commit fd6d81a

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

codegen/aws/core/src/main/java/software/amazon/smithy/python/aws/codegen/AwsUserAgentIntegration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) {
4949
"A unique and opaque application ID that is appended to the User-Agent header.")
5050
.type(Symbol.builder().name("str").build())
5151
.nullable(true)
52+
.useDescriptor(true)
53+
.validator(Symbol.builder()
54+
.name("validate_ua_string")
55+
.namespace("smithy_aws_core.config.validators", ".")
56+
.addDependency(AwsPythonDependency.SMITHY_AWS_CORE)
57+
.build())
5258
.build();
5359

5460
final String user_agent_plugin_file = "user_agent";

packages/smithy-aws-core/src/smithy_aws_core/config/validators.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,25 @@ def validate_retry_strategy(
144144
f"retry_strategy must be RetryStrategy or RetryStrategyOptions, got {type(value).__name__}",
145145
source,
146146
)
147+
148+
149+
def validate_ua_string(value: Any, source: SourceInfo | None = None) -> str | None:
150+
"""Validate a User-Agent string component.
151+
152+
:param value: The UA string value to validate
153+
:param source: The source that provided this value
154+
155+
:returns: The UA string or None if value is None
156+
157+
:raises ConfigValidationError: If the value is not a string
158+
"""
159+
if value is None:
160+
return None
161+
if not isinstance(value, str):
162+
raise ConfigValidationError(
163+
"sdk_ua_app_id",
164+
value,
165+
f"UA string must be a string, got {type(value).__name__}",
166+
source,
167+
)
168+
return value

packages/smithy-aws-core/tests/unit/config/test_property.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def _create_config_with_validator(
107107

108108
class ConfigWithValidator:
109109
region = ConfigProperty("region", validator=validator)
110-
retry_strategy = ConfigProperty("retry_strategy", validator=validator)
111110

112111
def __init__(self, resolver: ConfigResolver) -> None:
113112
self._resolver = resolver
@@ -153,9 +152,7 @@ class ConfigWithComplexResolver:
153152
retry_strategy = ConfigProperty(
154153
"retry_strategy",
155154
resolver_func=mock_resolver,
156-
default_value=RetryStrategyOptions(
157-
retry_mode="standard", max_attempts=3
158-
),
155+
default_value=RetryStrategyOptions(retry_mode="standard"),
159156
)
160157

161158
def __init__(self, resolver: ConfigResolver) -> None:
@@ -171,7 +168,7 @@ def __init__(self, resolver: ConfigResolver) -> None:
171168

172169
assert isinstance(result, RetryStrategyOptions)
173170
assert result.retry_mode == "standard"
174-
assert result.max_attempts == 3
171+
assert result.max_attempts is None
175172
assert source_info == SimpleSource("default")
176173

177174
def test_validator_not_called_on_cached_access(self) -> None:

packages/smithy-aws-core/tests/unit/config/test_validators.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
validate_max_attempts,
99
validate_region,
1010
validate_retry_mode,
11+
validate_ua_string,
1112
)
1213

1314

@@ -49,3 +50,20 @@ def test_invalid_retry_mode_error_message(self) -> None:
4950
"Invalid value for 'retry_mode': 'random_mode'. retry_mode must be one "
5051
"of ('standard',), got random_mode" in str(exc_info.value)
5152
)
53+
54+
55+
class TestValidateUaString:
56+
def test_allows_string(self) -> None:
57+
assert validate_ua_string("abc123") == "abc123"
58+
59+
def test_none_returns_none(self) -> None:
60+
assert validate_ua_string(None) is None
61+
62+
def test_empty_string_passthrough(self) -> None:
63+
assert validate_ua_string("") == ""
64+
65+
def test_rejects_non_string(self) -> None:
66+
with pytest.raises(ConfigValidationError) as exc_info:
67+
validate_ua_string(123)
68+
69+
assert exc_info.value.key == "sdk_ua_app_id"

0 commit comments

Comments
 (0)