-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrequest.py
More file actions
151 lines (118 loc) · 3.33 KB
/
request.py
File metadata and controls
151 lines (118 loc) · 3.33 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from __future__ import annotations
import sys
import urllib.parse
from dataclasses import dataclass, field
from enum import auto
from typing import TYPE_CHECKING, Any
from view.core.body import BodyMixin
from view.core.multi_map import MultiMap
from view.core.router import normalize_route
if TYPE_CHECKING:
from collections.abc import Mapping
from view.core.app import BaseApp
from view.core.headers import HTTPHeaders
__all__ = "Method", "Request"
if sys.version_info >= (3, 11):
from enum import StrEnum
else:
from enum import Enum
class StrEnum(str, Enum):
pass
class _UpperStrEnum(StrEnum):
@staticmethod
def _generate_next_value_(
name: str,
*_: Any,
) -> str:
return name.upper()
class Method(_UpperStrEnum):
"""
The HTTP request method.
"""
GET = auto()
"""
The GET method requests a representation of the specified resource.
Requests using GET should only retrieve data and should not contain
a request content.
"""
POST = auto()
"""
The POST method submits an entity to the specified resource, often causing
a change in state or side effects on the server.
"""
PUT = auto()
"""
The PUT method replaces all current representations of the target resource
with the request content.
"""
PATCH = auto()
"""
The PATCH method applies partial modifications to a resource.
"""
DELETE = auto()
"""
The DELETE method deletes the specified resource.
"""
CONNECT = auto()
"""
The CONNECT method establishes a tunnel to the server identified by the
target resource.
"""
OPTIONS = auto()
"""
The OPTIONS method describes the communication options for the target
resource.
"""
TRACE = auto()
"""
The TRACE method performs a message loop-back test along the path to the
target resource.
"""
HEAD = auto()
"""
The HEAD method asks for a response identical to a GET request, but
without a response body.
"""
@dataclass(slots=True)
class Request(BodyMixin):
"""
Dataclass representing an HTTP request.
"""
app: BaseApp
"""
The app associated with the HTTP request.
"""
path: str
"""
The path of the request, with the leading '/' and without a trailing '/'
or query string.
"""
method: Method
"""
The HTTP method of the request. See :class:`Method`.
"""
headers: HTTPHeaders
"""
A "multi-dictionary" containing the request headers. This is :class:`dict`-like,
but if a header has multiple values, it is represented by a list.
"""
query_parameters: MultiMap[str, str]
"""
The query string parameters of the HTTP request.
"""
path_parameters: Mapping[str, str] = field(
default_factory=dict, init=False
)
"""
The path parameters of this request.
"""
def __post_init__(self) -> None:
self.path = normalize_route(self.path)
def extract_query_parameters(query_string: str | bytes) -> MultiMap[str, str]:
"""
Extract a query string from a URL and return it as a multi-map.
"""
if isinstance(query_string, bytes):
query_string = query_string.decode("utf-8")
assert isinstance(query_string, str), query_string
return MultiMap(urllib.parse.parse_qsl(query_string))