forked from smithy-lang/smithy-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
130 lines (92 loc) · 3.77 KB
/
__init__.py
File metadata and controls
130 lines (92 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from collections.abc import Iterator
from dataclasses import dataclass
from typing import Literal, Protocol
FieldPosition = Literal["header", "trailer"]
"""The type of a field.
Defines its placement in a request or response.
header: Header field. In HTTP this is a header as defined in RFC 9110 Section 6.3.
trailer: Trailer field. In HTTP this is a trailer as defined in RFC 9110 Section 6.5.
Implementations of other protocols may use this FieldPosition for similar types of metadata.
"""
class Field(Protocol):
"""A name-value pair representing a single field in a request or response.
The kind will dictate metadata placement within an the message, for example as
header or trailer field in a HTTP request as defined in RFC 9110 Section 5.
All field names are case insensitive and case-variance must be treated as
equivalent. Names may be normalized but should be preserved for accuracy during
transmission.
"""
name: str
values: list[str]
kind: FieldPosition = "header"
def add(self, value: str) -> None:
"""Append a value to a field."""
...
def set(self, values: list[str]) -> None:
"""Overwrite existing field values."""
...
def remove(self, value: str) -> None:
"""Remove all matching entries from list."""
...
def as_string(self) -> str:
"""Serialize the ``Field``'s values into a single line string."""
...
def as_tuples(self) -> list[tuple[str, str]]:
"""Get list of ``name``, ``value`` tuples where each tuple represents one
value."""
...
class Fields(Protocol):
"""Protocol agnostic mapping of key-value pair request metadata, such as HTTP
fields."""
# Entries are keyed off the name of a provided Field
entries: dict[str, Field]
encoding: str = "utf-8"
def set_field(self, field: Field) -> None:
"""Alias for __setitem__ to utilize the field.name for the entry key."""
...
def __setitem__(self, name: str, field: Field) -> None:
"""Set entry for a Field name."""
...
def __getitem__(self, name: str) -> Field:
"""Retrieve Field entry."""
...
def __delitem__(self, name: str) -> None:
"""Delete entry from collection."""
...
def __iter__(self) -> Iterator[Field]:
"""Allow iteration over entries."""
...
def __len__(self) -> int:
"""Get total number of Field entries."""
...
def __contains__(self, key: str) -> bool:
"""Allows in/not in checks on Field entries."""
...
def get_by_type(self, kind: FieldPosition) -> list[Field]:
"""Helper function for retrieving specific types of fields.
Used to grab all headers or all trailers.
"""
...
def extend(self, other: "Fields") -> None:
"""Merges ``entries`` of ``other`` into the current ``entries``.
For every `Field` in the ``entries`` of ``other``: If the normalized name
already exists in the current ``entries``, the values from ``other`` are
appended. Otherwise, the ``Field`` is added to the list of ``entries``.
"""
...
QueryParamsList = list[tuple[str, str]]
@dataclass(kw_only=True)
class HTTPClientConfiguration:
"""Client-level HTTP configuration.
:param force_http_2: Whether to require HTTP/2.
"""
force_http_2: bool = False
@dataclass(kw_only=True)
class HTTPRequestConfiguration:
"""Request-level HTTP configuration.
:param read_timeout: How long, in seconds, the client will attempt to read the first
byte over an established, open connection before timing out.
"""
read_timeout: float | None = None