Skip to content

Commit 9c80e0a

Browse files
Pull resolver params from config
1 parent dda850b commit 9c80e0a

File tree

8 files changed

+85
-50
lines changed

8 files changed

+85
-50
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ await sleep(retry_token.retry_delay)
530530
writer.addImport("smithy_core.endpoints", "EndpointResolverParams");
531531
writer.write("""
532532
# Step 7f: Invoke endpoint_resolver.resolve_endpoint
533-
context.properties["endpoint_uri"] = config.endpoint_uri
534533
endpoint_resolver_parameters = EndpointResolverParams(
535534
operation=operation,
536535
input=context.request,

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,3 @@
44
import importlib.metadata
55

66
__version__: str = importlib.metadata.version("smithy-aws-core")
7-
8-
9-
from smithy_core.types import PropertyKey
10-
11-
12-
REGION = PropertyKey(key="region", value_type=str)
13-
"""An AWS region."""
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3+
from smithy_core.endpoints import StaticEndpointConfig
4+
from smithy_core.types import PropertyKey
5+
6+
7+
class RegionalEndpointConfig(StaticEndpointConfig):
8+
"""Endpoint config for services with standard regional endpoints."""
9+
10+
region: str | None
11+
"""The AWS region to address the request to."""
12+
13+
14+
REGIONAL_ENDPOINT_CONFIG = PropertyKey(key="config", value_type=RegionalEndpointConfig)
15+
"""Endpoint config for services with standard regional endpoints."""

packages/smithy-aws-core/src/smithy_aws_core/endpoints/standard_regional.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from smithy_core.endpoints import Endpoint, EndpointResolverParams, resolve_static_uri
88
from smithy_core.exceptions import EndpointResolutionError
99

10-
from .. import REGION
10+
from . import REGIONAL_ENDPOINT_CONFIG
1111

1212

1313
class StandardRegionalEndpointsResolver(EndpointResolver):
@@ -20,10 +20,11 @@ async def resolve_endpoint(self, params: EndpointResolverParams[Any]) -> Endpoin
2020
if (static_uri := resolve_static_uri(params)) is not None:
2121
return Endpoint(uri=static_uri)
2222

23-
if (region := params.context.get(REGION)) is not None:
23+
region_config = params.context.get(REGIONAL_ENDPOINT_CONFIG)
24+
if region_config is not None and region_config.region is not None:
2425
# TODO: use dns suffix determined from partition metadata
2526
dns_suffix = "amazonaws.com"
26-
hostname = f"{self._endpoint_prefix}.{region}.{dns_suffix}"
27+
hostname = f"{self._endpoint_prefix}.{region_config.region}.{dns_suffix}"
2728

2829
return Endpoint(uri=URI(host=hostname))
2930

packages/smithy-aws-core/tests/unit/endpoints/test_standard_regional.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3+
from dataclasses import dataclass
4+
from typing import Any
35
from unittest.mock import Mock
46

57
import pytest
68

79
from smithy_core import URI
8-
from smithy_core.endpoints import STATIC_URI, EndpointResolverParams
10+
from smithy_core.endpoints import EndpointResolverParams
911
from smithy_core.types import TypedProperties
1012
from smithy_core.exceptions import EndpointResolutionError
1113

12-
from smithy_aws_core import REGION
14+
from smithy_aws_core.endpoints import REGIONAL_ENDPOINT_CONFIG
1315
from smithy_aws_core.endpoints.standard_regional import (
1416
StandardRegionalEndpointsResolver,
1517
)
1618

1719

20+
@dataclass
21+
class EndpointConfig:
22+
endpoint_uri: str | URI | None = None
23+
region: str | None = None
24+
25+
@classmethod
26+
def params(
27+
cls, endpoint_uri: str | URI | None = None, region: str | None = None
28+
) -> EndpointResolverParams[Any]:
29+
properties = TypedProperties(
30+
{
31+
REGIONAL_ENDPOINT_CONFIG.key: cls(
32+
endpoint_uri=endpoint_uri, region=region
33+
)
34+
}
35+
)
36+
params = Mock(spec=EndpointResolverParams)
37+
params.context = properties
38+
return params
39+
40+
1841
async def test_resolve_endpoint_with_valid_sdk_endpoint_string():
1942
resolver = StandardRegionalEndpointsResolver(endpoint_prefix="service")
20-
params = Mock(spec=EndpointResolverParams)
21-
params.context = TypedProperties(
22-
{STATIC_URI.key: "https://example.com/path?query=123"}
23-
)
43+
params = EndpointConfig.params("https://example.com/path?query=123")
2444

2545
endpoint = await resolver.resolve_endpoint(params)
2646

@@ -35,8 +55,7 @@ async def test_resolve_endpoint_with_sdk_endpoint_uri():
3555
parsed_uri = URI(
3656
host="example.com", path="/path", scheme="https", query="query=123", port=443
3757
)
38-
params = Mock(spec=EndpointResolverParams)
39-
params.context = TypedProperties({STATIC_URI.key: parsed_uri})
58+
params = EndpointConfig.params(parsed_uri)
4059

4160
endpoint = await resolver.resolve_endpoint(params)
4261

@@ -45,17 +64,15 @@ async def test_resolve_endpoint_with_sdk_endpoint_uri():
4564

4665
async def test_resolve_endpoint_with_invalid_sdk_endpoint():
4766
resolver = StandardRegionalEndpointsResolver(endpoint_prefix="service")
48-
params = Mock(spec=EndpointResolverParams)
49-
params.context = TypedProperties({STATIC_URI.key: "invalid_uri"})
67+
params = EndpointConfig.params("invalid_uri")
5068

5169
with pytest.raises(EndpointResolutionError):
5270
await resolver.resolve_endpoint(params)
5371

5472

5573
async def test_resolve_endpoint_with_region():
5674
resolver = StandardRegionalEndpointsResolver(endpoint_prefix="service")
57-
params = Mock(spec=EndpointResolverParams)
58-
params.context = TypedProperties({REGION.key: "us-west-2"})
75+
params = EndpointConfig.params(region="us-west-2")
5976

6077
endpoint = await resolver.resolve_endpoint(params)
6178

@@ -64,18 +81,16 @@ async def test_resolve_endpoint_with_region():
6481

6582
async def test_resolve_endpoint_with_no_sdk_endpoint_or_region():
6683
resolver = StandardRegionalEndpointsResolver(endpoint_prefix="service")
67-
params = Mock(spec=EndpointResolverParams)
68-
params.context = TypedProperties()
84+
params = EndpointConfig.params()
6985

7086
with pytest.raises(EndpointResolutionError):
7187
await resolver.resolve_endpoint(params)
7288

7389

7490
async def test_resolve_endpoint_with_sdk_endpoint_and_region():
7591
resolver = StandardRegionalEndpointsResolver(endpoint_prefix="service")
76-
params = Mock(spec=EndpointResolverParams)
77-
params.context = TypedProperties(
78-
{STATIC_URI.key: "https://example.com", REGION.key: "us-west-2"}
92+
params = EndpointConfig.params(
93+
endpoint_uri="https://example.com", region="us-west-2"
7994
)
8095

8196
endpoint = await resolver.resolve_endpoint(params)

packages/smithy-core/src/smithy_core/endpoints.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
from typing import Any
3+
from typing import Any, Protocol
44
from dataclasses import dataclass, field
55
from urllib.parse import urlparse
66

@@ -14,17 +14,6 @@
1414
from .exceptions import EndpointResolutionError
1515

1616

17-
STATIC_URI: PropertyKey[str | _URI] = PropertyKey(
18-
key="endpoint_uri",
19-
# Python currently has problems expressing parametric types that can be
20-
# unions, literals, or other special types in addition to a class. So
21-
# we have to ignore the type below. PEP 747 should resolve the issue.
22-
# TODO: update this when PEP 747 lands
23-
value_type=str | _URI, # type: ignore
24-
)
25-
"""The property key for a statically defined URI."""
26-
27-
2817
@dataclass(kw_only=True)
2918
class Endpoint(_Endpoint):
3019
"""A resolved endpoint."""
@@ -54,6 +43,17 @@ class EndpointResolverParams[I: SerializeableShape]:
5443
"""The context of the operation invocation."""
5544

5645

46+
class StaticEndpointConfig(Protocol):
47+
"""A config that has a static endpoint."""
48+
49+
endpoint_uri: str | URI | None
50+
"""A static endpoint to use for the request."""
51+
52+
53+
STATIC_ENDPOINT_CONFIG = PropertyKey(key="config", value_type=StaticEndpointConfig)
54+
"""Property containing a config that has a static endpoint."""
55+
56+
5757
def resolve_static_uri(
5858
properties: _TypedProperties | EndpointResolverParams[Any],
5959
) -> _URI | None:
@@ -66,10 +66,12 @@ def resolve_static_uri(
6666
if isinstance(properties, EndpointResolverParams)
6767
else properties
6868
)
69-
static_uri = properties.get(STATIC_URI)
70-
if static_uri is None:
69+
static_uri_config = properties.get(STATIC_ENDPOINT_CONFIG)
70+
if static_uri_config is None or static_uri_config.endpoint_uri is None:
7171
return None
7272

73+
static_uri = static_uri_config.endpoint_uri
74+
7375
# If it's not a string, it's already a parsed URI so just pass it along.
7476
if not isinstance(static_uri, str):
7577
return static_uri

packages/smithy-core/src/smithy_core/interfaces/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ class PropertyKey[T](Protocol):
136136
key: str
137137
"""The string key used to access the value."""
138138

139-
# TODO: update this when PEP 747 lands to allow for unions and literals
140139
value_type: type[T]
141140
"""The type of the associated value in the properties bag."""
142141

packages/smithy-core/tests/unit/aio/test_endpoints.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3+
from dataclasses import dataclass
4+
from typing import Any
35
from unittest.mock import Mock
46
from smithy_core.types import TypedProperties
5-
from smithy_core.endpoints import EndpointResolverParams, STATIC_URI
7+
from smithy_core.endpoints import EndpointResolverParams, STATIC_ENDPOINT_CONFIG
68
from smithy_core.aio.endpoints import StaticEndpointResolver
79
from smithy_core import URI
810

911

12+
@dataclass
13+
class EndpointConfig:
14+
endpoint_uri: str | URI | None = None
15+
16+
@classmethod
17+
def params(
18+
cls, endpoint_uri: str | URI | None = None
19+
) -> EndpointResolverParams[Any]:
20+
params = Mock(spec=EndpointResolverParams)
21+
params.context = TypedProperties(
22+
{STATIC_ENDPOINT_CONFIG.key: cls(endpoint_uri)}
23+
)
24+
return params
25+
26+
1027
async def test_endpoint_provider_with_uri_string() -> None:
11-
params = Mock(spec=EndpointResolverParams)
12-
params.context = TypedProperties(
13-
{STATIC_URI.key: "https://foo.example.com:8080/spam?foo=bar&foo=baz"}
14-
)
28+
params = EndpointConfig.params("https://foo.example.com:8080/spam?foo=bar&foo=baz")
1529
expected = URI(
1630
host="foo.example.com",
1731
path="/spam",
@@ -32,8 +46,7 @@ async def test_endpoint_provider_with_uri_object() -> None:
3246
query="foo=bar&foo=baz",
3347
port=8080,
3448
)
35-
params = Mock(spec=EndpointResolverParams)
36-
params.context = TypedProperties({STATIC_URI.key: expected})
49+
params = EndpointConfig.params(expected)
3750
resolver = StaticEndpointResolver()
3851
result = await resolver.resolve_endpoint(params=params)
3952
assert result.uri == expected

0 commit comments

Comments
 (0)