smithy-http 0.5.0__tar.gz → 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- smithy_http-0.6.0/.changes/0.6.0.json +20 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/CHANGELOG.md +10 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/PKG-INFO +3 -1
- {smithy_http-0.5.0 → smithy_http-0.6.0}/pyproject.toml +3 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/__init__.py +13 -5
- smithy_http-0.6.0/src/smithy_http/aio/protocols.py +518 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/bindings.py +4 -3
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/deserializers.py +193 -57
- smithy_http-0.6.0/src/smithy_http/schema_extensions.py +490 -0
- smithy_http-0.6.0/src/smithy_http/serializers.py +1050 -0
- smithy_http-0.5.0/src/smithy_http/aio/protocols.py +0 -273
- smithy_http-0.5.0/src/smithy_http/serializers.py +0 -785
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.0.1.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.1.0.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.2.0.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.2.1.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.3.0.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.3.1.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.4.0.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.4.1.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.4.2.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.4.3.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.4.4.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.changes/0.5.0.json +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/.gitignore +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/NOTICE +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/README.md +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/__init__.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/aiohttp.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/auth/__init__.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/auth/apikey.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/crt.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/identity/__init__.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/identity/apikey.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/interfaces/__init__.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/aio/restjson.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/endpoints.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/exceptions.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/interceptors/__init__.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/interceptors/user_agent.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/interfaces/__init__.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/plugins/__init__.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/py.typed +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/restjson.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/testing/__init__.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/testing/mockhttp.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/testing/utils.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/user_agent.py +0 -0
- {smithy_http-0.5.0 → smithy_http-0.6.0}/src/smithy_http/utils.py +0 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"changes": [
|
|
3
|
+
{
|
|
4
|
+
"type": "feature",
|
|
5
|
+
"description": "`HttpClientProtocol` implements `extract_response_metadata()`, reporting the HTTP status code."
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
"type": "feature",
|
|
9
|
+
"description": "Added a naive `RpcV2CborClientProtocol` for the `smithy.protocols#rpcv2Cbor` protocol (optional `cbor` extra)."
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"type": "feature",
|
|
13
|
+
"description": "Added schema-cached HTTP binding plans for members, operations, payloads, and request and response dispatch."
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"type": "dependency",
|
|
17
|
+
"description": "Bump `smithy-cbor` from `~=0.0.0` to `~=0.1.0`."
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## v0.6.0
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
* `HttpClientProtocol` implements `extract_response_metadata()`, reporting the HTTP status code.
|
|
7
|
+
* Added a naive `RpcV2CborClientProtocol` for the `smithy.protocols#rpcv2Cbor` protocol (optional `cbor` extra).
|
|
8
|
+
* Added schema-cached HTTP binding plans for members, operations, payloads, and request and response dispatch.
|
|
9
|
+
|
|
10
|
+
### Dependencies
|
|
11
|
+
* Bump `smithy-cbor` from `~=0.0.0` to `~=0.1.0`.
|
|
12
|
+
|
|
3
13
|
## v0.5.0
|
|
4
14
|
|
|
5
15
|
### Features
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: smithy-http
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: HTTP components for Smithy tooling.
|
|
5
5
|
Project-URL: Changelog, https://github.com/smithy-lang/smithy-python/blob/develop/packages/smithy-http/CHANGELOG.md
|
|
6
6
|
Project-URL: Code, https://github.com/smithy-lang/smithy-python/blob/develop/packages/smithy-http/
|
|
@@ -31,6 +31,8 @@ Requires-Dist: aiohttp<4.0,>=3.11.12; extra == 'aiohttp'
|
|
|
31
31
|
Requires-Dist: yarl; extra == 'aiohttp'
|
|
32
32
|
Provides-Extra: awscrt
|
|
33
33
|
Requires-Dist: awscrt~=0.32.0; extra == 'awscrt'
|
|
34
|
+
Provides-Extra: cbor
|
|
35
|
+
Requires-Dist: smithy-cbor~=0.1.0; extra == 'cbor'
|
|
34
36
|
Description-Content-Type: text/markdown
|
|
35
37
|
|
|
36
38
|
# smithy-http
|
|
@@ -7,7 +7,7 @@ from typing import get_args
|
|
|
7
7
|
from . import interfaces
|
|
8
8
|
from .interfaces import FieldPosition
|
|
9
9
|
|
|
10
|
-
__version__ = "0.
|
|
10
|
+
__version__ = "0.6.0"
|
|
11
11
|
|
|
12
12
|
_VALID_FIELD_POSITIONS = frozenset(get_args(FieldPosition))
|
|
13
13
|
|
|
@@ -226,10 +226,18 @@ def tuples_to_fields(
|
|
|
226
226
|
:param kind: The Field kind to define for all tuples.
|
|
227
227
|
"""
|
|
228
228
|
fields = Fields()
|
|
229
|
+
entries = fields.entries
|
|
230
|
+
field_kind = kind or "header"
|
|
229
231
|
for name, value in tuples:
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
232
|
+
normalized_name = name.lower()
|
|
233
|
+
field = entries.get(normalized_name)
|
|
234
|
+
if field is None:
|
|
235
|
+
entries[normalized_name] = Field(
|
|
236
|
+
name=name,
|
|
237
|
+
values=[value],
|
|
238
|
+
kind=field_kind,
|
|
239
|
+
)
|
|
240
|
+
else:
|
|
241
|
+
field.values.append(value)
|
|
234
242
|
|
|
235
243
|
return fields
|
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import os
|
|
4
|
+
from collections.abc import AsyncIterable
|
|
5
|
+
from inspect import iscoroutinefunction
|
|
6
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
7
|
+
|
|
8
|
+
from smithy_core import URI as _URI
|
|
9
|
+
from smithy_core.aio.interfaces import AsyncByteStream, ClientProtocol, ProtocolSettings
|
|
10
|
+
from smithy_core.aio.interfaces import StreamingBlob as AsyncStreamingBlob
|
|
11
|
+
from smithy_core.aio.types import AsyncBytesReader
|
|
12
|
+
from smithy_core.codecs import Codec
|
|
13
|
+
from smithy_core.deserializers import DeserializeableShape
|
|
14
|
+
from smithy_core.documents import TypeRegistry
|
|
15
|
+
from smithy_core.exceptions import (
|
|
16
|
+
CallError,
|
|
17
|
+
ExpectationNotMetError,
|
|
18
|
+
MissingDependencyError,
|
|
19
|
+
ModeledError,
|
|
20
|
+
)
|
|
21
|
+
from smithy_core.interfaces import (
|
|
22
|
+
Endpoint,
|
|
23
|
+
SeekableBytesReader,
|
|
24
|
+
TypedProperties,
|
|
25
|
+
URI,
|
|
26
|
+
)
|
|
27
|
+
from smithy_core.interfaces import StreamingBlob as SyncStreamingBlob
|
|
28
|
+
from smithy_core.prelude import DOCUMENT, UNIT
|
|
29
|
+
from smithy_core.response import ResponseMetadata
|
|
30
|
+
from smithy_core.schemas import APIOperation, Schema
|
|
31
|
+
from smithy_core.serializers import SerializeableShape, SerializeableStruct
|
|
32
|
+
from smithy_core.shapes import ShapeID
|
|
33
|
+
from smithy_core.traits import (
|
|
34
|
+
ORIGINAL_SHAPE_ID,
|
|
35
|
+
RpcV2CborTrait,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
from .. import tuples_to_fields
|
|
39
|
+
from ..deserializers import HTTPResponseDeserializer
|
|
40
|
+
from ..schema_extensions import (
|
|
41
|
+
HTTP_BINDING_SCHEMA_EXTENSION,
|
|
42
|
+
HTTP_OPERATION_SCHEMA_EXTENSION,
|
|
43
|
+
)
|
|
44
|
+
from ..serializers import HTTPBindingSerializer, HTTPRequestSerializer
|
|
45
|
+
from . import HTTPRequest as _HTTPRequest
|
|
46
|
+
from .interfaces import HTTPErrorIdentifier, HTTPRequest, HTTPResponse
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class HttpClientProtocol(ClientProtocol[HTTPRequest, HTTPResponse]):
|
|
50
|
+
"""An HTTP-based protocol."""
|
|
51
|
+
|
|
52
|
+
def set_service_endpoint(
|
|
53
|
+
self,
|
|
54
|
+
*,
|
|
55
|
+
request: HTTPRequest,
|
|
56
|
+
endpoint: Endpoint,
|
|
57
|
+
) -> HTTPRequest:
|
|
58
|
+
uri = endpoint.uri
|
|
59
|
+
previous = request.destination
|
|
60
|
+
|
|
61
|
+
path = previous.path or uri.path
|
|
62
|
+
if uri.path is not None and previous.path is not None:
|
|
63
|
+
path = os.path.join(uri.path, previous.path.lstrip("/"))
|
|
64
|
+
|
|
65
|
+
if path is not None and not path.startswith("/"):
|
|
66
|
+
path = "/" + path
|
|
67
|
+
|
|
68
|
+
query = previous.query or uri.query
|
|
69
|
+
if uri.query and previous.query:
|
|
70
|
+
query = f"{uri.query}&{previous.query}"
|
|
71
|
+
|
|
72
|
+
request.destination = _URI(
|
|
73
|
+
scheme=uri.scheme,
|
|
74
|
+
username=uri.username or previous.username,
|
|
75
|
+
password=uri.password or previous.password,
|
|
76
|
+
host=uri.host,
|
|
77
|
+
port=uri.port or previous.port,
|
|
78
|
+
path=path,
|
|
79
|
+
query=query,
|
|
80
|
+
fragment=uri.fragment or previous.fragment,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
return request
|
|
84
|
+
|
|
85
|
+
def extract_response_metadata(
|
|
86
|
+
self,
|
|
87
|
+
*,
|
|
88
|
+
response: HTTPResponse,
|
|
89
|
+
context: TypedProperties,
|
|
90
|
+
) -> ResponseMetadata:
|
|
91
|
+
"""Extract the status code from an HTTP response.
|
|
92
|
+
|
|
93
|
+
Identifiers such as request IDs are not part of HTTP itself, so protocols
|
|
94
|
+
that define them are expected to override this and add them.
|
|
95
|
+
"""
|
|
96
|
+
return ResponseMetadata(http_status_code=response.status)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class HttpBindingClientProtocol(HttpClientProtocol):
|
|
100
|
+
"""An HTTP-based protocol that uses HTTP binding traits."""
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def payload_codec(self) -> Codec:
|
|
104
|
+
"""The codec used for the serde of input and output payloads."""
|
|
105
|
+
raise NotImplementedError()
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
def content_type(self) -> str:
|
|
109
|
+
"""The media type of the http payload."""
|
|
110
|
+
raise NotImplementedError()
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def error_identifier(self) -> HTTPErrorIdentifier:
|
|
114
|
+
"""The class used to identify the shape IDs of errors based on fields or other
|
|
115
|
+
response information."""
|
|
116
|
+
raise NotImplementedError()
|
|
117
|
+
|
|
118
|
+
def serialize_request[
|
|
119
|
+
OperationInput: "SerializeableShape",
|
|
120
|
+
OperationOutput: "DeserializeableShape",
|
|
121
|
+
](
|
|
122
|
+
self,
|
|
123
|
+
*,
|
|
124
|
+
operation: APIOperation[OperationInput, OperationOutput],
|
|
125
|
+
input: OperationInput,
|
|
126
|
+
endpoint: URI,
|
|
127
|
+
context: TypedProperties,
|
|
128
|
+
) -> HTTPRequest:
|
|
129
|
+
operation_metadata = operation.schema.get_extension(
|
|
130
|
+
HTTP_OPERATION_SCHEMA_EXTENSION
|
|
131
|
+
)
|
|
132
|
+
if isinstance(input, SerializeableStruct):
|
|
133
|
+
serializer = HTTPBindingSerializer(
|
|
134
|
+
payload_codec=self.payload_codec,
|
|
135
|
+
schema=operation.input_schema,
|
|
136
|
+
http_trait=operation_metadata.http_trait,
|
|
137
|
+
operation_metadata=operation_metadata,
|
|
138
|
+
)
|
|
139
|
+
try:
|
|
140
|
+
input.serialize_members(serializer)
|
|
141
|
+
except BaseException as error:
|
|
142
|
+
serializer.abort(type(error), error, error.__traceback__)
|
|
143
|
+
raise
|
|
144
|
+
return serializer.build_request()
|
|
145
|
+
|
|
146
|
+
serializer = HTTPRequestSerializer(
|
|
147
|
+
payload_codec=self.payload_codec,
|
|
148
|
+
http_trait=operation_metadata.http_trait,
|
|
149
|
+
operation_metadata=operation_metadata,
|
|
150
|
+
)
|
|
151
|
+
input.serialize(serializer)
|
|
152
|
+
if serializer.result is None:
|
|
153
|
+
raise ExpectationNotMetError(
|
|
154
|
+
"Expected request to be serialized, but was None"
|
|
155
|
+
)
|
|
156
|
+
return serializer.result
|
|
157
|
+
|
|
158
|
+
async def deserialize_response[
|
|
159
|
+
OperationInput: "SerializeableShape",
|
|
160
|
+
OperationOutput: "DeserializeableShape",
|
|
161
|
+
](
|
|
162
|
+
self,
|
|
163
|
+
*,
|
|
164
|
+
operation: APIOperation[OperationInput, OperationOutput],
|
|
165
|
+
request: HTTPRequest,
|
|
166
|
+
response: HTTPResponse,
|
|
167
|
+
error_registry: TypeRegistry,
|
|
168
|
+
context: TypedProperties,
|
|
169
|
+
) -> OperationOutput:
|
|
170
|
+
if not self._is_success(operation, context, response):
|
|
171
|
+
raise await self._create_error(
|
|
172
|
+
operation=operation,
|
|
173
|
+
request=request,
|
|
174
|
+
response=response,
|
|
175
|
+
response_body=await self._buffer_async_body(response.body),
|
|
176
|
+
error_registry=error_registry,
|
|
177
|
+
context=context,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
# if body is not streaming and is async, we have to buffer it
|
|
181
|
+
body: SyncStreamingBlob | None = None
|
|
182
|
+
response_metadata = operation.output_schema.get_extension(
|
|
183
|
+
HTTP_BINDING_SCHEMA_EXTENSION
|
|
184
|
+
).response
|
|
185
|
+
if response_metadata.streaming_member is None:
|
|
186
|
+
body = await self._buffer_async_body(response.body)
|
|
187
|
+
|
|
188
|
+
deserializer = HTTPResponseDeserializer(
|
|
189
|
+
payload_codec=self.payload_codec,
|
|
190
|
+
response=response,
|
|
191
|
+
body=body,
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
return operation.output.deserialize(deserializer)
|
|
195
|
+
|
|
196
|
+
async def _buffer_async_body(self, stream: AsyncStreamingBlob) -> SyncStreamingBlob:
|
|
197
|
+
if isinstance(stream, bytes | bytearray):
|
|
198
|
+
return stream
|
|
199
|
+
|
|
200
|
+
match stream:
|
|
201
|
+
case AsyncByteStream():
|
|
202
|
+
if not iscoroutinefunction(stream.read):
|
|
203
|
+
return stream # type: ignore
|
|
204
|
+
return await stream.read()
|
|
205
|
+
case AsyncIterable():
|
|
206
|
+
full = b""
|
|
207
|
+
async for chunk in stream:
|
|
208
|
+
full += chunk
|
|
209
|
+
return full
|
|
210
|
+
case _:
|
|
211
|
+
return stream
|
|
212
|
+
|
|
213
|
+
def _is_success(
|
|
214
|
+
self,
|
|
215
|
+
operation: APIOperation[Any, Any],
|
|
216
|
+
context: TypedProperties,
|
|
217
|
+
response: HTTPResponse,
|
|
218
|
+
) -> bool:
|
|
219
|
+
return 200 <= response.status < 300
|
|
220
|
+
|
|
221
|
+
async def _create_error(
|
|
222
|
+
self,
|
|
223
|
+
operation: APIOperation[Any, Any],
|
|
224
|
+
request: HTTPRequest,
|
|
225
|
+
response: HTTPResponse,
|
|
226
|
+
response_body: SyncStreamingBlob,
|
|
227
|
+
error_registry: TypeRegistry,
|
|
228
|
+
context: TypedProperties,
|
|
229
|
+
) -> CallError:
|
|
230
|
+
error_id = self.error_identifier.identify(
|
|
231
|
+
operation=operation, response=response
|
|
232
|
+
)
|
|
233
|
+
if error_id is not None and error_id not in error_registry:
|
|
234
|
+
error_id = self._resolve_error_id(
|
|
235
|
+
operation=operation,
|
|
236
|
+
error_id=error_id,
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
retry_after = self._retry_after(response)
|
|
240
|
+
|
|
241
|
+
if error_id is None and self._matches_content_type(response):
|
|
242
|
+
if isinstance(response_body, bytearray):
|
|
243
|
+
response_body = bytes(response_body)
|
|
244
|
+
deserializer = self.payload_codec.create_deserializer(source=response_body)
|
|
245
|
+
document = deserializer.read_document(schema=DOCUMENT)
|
|
246
|
+
document_error_id = document.discriminator
|
|
247
|
+
if document_error_id not in error_registry:
|
|
248
|
+
document_error_id = self._resolve_error_id(
|
|
249
|
+
operation=operation,
|
|
250
|
+
error_id=document_error_id,
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
if document_error_id in error_registry:
|
|
254
|
+
error_id = document_error_id
|
|
255
|
+
if isinstance(response_body, SeekableBytesReader):
|
|
256
|
+
response_body.seek(0)
|
|
257
|
+
|
|
258
|
+
if error_id is not None and error_id in error_registry:
|
|
259
|
+
error_shape = error_registry.get(error_id)
|
|
260
|
+
|
|
261
|
+
# make sure the error shape is derived from modeled exception
|
|
262
|
+
if not issubclass(error_shape, ModeledError):
|
|
263
|
+
raise ExpectationNotMetError(
|
|
264
|
+
f"Modeled errors must be derived from 'ModeledError', "
|
|
265
|
+
f"but got {error_shape}"
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
deserializer = HTTPResponseDeserializer(
|
|
269
|
+
payload_codec=self.payload_codec,
|
|
270
|
+
response=response,
|
|
271
|
+
body=response_body,
|
|
272
|
+
)
|
|
273
|
+
modeled_error = error_shape.deserialize(deserializer)
|
|
274
|
+
if retry_after is not None:
|
|
275
|
+
modeled_error.retry_after = retry_after
|
|
276
|
+
return modeled_error
|
|
277
|
+
|
|
278
|
+
message = (
|
|
279
|
+
f"Unknown error for operation {operation.schema.id} "
|
|
280
|
+
f"- status: {response.status}"
|
|
281
|
+
)
|
|
282
|
+
if error_id is not None:
|
|
283
|
+
message += f" - id: {error_id}"
|
|
284
|
+
if response.reason is not None:
|
|
285
|
+
message += f" - reason: {response.status}"
|
|
286
|
+
|
|
287
|
+
is_timeout = response.status == 408
|
|
288
|
+
is_throttle = response.status == 429
|
|
289
|
+
fault = "client" if response.status < 500 else "server"
|
|
290
|
+
|
|
291
|
+
return CallError(
|
|
292
|
+
message=message,
|
|
293
|
+
fault=fault,
|
|
294
|
+
is_throttling_error=is_throttle,
|
|
295
|
+
is_timeout_error=is_timeout,
|
|
296
|
+
is_retry_safe=is_throttle or is_timeout or None,
|
|
297
|
+
retry_after=retry_after,
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
def _retry_after(self, response: HTTPResponse) -> float | None:
|
|
301
|
+
"""The retry delay in seconds requested by the server, if the response carries one."""
|
|
302
|
+
return None
|
|
303
|
+
|
|
304
|
+
def _resolve_error_id(
|
|
305
|
+
self,
|
|
306
|
+
*,
|
|
307
|
+
operation: APIOperation[Any, Any],
|
|
308
|
+
error_id: ShapeID,
|
|
309
|
+
) -> ShapeID:
|
|
310
|
+
"""Resolve a response discriminator to its modeled error shape ID."""
|
|
311
|
+
return error_id
|
|
312
|
+
|
|
313
|
+
def _matches_content_type(self, response: HTTPResponse) -> bool:
|
|
314
|
+
if "content-type" not in response.fields:
|
|
315
|
+
return False
|
|
316
|
+
return response.fields["content-type"].as_string() == self.content_type
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
try:
|
|
320
|
+
from smithy_cbor import CBORCodec
|
|
321
|
+
from smithy_cbor import loads as _cbor_loads
|
|
322
|
+
|
|
323
|
+
_HAS_CBOR = True
|
|
324
|
+
except ImportError:
|
|
325
|
+
_HAS_CBOR = False # type: ignore
|
|
326
|
+
|
|
327
|
+
if TYPE_CHECKING:
|
|
328
|
+
from smithy_cbor import CBORCodec
|
|
329
|
+
from smithy_cbor import loads as _cbor_loads
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _assert_cbor() -> None:
|
|
333
|
+
if not _HAS_CBOR:
|
|
334
|
+
raise MissingDependencyError(
|
|
335
|
+
"Attempted to use CBOR codec, but smithy-cbor is not installed."
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def _is_unit(schema: Schema | None) -> bool:
|
|
340
|
+
"""rpcv2Cbor treats an operation with unit input as having no body, and unit output
|
|
341
|
+
as an empty response. Codegen synthesizes a wrapper struct for a unit member but
|
|
342
|
+
tags it with ``smithy.synthetic#originalShapeId = smithy.api#Unit``.
|
|
343
|
+
"""
|
|
344
|
+
if schema is None:
|
|
345
|
+
return False
|
|
346
|
+
if schema.id == UNIT.id:
|
|
347
|
+
return True
|
|
348
|
+
original = schema.traits.get(ORIGINAL_SHAPE_ID)
|
|
349
|
+
return original is not None and original.document_value == str(UNIT.id)
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
class _RpcV2ClientProtocol(HttpClientProtocol):
|
|
353
|
+
"""Shared framing for the Smithy RPC v2 protocol family (rpcv2Cbor, rpcv2Json).
|
|
354
|
+
|
|
355
|
+
Subclasses supply the codec-specific pieces: the protocol id, content type,
|
|
356
|
+
``Smithy-Protocol`` header value, the empty-document sentinel, codec construction,
|
|
357
|
+
and error ``__type`` decoding. All HTTP/RPC framing lives here.
|
|
358
|
+
"""
|
|
359
|
+
|
|
360
|
+
_id: ShapeID
|
|
361
|
+
_content_type: str
|
|
362
|
+
_smithy_protocol: str
|
|
363
|
+
# Serialized empty document (e.g. CBOR `{}` or JSON `{}`) used when a response body
|
|
364
|
+
# is absent so absent members take their defaults.
|
|
365
|
+
_empty_document: bytes
|
|
366
|
+
_codec: Codec
|
|
367
|
+
_service_name: str
|
|
368
|
+
|
|
369
|
+
@property
|
|
370
|
+
def id(self) -> ShapeID:
|
|
371
|
+
return self._id
|
|
372
|
+
|
|
373
|
+
@property
|
|
374
|
+
def payload_codec(self) -> Codec:
|
|
375
|
+
return self._codec
|
|
376
|
+
|
|
377
|
+
@property
|
|
378
|
+
def content_type(self) -> str:
|
|
379
|
+
return self._content_type
|
|
380
|
+
|
|
381
|
+
def _decode_document(self, body: bytes) -> object:
|
|
382
|
+
"""Decode a serialized document to a Python object, for reading the error
|
|
383
|
+
``__type`` discriminator.
|
|
384
|
+
"""
|
|
385
|
+
raise NotImplementedError
|
|
386
|
+
|
|
387
|
+
def serialize_request[
|
|
388
|
+
OperationInput: SerializeableShape,
|
|
389
|
+
OperationOutput: DeserializeableShape,
|
|
390
|
+
](
|
|
391
|
+
self,
|
|
392
|
+
*,
|
|
393
|
+
operation: APIOperation[OperationInput, OperationOutput],
|
|
394
|
+
input: OperationInput,
|
|
395
|
+
endpoint: URI,
|
|
396
|
+
context: TypedProperties,
|
|
397
|
+
) -> HTTPRequest:
|
|
398
|
+
operation_name = operation.schema.id.name
|
|
399
|
+
path = f"/service/{self._service_name}/operation/{operation_name}"
|
|
400
|
+
|
|
401
|
+
# The protocol omits the body AND the Content-Type header for operations whose
|
|
402
|
+
# input is the unit type (no modeled input). Every other operation serializes
|
|
403
|
+
# the input shape, even when it has no members.
|
|
404
|
+
fields: list[tuple[str, str]] = [
|
|
405
|
+
("Smithy-Protocol", self._smithy_protocol),
|
|
406
|
+
("Accept", self._content_type),
|
|
407
|
+
]
|
|
408
|
+
if _is_unit(operation.input_schema):
|
|
409
|
+
payload = b""
|
|
410
|
+
else:
|
|
411
|
+
payload = self._codec.serialize(shape=input)
|
|
412
|
+
fields.append(("Content-Type", self._content_type))
|
|
413
|
+
fields.append(("Content-Length", str(len(payload))))
|
|
414
|
+
|
|
415
|
+
return _HTTPRequest(
|
|
416
|
+
method="POST",
|
|
417
|
+
destination=_URI(host="", path=path),
|
|
418
|
+
fields=tuples_to_fields(fields),
|
|
419
|
+
body=AsyncBytesReader(payload),
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
async def deserialize_response[
|
|
423
|
+
OperationInput: SerializeableShape,
|
|
424
|
+
OperationOutput: DeserializeableShape,
|
|
425
|
+
](
|
|
426
|
+
self,
|
|
427
|
+
*,
|
|
428
|
+
operation: APIOperation[OperationInput, OperationOutput],
|
|
429
|
+
request: HTTPRequest,
|
|
430
|
+
response: HTTPResponse,
|
|
431
|
+
error_registry: TypeRegistry,
|
|
432
|
+
context: TypedProperties,
|
|
433
|
+
) -> OperationOutput:
|
|
434
|
+
body = await response.consume_body_async()
|
|
435
|
+
if response.status != 200:
|
|
436
|
+
raise self._create_error(
|
|
437
|
+
operation=operation,
|
|
438
|
+
response=response,
|
|
439
|
+
body=body,
|
|
440
|
+
error_registry=error_registry,
|
|
441
|
+
context=context,
|
|
442
|
+
)
|
|
443
|
+
# An empty body (unit output, or a server that elides the empty document) is a
|
|
444
|
+
# valid response: deserialize an empty document so absent members take defaults.
|
|
445
|
+
if not body or _is_unit(operation.output_schema):
|
|
446
|
+
body = self._empty_document
|
|
447
|
+
return self._codec.deserialize(source=body, shape=operation.output)
|
|
448
|
+
|
|
449
|
+
def _create_error(
|
|
450
|
+
self,
|
|
451
|
+
*,
|
|
452
|
+
operation: APIOperation[Any, Any],
|
|
453
|
+
response: HTTPResponse,
|
|
454
|
+
body: bytes,
|
|
455
|
+
error_registry: TypeRegistry,
|
|
456
|
+
context: TypedProperties,
|
|
457
|
+
) -> Exception:
|
|
458
|
+
# The protocol discriminates errors by a top-level `__type` member in the body
|
|
459
|
+
# holding the absolute error shape id (no error header). Resolve it against the
|
|
460
|
+
# registry, then deserialize the same body into the modeled error shape.
|
|
461
|
+
error_id: ShapeID | None = None
|
|
462
|
+
if body:
|
|
463
|
+
decoded = self._decode_document(body)
|
|
464
|
+
if isinstance(decoded, dict):
|
|
465
|
+
raw_type = cast("dict[str, object]", decoded).get("__type")
|
|
466
|
+
if isinstance(raw_type, str):
|
|
467
|
+
error_id = self._normalize_error_id(raw_type, operation)
|
|
468
|
+
|
|
469
|
+
if error_id is not None and error_id in error_registry:
|
|
470
|
+
error_shape = error_registry.get(error_id)
|
|
471
|
+
if not issubclass(error_shape, ModeledError):
|
|
472
|
+
raise ExpectationNotMetError(
|
|
473
|
+
f"Modeled errors must derive from 'ModeledError', got {error_shape}"
|
|
474
|
+
)
|
|
475
|
+
deserializer = self._codec.create_deserializer(body or self._empty_document)
|
|
476
|
+
return error_shape.deserialize(deserializer)
|
|
477
|
+
|
|
478
|
+
message = (
|
|
479
|
+
f"Unknown error for operation {operation.schema.id} "
|
|
480
|
+
f"- status: {response.status}"
|
|
481
|
+
)
|
|
482
|
+
if error_id is not None:
|
|
483
|
+
message += f" - id: {error_id}"
|
|
484
|
+
return CallError(
|
|
485
|
+
message=message,
|
|
486
|
+
fault="client" if response.status < 500 else "server",
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
@staticmethod
|
|
490
|
+
def _normalize_error_id(
|
|
491
|
+
raw_type: str, operation: APIOperation[Any, Any]
|
|
492
|
+
) -> ShapeID:
|
|
493
|
+
# `__type` may carry a full shape id, or (per spec) a name that may include a
|
|
494
|
+
# trailing "#"-qualified suffix. Match on the shape name against the operation's
|
|
495
|
+
# modeled errors so a bare or oddly-qualified name still resolves.
|
|
496
|
+
candidate = ShapeID(raw_type)
|
|
497
|
+
for error_schema in operation.error_schemas:
|
|
498
|
+
if error_schema.id == candidate or error_schema.id.name == candidate.name:
|
|
499
|
+
return error_schema.id
|
|
500
|
+
return candidate
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
class RpcV2CborClientProtocol(_RpcV2ClientProtocol):
|
|
504
|
+
"""An implementation of the smithy.protocols#rpcv2Cbor protocol."""
|
|
505
|
+
|
|
506
|
+
_id: ShapeID = RpcV2CborTrait.id
|
|
507
|
+
_content_type: str = "application/cbor"
|
|
508
|
+
_smithy_protocol: str = "rpc-v2-cbor"
|
|
509
|
+
# CBOR encoding of an empty indefinite-length map (0xBF 0xFF), i.e. `{}`.
|
|
510
|
+
_empty_document: bytes = b"\xbf\xff"
|
|
511
|
+
|
|
512
|
+
def __init__(self, settings: ProtocolSettings) -> None:
|
|
513
|
+
_assert_cbor()
|
|
514
|
+
self._service_name = settings.service_target
|
|
515
|
+
self._codec = CBORCodec(default_namespace=settings.namespace)
|
|
516
|
+
|
|
517
|
+
def _decode_document(self, body: bytes) -> object:
|
|
518
|
+
return _cbor_loads(body) # type: ignore[possibly-unbound]
|
|
@@ -76,13 +76,14 @@ class _BindingMatcher:
|
|
|
76
76
|
self.response_status = response_status
|
|
77
77
|
found_body = False
|
|
78
78
|
found_payload = False
|
|
79
|
-
|
|
79
|
+
members = struct.members_by_index
|
|
80
|
+
self.bindings = [Binding.BODY] * len(members)
|
|
80
81
|
self.payload_member = None
|
|
81
82
|
self.event_stream_member = None
|
|
82
83
|
|
|
83
|
-
for member in
|
|
84
|
+
for member in members:
|
|
84
85
|
binding = self._do_match(member)
|
|
85
|
-
self.bindings.
|
|
86
|
+
self.bindings[member.expect_member_index()] = binding
|
|
86
87
|
found_body = (
|
|
87
88
|
found_body or binding is Binding.BODY or binding is Binding.HOST
|
|
88
89
|
)
|