capo-keyspacesstreams 0.1.0__py3-none-any.whl
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.
- capo_keyspacesstreams/__init__.py +74 -0
- capo_keyspacesstreams/_async.py +25 -0
- capo_keyspacesstreams/_auth/_identity.py +16 -0
- capo_keyspacesstreams/_auth/_providers.py +886 -0
- capo_keyspacesstreams/_auth/_signers.py +88 -0
- capo_keyspacesstreams/_auth/_sigv4.py +434 -0
- capo_keyspacesstreams/_auth/_zapros_handler.py +80 -0
- capo_keyspacesstreams/_iter.py +113 -0
- capo_keyspacesstreams/_operations/keyspaces_streams/get_records.py +171 -0
- capo_keyspacesstreams/_operations/keyspaces_streams/get_shard_iterator.py +170 -0
- capo_keyspacesstreams/_operations/keyspaces_streams/get_stream.py +174 -0
- capo_keyspacesstreams/_operations/keyspaces_streams/list_streams.py +170 -0
- capo_keyspacesstreams/_pagination.py +21 -0
- capo_keyspacesstreams/_protocol/__init__.py +1 -0
- capo_keyspacesstreams/_protocol/errors.py +93 -0
- capo_keyspacesstreams/_protocol/eventstream.py +238 -0
- capo_keyspacesstreams/_protocol/serialize.py +47 -0
- capo_keyspacesstreams/_protocol/xml.py +33 -0
- capo_keyspacesstreams/_rule_engine/__init__.py +0 -0
- capo_keyspacesstreams/_rule_engine/_aws_partition.py +160 -0
- capo_keyspacesstreams/_rule_engine/_endpoint_rule_set.py +65 -0
- capo_keyspacesstreams/_rule_engine/_endpoint_runtime.py +389 -0
- capo_keyspacesstreams/_services/_aws_config.py +156 -0
- capo_keyspacesstreams/_services/_pipeline.py +196 -0
- capo_keyspacesstreams/_services/async_keyspaces_streams.py +442 -0
- capo_keyspacesstreams/_services/keyspaces_streams.py +438 -0
- capo_keyspacesstreams/errors/__init__.py +32 -0
- capo_keyspacesstreams/errors/_base.py +94 -0
- capo_keyspacesstreams/errors/access_denied_exception.py +47 -0
- capo_keyspacesstreams/errors/internal_server_exception.py +47 -0
- capo_keyspacesstreams/errors/resource_not_found_exception.py +47 -0
- capo_keyspacesstreams/errors/throttling_exception.py +47 -0
- capo_keyspacesstreams/errors/validation_exception.py +72 -0
- capo_keyspacesstreams/py.typed +0 -0
- capo_keyspacesstreams/types/_prelude/blob.py +12 -0
- capo_keyspacesstreams/types/_prelude/timestamp.py +17 -0
- capo_keyspacesstreams/types/date.py +20 -0
- capo_keyspacesstreams/types/get_records_input.py +37 -0
- capo_keyspacesstreams/types/get_records_output.py +70 -0
- capo_keyspacesstreams/types/get_shard_iterator_input.py +70 -0
- capo_keyspacesstreams/types/get_shard_iterator_output.py +30 -0
- capo_keyspacesstreams/types/get_stream_input.py +63 -0
- capo_keyspacesstreams/types/get_stream_output.py +144 -0
- capo_keyspacesstreams/types/iterator_description.py +42 -0
- capo_keyspacesstreams/types/iterator_position.py +17 -0
- capo_keyspacesstreams/types/keyspace_name.py +5 -0
- capo_keyspacesstreams/types/keyspaces_cell.py +63 -0
- capo_keyspacesstreams/types/keyspaces_cell_list.py +35 -0
- capo_keyspacesstreams/types/keyspaces_cell_map.py +39 -0
- capo_keyspacesstreams/types/keyspaces_cell_map_definition.py +83 -0
- capo_keyspacesstreams/types/keyspaces_cell_value.py +333 -0
- capo_keyspacesstreams/types/keyspaces_cells.py +35 -0
- capo_keyspacesstreams/types/keyspaces_keys_map.py +39 -0
- capo_keyspacesstreams/types/keyspaces_metadata.py +29 -0
- capo_keyspacesstreams/types/keyspaces_row.py +83 -0
- capo_keyspacesstreams/types/keyspaces_udt_map.py +35 -0
- capo_keyspacesstreams/types/list_streams_input.py +50 -0
- capo_keyspacesstreams/types/list_streams_output.py +47 -0
- capo_keyspacesstreams/types/origin_type.py +18 -0
- capo_keyspacesstreams/types/record.py +146 -0
- capo_keyspacesstreams/types/record_list.py +29 -0
- capo_keyspacesstreams/types/sequence_number.py +5 -0
- capo_keyspacesstreams/types/sequence_number_range.py +38 -0
- capo_keyspacesstreams/types/shard.py +70 -0
- capo_keyspacesstreams/types/shard_description_list.py +29 -0
- capo_keyspacesstreams/types/shard_filter.py +47 -0
- capo_keyspacesstreams/types/shard_filter_type.py +14 -0
- capo_keyspacesstreams/types/shard_id.py +5 -0
- capo_keyspacesstreams/types/shard_id_list.py +17 -0
- capo_keyspacesstreams/types/shard_id_token.py +5 -0
- capo_keyspacesstreams/types/shard_iterator.py +5 -0
- capo_keyspacesstreams/types/shard_iterator_type.py +19 -0
- capo_keyspacesstreams/types/stream.py +54 -0
- capo_keyspacesstreams/types/stream_arn.py +5 -0
- capo_keyspacesstreams/types/stream_arn_token.py +5 -0
- capo_keyspacesstreams/types/stream_list.py +29 -0
- capo_keyspacesstreams/types/stream_status.py +19 -0
- capo_keyspacesstreams/types/stream_view_type.py +19 -0
- capo_keyspacesstreams/types/table_name.py +5 -0
- capo_keyspacesstreams/types/validation_exception_type.py +19 -0
- capo_keyspacesstreams-0.1.0.dist-info/METADATA +101 -0
- capo_keyspacesstreams-0.1.0.dist-info/RECORD +85 -0
- capo_keyspacesstreams-0.1.0.dist-info/WHEEL +5 -0
- capo_keyspacesstreams-0.1.0.dist-info/licenses/LICENSE +21 -0
- capo_keyspacesstreams-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"""Generated from Smithy shape ``com.amazonaws.keyspacesstreams#ListStreams``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import zapros
|
|
9
|
+
from typing_extensions import Never
|
|
10
|
+
|
|
11
|
+
import capo_keyspacesstreams._auth._signers
|
|
12
|
+
import capo_keyspacesstreams._auth._sigv4
|
|
13
|
+
import capo_keyspacesstreams._protocol.eventstream
|
|
14
|
+
import capo_keyspacesstreams.errors.access_denied_exception
|
|
15
|
+
import capo_keyspacesstreams.errors.internal_server_exception
|
|
16
|
+
import capo_keyspacesstreams.errors.resource_not_found_exception
|
|
17
|
+
import capo_keyspacesstreams.errors.throttling_exception
|
|
18
|
+
import capo_keyspacesstreams.errors.validation_exception
|
|
19
|
+
import capo_keyspacesstreams.types.list_streams_input
|
|
20
|
+
import capo_keyspacesstreams.types.list_streams_output
|
|
21
|
+
import capo_keyspacesstreams.types.stream_list
|
|
22
|
+
from capo_keyspacesstreams._protocol.errors import parse_error_metadata_json
|
|
23
|
+
from capo_keyspacesstreams._rule_engine._endpoint_rule_set import (
|
|
24
|
+
EndpointParams,
|
|
25
|
+
resolve,
|
|
26
|
+
)
|
|
27
|
+
from capo_keyspacesstreams._services._pipeline import (
|
|
28
|
+
AsyncOperationOptions,
|
|
29
|
+
OperationOptions,
|
|
30
|
+
)
|
|
31
|
+
from capo_keyspacesstreams.errors import UnknownServiceError
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def handle_error(response: zapros.Response) -> Never:
|
|
35
|
+
data = json.loads(response.read())
|
|
36
|
+
code, message = parse_error_metadata_json(response, data)
|
|
37
|
+
match code:
|
|
38
|
+
case "AccessDeniedException":
|
|
39
|
+
raise capo_keyspacesstreams.errors.access_denied_exception.AccessDeniedException.from_aws_json_1_0(
|
|
40
|
+
data, message
|
|
41
|
+
)
|
|
42
|
+
case "InternalServerException":
|
|
43
|
+
raise capo_keyspacesstreams.errors.internal_server_exception.InternalServerException.from_aws_json_1_0(
|
|
44
|
+
data, message
|
|
45
|
+
)
|
|
46
|
+
case "ResourceNotFoundException":
|
|
47
|
+
raise capo_keyspacesstreams.errors.resource_not_found_exception.ResourceNotFoundException.from_aws_json_1_0(
|
|
48
|
+
data, message
|
|
49
|
+
)
|
|
50
|
+
case "ThrottlingException":
|
|
51
|
+
raise capo_keyspacesstreams.errors.throttling_exception.ThrottlingException.from_aws_json_1_0(
|
|
52
|
+
data, message
|
|
53
|
+
)
|
|
54
|
+
case "ValidationException":
|
|
55
|
+
raise capo_keyspacesstreams.errors.validation_exception.ValidationException.from_aws_json_1_0(
|
|
56
|
+
data, message
|
|
57
|
+
)
|
|
58
|
+
case _:
|
|
59
|
+
raise UnknownServiceError(code=code, message=message, response=response)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def handle_response(
|
|
63
|
+
response: zapros.Response,
|
|
64
|
+
) -> capo_keyspacesstreams.types.list_streams_output.ListStreamsOutput:
|
|
65
|
+
out: capo_keyspacesstreams.types.list_streams_output.ListStreamsOutput = (
|
|
66
|
+
capo_keyspacesstreams.types.list_streams_output.deserialize_aws_json_1_0(
|
|
67
|
+
json.loads(response.read())
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
return out
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
async def async_handle_response(
|
|
74
|
+
response: zapros.Response,
|
|
75
|
+
) -> capo_keyspacesstreams.types.list_streams_output.ListStreamsOutput:
|
|
76
|
+
out: capo_keyspacesstreams.types.list_streams_output.ListStreamsOutput = (
|
|
77
|
+
capo_keyspacesstreams.types.list_streams_output.deserialize_aws_json_1_0(
|
|
78
|
+
json.loads(await response.aread())
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
return out
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def get_signer(
|
|
85
|
+
options: AsyncOperationOptions | OperationOptions,
|
|
86
|
+
auth_schemes: list[dict[str, Any]] | None = None,
|
|
87
|
+
) -> capo_keyspacesstreams._auth._signers.Signer | None:
|
|
88
|
+
name_to_schema = {s["name"]: s for s in (auth_schemes or [])} # noqa: F841
|
|
89
|
+
if (
|
|
90
|
+
options.credentials_provider is not None
|
|
91
|
+
and name_to_schema
|
|
92
|
+
and not name_to_schema.keys() & {"sigv4", "sigv4-s3express"}
|
|
93
|
+
):
|
|
94
|
+
raise RuntimeError(
|
|
95
|
+
"Endpoint requires an unsupported auth scheme: " + ", ".join(name_to_schema)
|
|
96
|
+
)
|
|
97
|
+
if options.credentials_provider is not None:
|
|
98
|
+
endpoint_scheme = name_to_schema.get("sigv4") or name_to_schema.get(
|
|
99
|
+
"sigv4-s3express"
|
|
100
|
+
)
|
|
101
|
+
if endpoint_scheme is not None or not name_to_schema:
|
|
102
|
+
sigv4_config = capo_keyspacesstreams._auth._sigv4.build_sigv4_auth_scheme(
|
|
103
|
+
"cassandra", options.region, endpoint_scheme
|
|
104
|
+
)
|
|
105
|
+
if sigv4_config is not None:
|
|
106
|
+
return capo_keyspacesstreams._auth._signers.SigV4Signer(
|
|
107
|
+
options.credentials_provider, auth_scheme=sigv4_config
|
|
108
|
+
)
|
|
109
|
+
raise RuntimeError("Auth was not resolved")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def build_request(
|
|
113
|
+
options: OperationOptions | AsyncOperationOptions,
|
|
114
|
+
input_: capo_keyspacesstreams.types.list_streams_input.ListStreamsInput,
|
|
115
|
+
) -> zapros.Request:
|
|
116
|
+
endpoint = resolve(
|
|
117
|
+
EndpointParams(
|
|
118
|
+
UseFIPS=options.use_fips, Endpoint=options.endpoint, Region=options.region
|
|
119
|
+
)
|
|
120
|
+
) # noqa: F841
|
|
121
|
+
url = endpoint.url.rstrip("/") + ""
|
|
122
|
+
params: list[tuple[str, str]] = []
|
|
123
|
+
headers: dict[str, str] = {k: ", ".join(v) for k, v in endpoint.headers.items()}
|
|
124
|
+
headers["X-Amz-Target"] = "KeyspacesStreams.ListStreams"
|
|
125
|
+
body: bytes | None = json.dumps(
|
|
126
|
+
capo_keyspacesstreams.types.list_streams_input.serialize_aws_json_1_0(input_),
|
|
127
|
+
allow_nan=False,
|
|
128
|
+
).encode()
|
|
129
|
+
headers["content-type"] = "application/x-amz-json-1.0"
|
|
130
|
+
signer = get_signer(options, auth_schemes=endpoint.properties.get("authSchemes"))
|
|
131
|
+
normalized_url = zapros.URL(url)
|
|
132
|
+
for k, v in params:
|
|
133
|
+
normalized_url.search_params.append(k, v)
|
|
134
|
+
return zapros.Request(
|
|
135
|
+
normalized_url, "POST", headers=headers, body=body, context={"signer": signer}
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def list_streams(
|
|
140
|
+
options: OperationOptions,
|
|
141
|
+
input_: capo_keyspacesstreams.types.list_streams_input.ListStreamsInput,
|
|
142
|
+
) -> tuple[
|
|
143
|
+
capo_keyspacesstreams.types.list_streams_output.ListStreamsOutput, zapros.Response
|
|
144
|
+
]:
|
|
145
|
+
response = options.client.handler.handle(build_request(options, input_))
|
|
146
|
+
try:
|
|
147
|
+
if response.status >= 300:
|
|
148
|
+
response.read()
|
|
149
|
+
handle_error(response)
|
|
150
|
+
return handle_response(response), response
|
|
151
|
+
except BaseException:
|
|
152
|
+
response.close()
|
|
153
|
+
raise
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
async def async_list_streams(
|
|
157
|
+
options: AsyncOperationOptions,
|
|
158
|
+
input_: capo_keyspacesstreams.types.list_streams_input.ListStreamsInput,
|
|
159
|
+
) -> tuple[
|
|
160
|
+
capo_keyspacesstreams.types.list_streams_output.ListStreamsOutput, zapros.Response
|
|
161
|
+
]:
|
|
162
|
+
response = await options.client.handler.ahandle(build_request(options, input_))
|
|
163
|
+
try:
|
|
164
|
+
if response.status >= 300:
|
|
165
|
+
await response.aread()
|
|
166
|
+
handle_error(response)
|
|
167
|
+
return await async_handle_response(response), response
|
|
168
|
+
except BaseException:
|
|
169
|
+
await response.aclose()
|
|
170
|
+
raise
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Runtime helpers for generated paginated ``iter_*`` operation methods."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def resolve_path(obj: Any, path: tuple[str, ...]) -> Any:
|
|
9
|
+
"""Walk ``path`` segments on a nested ``dict`` (TypedDict at runtime).
|
|
10
|
+
|
|
11
|
+
Returns ``None`` as soon as any intermediate value is missing or non-dict —
|
|
12
|
+
the generated iter loop uses a falsy token as the terminate signal, so this
|
|
13
|
+
short-circuit gives the same semantics for dotted paths as for top-level ones.
|
|
14
|
+
"""
|
|
15
|
+
for key in path:
|
|
16
|
+
if not isinstance(obj, dict):
|
|
17
|
+
return None
|
|
18
|
+
obj = obj.get(key)
|
|
19
|
+
if obj is None:
|
|
20
|
+
return None
|
|
21
|
+
return obj
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Protocol runtime modules. Hand-written; not generated."""
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Shared service-error runtime.
|
|
2
|
+
|
|
3
|
+
Hand-written, not regenerated. Helpers for extracting error metadata
|
|
4
|
+
from HTTP error responses.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from zapros import Response
|
|
12
|
+
|
|
13
|
+
from .xml import fromstring
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from xml.etree.ElementTree import Element
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def find_error_element(root: Element) -> Element:
|
|
20
|
+
"""Return the element holding the error structure's members.
|
|
21
|
+
|
|
22
|
+
Handles the three XML error envelopes: a bare ``<Error>`` root
|
|
23
|
+
(restXml with ``noErrorWrapping``), an ``<ErrorResponse><Error>``
|
|
24
|
+
wrapper (restXml/awsQuery), and the ec2Query
|
|
25
|
+
``<Response><Errors><Error>`` envelope. Falls back to ``root``
|
|
26
|
+
when no ``<Error>`` element is found.
|
|
27
|
+
"""
|
|
28
|
+
if root.tag.endswith("Error"):
|
|
29
|
+
return root
|
|
30
|
+
for path in ("Error", "Errors/Error"):
|
|
31
|
+
err = root.find(path)
|
|
32
|
+
if err is not None:
|
|
33
|
+
return err
|
|
34
|
+
return root
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def parse_error_metadata(root: Element) -> tuple[str | None, str | None]:
|
|
38
|
+
"""Return ``(code, message)`` from an XML error envelope.
|
|
39
|
+
|
|
40
|
+
Accepts any envelope understood by :func:`find_error_element`.
|
|
41
|
+
Missing children yield ``None``.
|
|
42
|
+
"""
|
|
43
|
+
err = find_error_element(root)
|
|
44
|
+
code_el = err.find("Code")
|
|
45
|
+
msg_el = err.find("Message")
|
|
46
|
+
code = code_el.text if code_el is not None else None
|
|
47
|
+
message = msg_el.text if msg_el is not None else None
|
|
48
|
+
return code, message
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def parse_error_metadata_json(
|
|
52
|
+
response: Response, data: dict
|
|
53
|
+
) -> tuple[str | None, str | None]:
|
|
54
|
+
"""Return ``(code, message)`` from a restJson1 error response.
|
|
55
|
+
|
|
56
|
+
Code precedence: the ``X-Amzn-Errortype`` response header, then the
|
|
57
|
+
``__type`` body field, then ``code``. The raw value is normalized by
|
|
58
|
+
dropping a trailing ``:uri`` suffix first, then a ``prefix#``
|
|
59
|
+
namespace — in that order, so a ``#`` inside the uri suffix cannot
|
|
60
|
+
hijack the code. Message comes from ``message`` or ``Message``.
|
|
61
|
+
Missing values yield ``None``.
|
|
62
|
+
"""
|
|
63
|
+
code = (
|
|
64
|
+
response.headers.get("X-Amzn-Errortype")
|
|
65
|
+
or data.get("__type")
|
|
66
|
+
or data.get("code")
|
|
67
|
+
)
|
|
68
|
+
if code is not None:
|
|
69
|
+
code = code.split(":", 1)[0].rsplit("#", 1)[-1]
|
|
70
|
+
message = data.get("message") or data.get("Message")
|
|
71
|
+
return code, message
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def is_xml_error_body(body: bytes) -> bool:
|
|
75
|
+
"""Whether a 2xx response body is really an XML error document.
|
|
76
|
+
|
|
77
|
+
S3 answers CopyObject, UploadPartCopy and CompleteMultipartUpload
|
|
78
|
+
with ``200 OK`` before the operation finishes; a failure after that
|
|
79
|
+
point is reported as an ``<Error>`` body (or an empty body) on the
|
|
80
|
+
200 response. Official SDKs check the body for exactly these
|
|
81
|
+
operations.
|
|
82
|
+
"""
|
|
83
|
+
if not body:
|
|
84
|
+
return True
|
|
85
|
+
return fromstring(body).tag == "Error"
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
__all__ = [
|
|
89
|
+
"find_error_element",
|
|
90
|
+
"is_xml_error_body",
|
|
91
|
+
"parse_error_metadata",
|
|
92
|
+
"parse_error_metadata_json",
|
|
93
|
+
]
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"""Shared Amazon eventstream binary-format runtime.
|
|
2
|
+
|
|
3
|
+
Hand-written, not regenerated. Provides framing, header encoding, and
|
|
4
|
+
checksum validation for Smithy event-stream messages.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import datetime
|
|
10
|
+
import uuid
|
|
11
|
+
import zlib
|
|
12
|
+
from collections.abc import AsyncIterator, Callable, Iterator
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from typing import TypeVar, Union
|
|
15
|
+
|
|
16
|
+
HeaderValue = Union[bool, int, bytes, str, datetime.datetime, uuid.UUID]
|
|
17
|
+
T = TypeVar("T")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# Header type constants from the Amazon eventstream specification.
|
|
21
|
+
_TRUE_HEADER = 0x00
|
|
22
|
+
_FALSE_HEADER = 0x01
|
|
23
|
+
_BYTE_HEADER = 0x02
|
|
24
|
+
_SHORT_HEADER = 0x03
|
|
25
|
+
_INTEGER_HEADER = 0x04
|
|
26
|
+
_LONG_HEADER = 0x05
|
|
27
|
+
_BYTE_ARRAY_HEADER = 0x06
|
|
28
|
+
_STRING_HEADER = 0x07
|
|
29
|
+
_TIMESTAMP_HEADER = 0x08
|
|
30
|
+
_UUID_HEADER = 0x09
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _encode_header_value(value: HeaderValue) -> bytes:
|
|
34
|
+
if isinstance(value, bool):
|
|
35
|
+
return bytes([_TRUE_HEADER if value else _FALSE_HEADER])
|
|
36
|
+
if isinstance(value, int):
|
|
37
|
+
if -128 <= value <= 127:
|
|
38
|
+
return bytes([_BYTE_HEADER]) + value.to_bytes(1, "big", signed=True)
|
|
39
|
+
if -32768 <= value <= 32767:
|
|
40
|
+
return bytes([_SHORT_HEADER]) + value.to_bytes(2, "big", signed=True)
|
|
41
|
+
if -2147483648 <= value <= 2147483647:
|
|
42
|
+
return bytes([_INTEGER_HEADER]) + value.to_bytes(4, "big", signed=True)
|
|
43
|
+
# Fall through to signed 64-bit.
|
|
44
|
+
return bytes([_LONG_HEADER]) + value.to_bytes(8, "big", signed=True)
|
|
45
|
+
if isinstance(value, bytes):
|
|
46
|
+
return bytes([_BYTE_ARRAY_HEADER]) + len(value).to_bytes(2, "big") + value
|
|
47
|
+
if isinstance(value, str):
|
|
48
|
+
encoded = value.encode("utf-8")
|
|
49
|
+
return bytes([_STRING_HEADER]) + len(encoded).to_bytes(2, "big") + encoded
|
|
50
|
+
if isinstance(value, datetime.datetime):
|
|
51
|
+
millis = int(value.timestamp() * 1000)
|
|
52
|
+
return bytes([_TIMESTAMP_HEADER]) + millis.to_bytes(8, "big", signed=True)
|
|
53
|
+
if isinstance(value, uuid.UUID):
|
|
54
|
+
return bytes([_UUID_HEADER]) + value.bytes
|
|
55
|
+
raise TypeError(f"unsupported eventstream header value: {value!r}")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _decode_header_value(
|
|
59
|
+
type_byte: int, data: bytes | bytearray, pos: int
|
|
60
|
+
) -> tuple[HeaderValue, int]:
|
|
61
|
+
if type_byte == _TRUE_HEADER:
|
|
62
|
+
return True, pos
|
|
63
|
+
if type_byte == _FALSE_HEADER:
|
|
64
|
+
return False, pos
|
|
65
|
+
if type_byte == _BYTE_HEADER:
|
|
66
|
+
return int.from_bytes(data[pos : pos + 1], "big", signed=True), pos + 1
|
|
67
|
+
if type_byte == _SHORT_HEADER:
|
|
68
|
+
return int.from_bytes(data[pos : pos + 2], "big", signed=True), pos + 2
|
|
69
|
+
if type_byte == _INTEGER_HEADER:
|
|
70
|
+
return int.from_bytes(data[pos : pos + 4], "big", signed=True), pos + 4
|
|
71
|
+
if type_byte == _LONG_HEADER:
|
|
72
|
+
return int.from_bytes(data[pos : pos + 8], "big", signed=True), pos + 8
|
|
73
|
+
if type_byte == _BYTE_ARRAY_HEADER:
|
|
74
|
+
length = int.from_bytes(data[pos : pos + 2], "big")
|
|
75
|
+
pos += 2
|
|
76
|
+
return bytes(data[pos : pos + length]), pos + length
|
|
77
|
+
if type_byte == _STRING_HEADER:
|
|
78
|
+
length = int.from_bytes(data[pos : pos + 2], "big")
|
|
79
|
+
pos += 2
|
|
80
|
+
return data[pos : pos + length].decode("utf-8"), pos + length
|
|
81
|
+
if type_byte == _TIMESTAMP_HEADER:
|
|
82
|
+
millis = int.from_bytes(data[pos : pos + 8], "big", signed=True)
|
|
83
|
+
return (
|
|
84
|
+
datetime.datetime.fromtimestamp(millis / 1000, tz=datetime.timezone.utc),
|
|
85
|
+
pos + 8,
|
|
86
|
+
)
|
|
87
|
+
if type_byte == _UUID_HEADER:
|
|
88
|
+
return uuid.UUID(bytes=bytes(data[pos : pos + 16])), pos + 16
|
|
89
|
+
raise ValueError(f"unsupported eventstream header type: {type_byte:#x}")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _encode_headers(headers: dict[str, HeaderValue]) -> bytes:
|
|
93
|
+
parts: list[bytes] = []
|
|
94
|
+
for name, value in headers.items():
|
|
95
|
+
name_bytes = name.encode("utf-8")
|
|
96
|
+
if len(name_bytes) > 255:
|
|
97
|
+
raise ValueError(f"eventstream header name too long: {name!r}")
|
|
98
|
+
parts.append(bytes([len(name_bytes)]) + name_bytes)
|
|
99
|
+
parts.append(_encode_header_value(value))
|
|
100
|
+
return b"".join(parts)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _decode_headers(data: bytes | bytearray) -> dict[str, HeaderValue]:
|
|
104
|
+
pos = 0
|
|
105
|
+
headers: dict[str, HeaderValue] = {}
|
|
106
|
+
while pos < len(data):
|
|
107
|
+
name_len = data[pos]
|
|
108
|
+
pos += 1
|
|
109
|
+
name = data[pos : pos + name_len].decode("utf-8")
|
|
110
|
+
pos += name_len
|
|
111
|
+
type_byte = data[pos]
|
|
112
|
+
pos += 1
|
|
113
|
+
value, pos = _decode_header_value(type_byte, data, pos)
|
|
114
|
+
headers[name] = value
|
|
115
|
+
return headers
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass
|
|
119
|
+
class Message:
|
|
120
|
+
"""A single Amazon eventstream message."""
|
|
121
|
+
|
|
122
|
+
headers: dict[str, HeaderValue] = field(default_factory=dict)
|
|
123
|
+
payload: bytes = b""
|
|
124
|
+
|
|
125
|
+
def encode(self) -> bytes:
|
|
126
|
+
"""Serialize this message into eventstream framing bytes."""
|
|
127
|
+
headers_bytes = _encode_headers(self.headers)
|
|
128
|
+
headers_length = len(headers_bytes)
|
|
129
|
+
payload = self.payload
|
|
130
|
+
total_length = 12 + headers_length + len(payload)
|
|
131
|
+
|
|
132
|
+
prelude = total_length.to_bytes(4, "big") + headers_length.to_bytes(4, "big")
|
|
133
|
+
prelude_crc = zlib.crc32(prelude).to_bytes(4, "big")
|
|
134
|
+
message_body = prelude + prelude_crc + headers_bytes + payload
|
|
135
|
+
message_crc = zlib.crc32(message_body).to_bytes(4, "big")
|
|
136
|
+
return message_body + message_crc
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@dataclass
|
|
140
|
+
class MessageDecoder:
|
|
141
|
+
"""Stateful decoder that turns byte chunks into :class:`Message` objects.
|
|
142
|
+
|
|
143
|
+
``feed`` may emit zero, one, or many messages for each chunk. Partial
|
|
144
|
+
frames are buffered until the next chunk completes them.
|
|
145
|
+
"""
|
|
146
|
+
|
|
147
|
+
_buffer: bytearray = field(default_factory=bytearray, init=False)
|
|
148
|
+
|
|
149
|
+
def feed(self, data: bytes) -> Iterator[Message]:
|
|
150
|
+
"""Append ``data`` to the internal buffer and yield any full messages."""
|
|
151
|
+
self._buffer.extend(data)
|
|
152
|
+
while True:
|
|
153
|
+
if len(self._buffer) < 12:
|
|
154
|
+
return
|
|
155
|
+
|
|
156
|
+
total_length = int.from_bytes(self._buffer[:4], "big")
|
|
157
|
+
headers_length = int.from_bytes(self._buffer[4:8], "big")
|
|
158
|
+
prelude = bytes(self._buffer[:8])
|
|
159
|
+
prelude_crc = int.from_bytes(self._buffer[8:12], "big")
|
|
160
|
+
|
|
161
|
+
if total_length < 12 + headers_length:
|
|
162
|
+
raise ValueError(
|
|
163
|
+
f"invalid eventstream message: total_length={total_length} "
|
|
164
|
+
f"is smaller than minimum {12 + headers_length}"
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
message_end = total_length + 4
|
|
168
|
+
if len(self._buffer) < message_end:
|
|
169
|
+
return
|
|
170
|
+
|
|
171
|
+
computed_prelude_crc = zlib.crc32(prelude) & 0xFFFFFFFF
|
|
172
|
+
if computed_prelude_crc != prelude_crc:
|
|
173
|
+
raise ValueError("eventstream prelude checksum mismatch")
|
|
174
|
+
|
|
175
|
+
message_bytes = bytes(self._buffer[:total_length])
|
|
176
|
+
message_crc = int.from_bytes(
|
|
177
|
+
self._buffer[total_length : total_length + 4], "big"
|
|
178
|
+
)
|
|
179
|
+
computed_message_crc = zlib.crc32(message_bytes) & 0xFFFFFFFF
|
|
180
|
+
if computed_message_crc != message_crc:
|
|
181
|
+
raise ValueError("eventstream message checksum mismatch")
|
|
182
|
+
|
|
183
|
+
headers = _decode_headers(self._buffer[12 : 12 + headers_length])
|
|
184
|
+
payload = bytes(self._buffer[12 + headers_length : total_length])
|
|
185
|
+
del self._buffer[:message_end]
|
|
186
|
+
yield Message(headers=headers, payload=payload)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def raw_stream_to_events(
|
|
190
|
+
raw_stream: Iterator[bytes], decoder: MessageDecoder, deser: Callable[[Message], T]
|
|
191
|
+
) -> Iterator[T]:
|
|
192
|
+
"""Map a raw byte iterator into deserialized event objects."""
|
|
193
|
+
for chunk in raw_stream:
|
|
194
|
+
for message in decoder.feed(chunk):
|
|
195
|
+
yield deser(message)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
async def async_raw_stream_to_events(
|
|
199
|
+
raw_stream: AsyncIterator[bytes],
|
|
200
|
+
decoder: MessageDecoder,
|
|
201
|
+
deser: Callable[[Message], T],
|
|
202
|
+
) -> AsyncIterator[T]:
|
|
203
|
+
"""Map an async raw byte iterator into deserialized event objects."""
|
|
204
|
+
async for chunk in raw_stream:
|
|
205
|
+
for message in decoder.feed(chunk):
|
|
206
|
+
yield deser(message)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def read_messages(
|
|
210
|
+
raw_stream: Iterator[bytes], decoder: MessageDecoder
|
|
211
|
+
) -> Iterator[Message]:
|
|
212
|
+
"""Yield :class:`Message` objects framed out of a raw byte iterator.
|
|
213
|
+
|
|
214
|
+
Used by RPC event streams (e.g. awsJson) where the first message may be an
|
|
215
|
+
initial-response that must be pulled off before consuming the events.
|
|
216
|
+
"""
|
|
217
|
+
for chunk in raw_stream:
|
|
218
|
+
yield from decoder.feed(chunk)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
async def async_read_messages(
|
|
222
|
+
raw_stream: AsyncIterator[bytes], decoder: MessageDecoder
|
|
223
|
+
) -> AsyncIterator[Message]:
|
|
224
|
+
"""Async variant of :func:`read_messages`."""
|
|
225
|
+
async for chunk in raw_stream:
|
|
226
|
+
for message in decoder.feed(chunk):
|
|
227
|
+
yield message
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
__all__ = [
|
|
231
|
+
"HeaderValue",
|
|
232
|
+
"Message",
|
|
233
|
+
"MessageDecoder",
|
|
234
|
+
"async_raw_stream_to_events",
|
|
235
|
+
"async_read_messages",
|
|
236
|
+
"raw_stream_to_events",
|
|
237
|
+
"read_messages",
|
|
238
|
+
]
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Shared request-binding serialization runtime.
|
|
2
|
+
|
|
3
|
+
Hand-written, not regenerated. Text encodings for values bound to HTTP
|
|
4
|
+
labels, query parameters, and headers by the generated operations.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import base64
|
|
10
|
+
import datetime
|
|
11
|
+
from email.utils import format_datetime
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def as_utc(value: datetime.datetime) -> datetime.datetime:
|
|
15
|
+
"""Normalize to an aware UTC datetime; naive values are taken as UTC."""
|
|
16
|
+
if value.tzinfo is None:
|
|
17
|
+
return value.replace(tzinfo=datetime.timezone.utc)
|
|
18
|
+
return value.astimezone(datetime.timezone.utc)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def fmt_date_time(value: datetime.datetime) -> str:
|
|
22
|
+
"""RFC 3339 with a ``Z`` designator, e.g. ``2015-01-25T08:00:00Z``."""
|
|
23
|
+
return as_utc(value).isoformat().replace("+00:00", "Z")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def fmt_http_date(value: datetime.datetime) -> str:
|
|
27
|
+
"""IMF-fixdate, e.g. ``Sun, 25 Jan 2015 08:00:00 GMT``."""
|
|
28
|
+
return format_datetime(as_utc(value), usegmt=True)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def fmt_epoch_seconds(value: datetime.datetime) -> str:
|
|
32
|
+
"""Seconds since the epoch; whole seconds carry no ``.0`` suffix."""
|
|
33
|
+
ts = as_utc(value).timestamp()
|
|
34
|
+
return str(int(ts)) if ts == int(ts) else str(ts)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def b64(value: bytes) -> str:
|
|
38
|
+
return base64.b64encode(value).decode("ascii")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
__all__ = [
|
|
42
|
+
"as_utc",
|
|
43
|
+
"b64",
|
|
44
|
+
"fmt_date_time",
|
|
45
|
+
"fmt_epoch_seconds",
|
|
46
|
+
"fmt_http_date",
|
|
47
|
+
]
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Shared XML protocol runtime.
|
|
2
|
+
|
|
3
|
+
Hand-written, not regenerated. Holds re-exports of
|
|
4
|
+
``xml.etree.ElementTree`` primitives generated code depends on.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from xml.etree.ElementTree import Element, SubElement, tostring
|
|
8
|
+
from xml.etree.ElementTree import fromstring as _fromstring
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def fromstring(text: str | bytes) -> Element:
|
|
12
|
+
"""Parse XML and strip namespaces so generated ``el.find("Tag")`` and
|
|
13
|
+
``el.get("attr")`` lookups match. AWS restXml responses use a default
|
|
14
|
+
namespace which ElementTree otherwise qualifies onto every tag as
|
|
15
|
+
``{ns}Tag``; prefixed attributes (e.g. ``xsi:type``) get the same
|
|
16
|
+
treatment on their keys."""
|
|
17
|
+
root = _fromstring(text)
|
|
18
|
+
for el in root.iter():
|
|
19
|
+
if el.tag.startswith("{"):
|
|
20
|
+
el.tag = el.tag.rpartition("}")[2]
|
|
21
|
+
if any(key.startswith("{") for key in el.attrib):
|
|
22
|
+
el.attrib = {
|
|
23
|
+
key.rpartition("}")[2]: value for key, value in el.attrib.items()
|
|
24
|
+
}
|
|
25
|
+
return root
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"Element",
|
|
30
|
+
"SubElement",
|
|
31
|
+
"fromstring",
|
|
32
|
+
"tostring",
|
|
33
|
+
]
|
|
File without changes
|