smithy-http 0.0.1__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.
@@ -0,0 +1,97 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ # pyright: reportMissingTypeStubs=false,reportUnknownMemberType=false
4
+
5
+ from dataclasses import dataclass, field
6
+ from string import ascii_letters, digits
7
+
8
+ _USERAGENT_ALLOWED_CHARACTERS = ascii_letters + digits + "!$%&'*+-.^_`|~"
9
+
10
+
11
+ @dataclass(frozen=True, slots=True)
12
+ class UserAgentComponent:
13
+ """Component of a User-Agent header string in the standard format.
14
+
15
+ Each component consists of a prefix, a name, and a value. In the string
16
+ representation these are combined in the format ``prefix/name#value``.
17
+
18
+ This class is considered private and is subject to abrupt breaking changes.
19
+ """
20
+
21
+ prefix: str
22
+ name: str
23
+ value: str | None = None
24
+
25
+ def __str__(self):
26
+ """Create string like 'prefix/name#value' from a UserAgentComponent."""
27
+ clean_prefix = sanitize_user_agent_string_component(
28
+ self.prefix, allow_hash=True
29
+ )
30
+ clean_name = sanitize_user_agent_string_component(self.name, allow_hash=False)
31
+ if self.value is None or self.value == "":
32
+ return f"{clean_prefix}/{clean_name}"
33
+ clean_value = sanitize_user_agent_string_component(self.value, allow_hash=True)
34
+ return f"{clean_prefix}/{clean_name}#{clean_value}"
35
+
36
+
37
+ @dataclass(frozen=True, slots=True)
38
+ class RawStringUserAgentComponent:
39
+ """UserAgentComponent interface wrapper around ``str``.
40
+
41
+ Use for User-Agent header components that are not constructed from prefix+name+value
42
+ but instead are provided as strings. No sanitization is performed.
43
+ """
44
+
45
+ value: str
46
+
47
+ def __str__(self) -> str:
48
+ return self.value
49
+
50
+
51
+ @dataclass(kw_only=True, slots=True)
52
+ class UserAgent:
53
+ sdk_metadata: list[UserAgentComponent] = field(default_factory=list)
54
+ internal_metadata: list[UserAgentComponent] = field(default_factory=list)
55
+ ua_metadata: list[UserAgentComponent] = field(default_factory=list)
56
+ api_metadata: list[UserAgentComponent] = field(default_factory=list)
57
+ os_metadata: list[UserAgentComponent] = field(default_factory=list)
58
+ language_metadata: list[UserAgentComponent] = field(default_factory=list)
59
+ env_metadata: list[UserAgentComponent] = field(default_factory=list)
60
+ config_metadata: list[UserAgentComponent] = field(default_factory=list)
61
+ feat_metadata: list[UserAgentComponent] = field(default_factory=list)
62
+ additional_metadata: list[UserAgentComponent | RawStringUserAgentComponent] = field(
63
+ default_factory=list
64
+ )
65
+
66
+ def __str__(self) -> str:
67
+ components = [
68
+ *self.sdk_metadata,
69
+ *self.internal_metadata,
70
+ *self.ua_metadata,
71
+ *self.api_metadata,
72
+ *self.os_metadata,
73
+ *self.language_metadata,
74
+ *self.env_metadata,
75
+ *self.config_metadata,
76
+ *self.feat_metadata,
77
+ *self.additional_metadata,
78
+ ]
79
+ return " ".join([str(comp) for comp in components])
80
+
81
+
82
+ def sanitize_user_agent_string_component(raw_str: str, allow_hash: bool = False) -> str:
83
+ """Replaces all not allowed characters in the string with a dash ("-").
84
+
85
+ Allowed characters are ASCII alphanumerics and ``!$%&'*+-.^_`|~``. If
86
+ ``allow_hash`` is ``True``, "#"``" is also allowed.
87
+
88
+ :type raw_str: str
89
+ :param raw_str: The input string to be sanitized.
90
+
91
+ :type allow_hash: bool
92
+ :param allow_hash: Whether "#" is considered an allowed character.
93
+ """
94
+ return "".join(
95
+ c if c in _USERAGENT_ALLOWED_CHARACTERS or (allow_hash and c == "#") else "-"
96
+ for c in raw_str
97
+ )
smithy_http/utils.py ADDED
@@ -0,0 +1,125 @@
1
+ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ from collections.abc import Sequence
4
+ from urllib.parse import quote as urlquote
5
+
6
+ from smithy_core.exceptions import SmithyException
7
+
8
+
9
+ def split_header(given: str, handle_unquoted_http_date: bool = False) -> list[str]:
10
+ """Splits a header value into a list of strings.
11
+
12
+ The format is based on RFC9110's list production found in secion 5.6.1 with
13
+ the quoted string production found in section 5.6.4. In short:
14
+
15
+ A list is 1 or more elements surrounded by optional whitespace and separated by
16
+ commas. Elements may be quoted with double quotes (``"``) to contain leading or
17
+ trailing whitespace, commas, or double quotes. Inside the the double quotes, a
18
+ value may be escaped with a backslash (``\\``). Elements that contain no contents
19
+ are ignored.
20
+
21
+ If the list is known to contain unquoted IMF-fixdate formatted timestamps, the
22
+ ``handle_unquoted_http_date`` parameter can be set to ensure the list isn't
23
+ split on the commas inside the timestamps.
24
+
25
+ :param given: The header value to split.
26
+ :param handle_unquoted_http_date: Support splitting IMF-fixdate lists without
27
+ quotes. Defaults to False.
28
+ :returns: The header value split on commas.
29
+ """
30
+ result: list[str] = []
31
+
32
+ i = 0
33
+ while i < len(given):
34
+ if given[i].isspace():
35
+ # Skip any leading space.
36
+ i += 1
37
+ elif given[i] == '"':
38
+ # Grab the contents of the quoted value and append it.
39
+ entry, i = _consume_until(given, i + 1, '"', escape_char="\\")
40
+ result.append(entry)
41
+
42
+ if i > len(given) or given[i - 1] != '"':
43
+ raise SmithyException(
44
+ f"Invalid header list syntax: expected end quote but reached end "
45
+ f"of value: `{given}`"
46
+ )
47
+
48
+ # Skip until the next comma.
49
+ excess, i = _consume_until(given, i, ",")
50
+ if excess.strip():
51
+ raise SmithyException(
52
+ f"Invalid header list syntax: Found quote contents after "
53
+ f"end-quote: `{excess}` in `{given}`"
54
+ )
55
+ else:
56
+ entry, i = _consume_until(
57
+ given, i, ",", skip_first=handle_unquoted_http_date
58
+ )
59
+ if stripped := entry.strip():
60
+ result.append(stripped)
61
+
62
+ return result
63
+
64
+
65
+ def _consume_until(
66
+ given: str,
67
+ start_index: int,
68
+ end_char: str,
69
+ escape_char: str | None = None,
70
+ skip_first: bool = False,
71
+ ) -> tuple[str, int]:
72
+ """Creates a slice of the given string from the start index to the end character.
73
+
74
+ This also handles resolving escaped characters using the given escape_char if
75
+ provided.
76
+
77
+ If `skip_first` is true, the first instance of the end character will be skipped.
78
+ This is to enable support for unquoted IMF fixdate timestamps.
79
+
80
+ :param given: The whole header string.
81
+ :param start_index: The index at which to start slicing.
82
+ :param end_char: The character to split on. This is not included in the output.
83
+ :param escape_char: The character to escape with, e.g. ``\\``.
84
+ :param skip_first: Whether to skip the first instance of the end character.
85
+ :returns: A substring from the start index to the first instance of the end char.
86
+ """
87
+ should_skip = skip_first
88
+ end_index = start_index
89
+ result = ""
90
+ escaped = False
91
+ while end_index < len(given):
92
+ if escaped:
93
+ result += given[end_index]
94
+ escaped = False
95
+ elif given[end_index] == escape_char:
96
+ escaped = True
97
+ elif given[end_index] == end_char:
98
+ if should_skip:
99
+ result += given[end_index]
100
+ should_skip = False
101
+ else:
102
+ break
103
+ else:
104
+ result += given[end_index]
105
+ end_index += 1
106
+ return result, end_index + 1
107
+
108
+
109
+ def join_query_params(
110
+ params: Sequence[tuple[str, str | None]], prefix: str = ""
111
+ ) -> str:
112
+ """Join a list of query parameter key-value tuples.
113
+
114
+ :param params: The list of key-value query parameter tuples.
115
+ :param prefix: An optional query prefix.
116
+ """
117
+ query: str = prefix
118
+ for param in params:
119
+ if query:
120
+ query += "&"
121
+ if param[1] is None:
122
+ query += urlquote(param[0], safe="")
123
+ else:
124
+ query += f"{urlquote(param[0], safe='')}={urlquote(param[1], safe='')}"
125
+ return query
@@ -0,0 +1,12 @@
1
+ Metadata-Version: 2.4
2
+ Name: smithy-http
3
+ Version: 0.0.1
4
+ Summary: HTTP components for Smithy tooling.
5
+ License-File: NOTICE
6
+ Requires-Python: >=3.12
7
+ Requires-Dist: smithy-core
8
+ Provides-Extra: aiohttp
9
+ Requires-Dist: aiohttp<4.0,>=3.11.12; extra == 'aiohttp'
10
+ Requires-Dist: yarl; extra == 'aiohttp'
11
+ Provides-Extra: awscrt
12
+ Requires-Dist: awscrt>=0.23.10; extra == 'awscrt'
@@ -0,0 +1,28 @@
1
+ smithy_http/__init__.py,sha256=VL65hKy0ukGIPdFUqOSj1ULhegZ8Fod3hqFmxyuknds,8456
2
+ smithy_http/deserializers.py,sha256=rL5lniun4os_ayfJMIhJMG8thASFV1a8ybyT3yaC3Us,9356
3
+ smithy_http/endpoints.py,sha256=6CdEZbB71AEtnzK6m8gqD-OEh_Xo-g_habCfVkipuic,1064
4
+ smithy_http/exceptions.py,sha256=qMkaUoFbxn_VTTaxg2XROpe1im8nN5iuffasnuaJXfo,279
5
+ smithy_http/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ smithy_http/restjson.py,sha256=w92ojvaViS4Gr9coMQJKbrpv4bI57gp0DT6cyP85RZs,885
7
+ smithy_http/serializers.py,sha256=bIr_umBBY6hnLFWqFzI_N_DlN3d3MSO3BOaQbkDrQrg,24417
8
+ smithy_http/user_agent.py,sha256=hy1lmqlGTJkCW53bQ_BGhoztUFPvnyTW3_Ud78d--fU,3633
9
+ smithy_http/utils.py,sha256=uCcnfMR3fztxt3FQxb8teqpMd97KnmbJLmudHDbtSJE,4547
10
+ smithy_http/aio/__init__.py,sha256=inszysO2ouRKceDbmaYAnOsVK1vhOLL7mDab-x46z_o,2031
11
+ smithy_http/aio/aiohttp.py,sha256=KEVlQULb-h-WdZ-zZUsZYf31il1SA0RBpHwWcNMsvJo,4688
12
+ smithy_http/aio/crt.py,sha256=87hXetWA9wmQ4GH52bgrvvtDOt4VoroKX9Xaptrui7Q,17720
13
+ smithy_http/aio/protocols.py,sha256=PrXiOxxrQ1smEUcOPoCJDz4GYPrLSygoY8qrSGj7FxY,3984
14
+ smithy_http/aio/restjson.py,sha256=a97E3WUyg3tgu96PWohLom2_LeISxdZJZ2ZJTf803Pg,2269
15
+ smithy_http/aio/auth/__init__.py,sha256=5idTbB990emq5l8fB_u7RfQB5qGATEBtb4PxBU2IIhs,109
16
+ smithy_http/aio/auth/apikey.py,sha256=ln2gqGTcCR79wRKq6Ka1Zz8pFi-IWyyhdBTHgW2xBLY,4162
17
+ smithy_http/aio/identity/__init__.py,sha256=5idTbB990emq5l8fB_u7RfQB5qGATEBtb4PxBU2IIhs,109
18
+ smithy_http/aio/identity/apikey.py,sha256=f08WUjhmN_yb4C6xAIJZ1sV-tEgIvP0rNXNyLtFgIms,1338
19
+ smithy_http/aio/interfaces/__init__.py,sha256=USA3Ihby6q_KSHSQZi7sFfzznrQiJqjd7waYZpz61i0,2744
20
+ smithy_http/aio/interfaces/auth.py,sha256=wUy7AloDdo6Ex46y-ekrqU6tCxBDRDLoCf8_-HNxZVU,3426
21
+ smithy_http/interceptors/__init__.py,sha256=5idTbB990emq5l8fB_u7RfQB5qGATEBtb4PxBU2IIhs,109
22
+ smithy_http/interceptors/user_agent.py,sha256=xeiUTIY4fV5BIb2bsfTyHybwXxaEVTQ4dZ8NGyXo1t8,5678
23
+ smithy_http/interfaces/__init__.py,sha256=hVxbzSh-Nw8C4StRwll8WZbLWdHDvZX5AXiS03wfK3k,4033
24
+ smithy_http/plugins/__init__.py,sha256=BAITTpnR4vsEUtlTZ-2sYdebHot1_kAZqutXB-rV0ww,472
25
+ smithy_http-0.0.1.dist-info/METADATA,sha256=49s7eKpW6jh4U_6EXyVI2v97Ulb2a9a11CBEfoSXzXc,366
26
+ smithy_http-0.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
27
+ smithy_http-0.0.1.dist-info/licenses/NOTICE,sha256=1CkO1kwu3Q_OHYTj-d-yiBJA_lNN73a4zSntavaD4oc,67
28
+ smithy_http-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1 @@
1
+ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.