Skip to content

Commit b7e15d1

Browse files
committed
Add Config/HttpConfig Protocols + generate __version__
1 parent 83a0a8f commit b7e15d1

File tree

6 files changed

+69
-14
lines changed

6 files changed

+69
-14
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ public List<RuntimeClientPlugin> getClientPlugins() {
3131
.addConfigProperty(
3232
ConfigProperty.builder()
3333
.name("sdk_ua_app_id")
34-
.documentation("A unique and opaque application ID that is appended to the User-Agent header.")
34+
.documentation(
35+
"A unique and opaque application ID that is appended to the User-Agent header.")
3536
.type(Symbol.builder().name("str").build())
3637
.nullable(true)
37-
.build()
38-
)
38+
.build())
3939
.pythonPlugin(
4040
SymbolReference.builder()
4141
.symbol(Symbol.builder()
42-
.namespace(AwsPythonDependency.SMITHY_AWS_CORE.packageName() + ".plugins", ".")
42+
.namespace(
43+
AwsPythonDependency.SMITHY_AWS_CORE.packageName() + ".plugins",
44+
".")
4345
.name("user_agent_plugin")
4446
.addDependency(AwsPythonDependency.SMITHY_AWS_CORE)
4547
.build())
46-
.build()
47-
)
48-
.build()
49-
);
48+
.build())
49+
.build());
5050
}
5151

5252
}

codegen/core/src/main/java/software/amazon/smithy/python/codegen/DirectedPythonCodegen.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,24 @@ public void generateIntEnumShape(GenerateIntEnumDirective<GenerationContext, Pyt
273273

274274
@Override
275275
public void customizeBeforeIntegrations(CustomizeDirective<GenerationContext, PythonSettings> directive) {
276+
generateServiceModuleInit(directive);
276277
generateInits(directive);
277278
}
278279

280+
/**
281+
* Creates top level __init__.py file.
282+
*/
283+
private void generateServiceModuleInit(CustomizeDirective<GenerationContext, PythonSettings> directive) {
284+
directive.context()
285+
.writerDelegator()
286+
.useFileWriter(
287+
"%s/__init__.py".formatted(directive.context().settings().moduleName()),
288+
writer -> {
289+
writer
290+
.write("__version__ = '$L'", directive.context().settings().moduleVersion());
291+
});
292+
}
293+
279294
/**
280295
* Creates __init__.py files where not already present.
281296
*/

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/ConfigGenerator.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,19 @@ private void writeInterceptorsType(PythonWriter writer) {
289289
}
290290

291291
private void generateConfig(GenerationContext context, PythonWriter writer) {
292-
var symbol = CodegenUtils.getConfigSymbol(context.settings());
292+
var configSymbol = CodegenUtils.getConfigSymbol(context.settings());
293+
Symbol configProtocolSymbol = null;
294+
if (context.applicationProtocol().isHttpProtocol()) {
295+
configProtocolSymbol = Symbol.builder()
296+
.name("HttpConfig")
297+
.namespace("smithy_http.interfaces.config", ".")
298+
.build();
299+
} else {
300+
configProtocolSymbol = Symbol.builder()
301+
.name("Config")
302+
.namespace("smithy_core.interfaces.config", ".")
303+
.build();
304+
}
293305

294306
// Initialize the list of config properties with our base properties. Here a new
295307
// list is constructed because that base list is immutable.
@@ -324,7 +336,7 @@ private void generateConfig(GenerationContext context, PythonWriter writer) {
324336
writer.addStdlibImport("dataclasses", "dataclass");
325337
writer.write("""
326338
@dataclass(init=False)
327-
class $L:
339+
class $T($T):
328340
\"""Configuration for $L.\"""
329341
330342
${C|}
@@ -340,7 +352,8 @@ def __init__(
340352
\"""
341353
${C|}
342354
""",
343-
symbol.getName(),
355+
configSymbol,
356+
configProtocolSymbol,
344357
context.settings().service().getName(),
345358
writer.consumer(w -> writePropertyDeclarations(w, finalProperties)),
346359
writer.consumer(w -> writeInitParams(w, finalProperties)),

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ def __str__(self):
5151
return f"{clean_prefix}/{clean_name}#{clean_value}"
5252

5353

54+
@dataclass(frozen=True, slots=True)
5455
class RawStringUserAgentComponent:
5556
"""UserAgentComponent interface wrapper around ``str``.
5657
5758
Use for User-Agent header components that are not constructed from prefix+name+value
5859
but instead are provided as strings. No sanitization is performed.
5960
"""
6061

61-
def __init__(self, value: str):
62-
self._value = value
62+
value: str
6363

6464
def __str__(self) -> str:
65-
return self._value
65+
return self.value
6666

6767

6868
_UAComponent = UserAgentComponent | RawStringUserAgentComponent
@@ -71,6 +71,7 @@ def __str__(self) -> str:
7171
class UserAgent:
7272
def __init__(
7373
self,
74+
*,
7475
platform_name: str | None,
7576
platform_version: str | None,
7677
platform_machine: str | None,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from typing import Protocol, Any
4+
5+
from smithy_core.interceptors import Interceptor
6+
from smithy_core.interfaces.retries import RetryStrategy
7+
8+
9+
class Config(Protocol):
10+
interceptors: list[Interceptor[Any, Any, Any, Any]]
11+
retry_strategy: RetryStrategy
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from typing import Any
4+
5+
from smithy_core.interfaces import URI
6+
from smithy_core.interfaces.config import Config
7+
from smithy_http.aio.interfaces import HTTPClient, EndpointResolver
8+
from smithy_http.interfaces import HTTPRequestConfiguration
9+
10+
11+
class HttpConfig(Config):
12+
http_client: HTTPClient
13+
http_request_config: HTTPRequestConfiguration | None
14+
endpoint_resolver: EndpointResolver[Any]
15+
endpoint_uri: str | URI | None

0 commit comments

Comments
 (0)