Skip to content

Commit 83a0a8f

Browse files
committed
Cleanups from PR (still todo: seperate into generic user agent and aws user agent).
1 parent d5473b5 commit 83a0a8f

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package software.amazon.smithy.python.aws.codegen;
66

7-
import java.util.Collections;
87
import java.util.List;
98
import software.amazon.smithy.codegen.core.Symbol;
109
import software.amazon.smithy.codegen.core.SymbolReference;
@@ -24,12 +23,11 @@ public List<RuntimeClientPlugin> getClientPlugins() {
2423
RuntimeClientPlugin.builder()
2524
.addConfigProperty(
2625
ConfigProperty.builder()
27-
// TODO: This is the name used in boto, but potentially could be user_agent_prefix. Depends on backwards compat strategy.
28-
.name("user_agent_extra")
29-
.documentation("Additional suffix to be added to the User-Agent header.")
30-
.type(Symbol.builder().name("str").build()) // TODO: Should common types like this be defined as constants somewhere?
31-
.nullable(true)
32-
.build())
26+
.name("user_agent_extra")
27+
.documentation("Additional suffix to be added to the User-Agent header.")
28+
.type(Symbol.builder().name("str").build())
29+
.nullable(true)
30+
.build())
3331
.addConfigProperty(
3432
ConfigProperty.builder()
3533
.name("sdk_ua_app_id")

packages/smithy-aws-core/src/smithy_aws_core/interceptors/user_agent.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ class UserAgentInterceptor(Interceptor[Request, None, HTTPRequest, None]):
1212

1313
def __init__(
1414
self,
15+
*,
1516
ua_suffix: str | None = None,
1617
ua_app_id: str | None = None,
1718
sdk_version: str | None = "0.0.1",
1819
) -> None:
1920
"""Initialize the UserAgentInterceptor.
2021
21-
:ua_suffix: Additional suffix to be added to the UserAgent header. :ua_app_id:
22-
User defined and opaque application ID to be added to the UserAgent header.
22+
:param ua_suffix: Additional suffix to be added to the UserAgent header.
23+
:param ua_app_id: User defined and opaque application ID to be added to the
24+
UserAgent header.
25+
:param sdk_version: SDK version to be added to the UserAgent header.
2326
"""
2427
super().__init__()
2528
self._ua_suffix = ua_suffix

packages/smithy-aws-core/src/smithy_aws_core/user_agent.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
import os
66
import platform
7+
from dataclasses import dataclass
78
from string import ascii_letters, digits
8-
from typing import NamedTuple, Optional, Self, Union, List
9+
from typing import Self
910

1011
from smithy_http.aio.crt import HAS_CRT
1112

@@ -24,7 +25,8 @@
2425
_USERAGENT_SDK_NAME = "aws-sdk-python"
2526

2627

27-
class UserAgentComponent(NamedTuple):
28+
@dataclass(frozen=True, slots=True)
29+
class UserAgentComponent:
2830
"""Component of a User-Agent header string in the standard format.
2931
3032
Each component consists of a prefix, a name, and a value. In the string
@@ -37,7 +39,7 @@ class UserAgentComponent(NamedTuple):
3739
name: str
3840
value: str | None = None
3941

40-
def to_string(self):
42+
def __str__(self):
4143
"""Create string like 'prefix/name#value' from a UserAgentComponent."""
4244
clean_prefix = sanitize_user_agent_string_component(
4345
self.prefix, allow_hash=True
@@ -59,11 +61,11 @@ class RawStringUserAgentComponent:
5961
def __init__(self, value: str):
6062
self._value = value
6163

62-
def to_string(self) -> str:
64+
def __str__(self) -> str:
6365
return self._value
6466

6567

66-
_UAComponent = Union[UserAgentComponent, RawStringUserAgentComponent]
68+
_UAComponent = UserAgentComponent | RawStringUserAgentComponent
6769

6870

6971
class UserAgent:
@@ -92,17 +94,14 @@ def __init__(
9294

9395
@classmethod
9496
def from_environment(cls) -> Self:
95-
crt_version = None
96-
if HAS_CRT:
97-
crt_version = _get_crt_version() or "Unknown"
9897
return cls(
9998
platform_name=platform.system(),
10099
platform_version=platform.release(),
101100
platform_machine=platform.machine(),
102101
python_version=platform.python_version(),
103102
python_implementation=platform.python_implementation(),
104103
execution_env=os.environ.get("AWS_EXECUTION_ENV"),
105-
crt_version=crt_version,
104+
crt_version=_get_crt_version(),
106105
)
107106

108107
def with_config(
@@ -130,7 +129,7 @@ def to_string(self) -> str:
130129
*self._build_suffix(),
131130
]
132131

133-
return " ".join([comp.to_string() for comp in components])
132+
return " ".join([str(comp) for comp in components])
134133

135134
def _build_sdk_metadata(self) -> list[UserAgentComponent]:
136135
"""Build the SDK name and version component of the User-Agent header.
@@ -261,9 +260,9 @@ def sanitize_user_agent_string_component(raw_str: str, allow_hash: bool = False)
261260

262261
def _get_crt_version() -> str | None:
263262
"""This function is considered private and is subject to abrupt breaking changes."""
264-
try:
263+
if HAS_CRT:
265264
import awscrt
266265

267266
return awscrt.__version__
268-
except AttributeError:
267+
else:
269268
return None

0 commit comments

Comments
 (0)