hip 0.2__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.
- hip/__init__.py +56 -0
- hip/__version__.py +2 -0
- hip/_body.py +73 -0
- hip/_client.py +180 -0
- hip/_connection.py +181 -0
- hip/_content.py +557 -0
- hip/_headers.py +251 -0
- hip/_network.py +252 -0
- hip/_parsers.py +531 -0
- hip/_pool.py +102 -0
- hip/_request.py +124 -0
- hip/_response.py +207 -0
- hip/_server.py +123 -0
- hip/_streams.py +199 -0
- hip/_urlencode.py +76 -0
- hip/_urlparse.py +533 -0
- hip/_urls.py +541 -0
- hip-0.2.dist-info/METADATA +111 -0
- hip-0.2.dist-info/RECORD +20 -0
- hip-0.2.dist-info/WHEEL +4 -0
hip/_urls.py
ADDED
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
from ._urlencode import unquote, urldecode, urlencode
|
|
6
|
+
from ._urlparse import urlparse
|
|
7
|
+
|
|
8
|
+
__all__ = ["URL", "QueryParams"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class URL:
|
|
12
|
+
"""
|
|
13
|
+
The components of a URL are broken down like this:
|
|
14
|
+
|
|
15
|
+
https://jo%40email.com:a%20secret@müller.de:1234/pa%20th?search=ab#anchorlink
|
|
16
|
+
[scheme] [ username ] [password] [ host ][port][ path ] [ query ] [fragment]
|
|
17
|
+
[ userinfo ] [ netloc ][ raw_path ]
|
|
18
|
+
|
|
19
|
+
Note that:
|
|
20
|
+
|
|
21
|
+
* `url.scheme` is normalized to always be lowercased.
|
|
22
|
+
|
|
23
|
+
* `url.host` is normalized to always be lowercased. Internationalized domain
|
|
24
|
+
names are represented in unicode, without IDNA encoding applied. For instance:
|
|
25
|
+
|
|
26
|
+
url = hip.URL("http://中国.icom.museum")
|
|
27
|
+
assert url.host == "中国.icom.museum"
|
|
28
|
+
url = hip.URL("http://xn--fiqs8s.icom.museum")
|
|
29
|
+
assert url.host == "中国.icom.museum"
|
|
30
|
+
|
|
31
|
+
* `url.raw_host` is normalized to always be lowercased, and is IDNA encoded.
|
|
32
|
+
|
|
33
|
+
url = hip.URL("http://中国.icom.museum")
|
|
34
|
+
assert url.raw_host == b"xn--fiqs8s.icom.museum"
|
|
35
|
+
url = hip.URL("http://xn--fiqs8s.icom.museum")
|
|
36
|
+
assert url.raw_host == b"xn--fiqs8s.icom.museum"
|
|
37
|
+
|
|
38
|
+
* `url.port` is either None or an integer. URLs that include the default port for
|
|
39
|
+
"http", "https", "ws", "wss", and "ftp" schemes have their port
|
|
40
|
+
normalized to `None`.
|
|
41
|
+
|
|
42
|
+
assert hip.URL("http://example.com") == hip.URL("http://example.com:80")
|
|
43
|
+
assert hip.URL("http://example.com").port is None
|
|
44
|
+
assert hip.URL("http://example.com:80").port is None
|
|
45
|
+
|
|
46
|
+
* `url.userinfo` is raw bytes, without URL escaping. Usually you'll want to work
|
|
47
|
+
with `url.username` and `url.password` instead, which handle the URL escaping.
|
|
48
|
+
|
|
49
|
+
* `url.raw_path` is raw bytes of both the path and query, without URL escaping.
|
|
50
|
+
This portion is used as the target when constructing HTTP requests. Usually you'll
|
|
51
|
+
want to work with `url.path` instead.
|
|
52
|
+
|
|
53
|
+
* `url.query` is raw bytes, without URL escaping. A URL query string portion can
|
|
54
|
+
only be properly URL escaped when decoding the parameter names and values
|
|
55
|
+
themselves.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
def __init__(self, url: URL | str = "", **kwargs: typing.Any) -> None:
|
|
59
|
+
if kwargs:
|
|
60
|
+
allowed = {
|
|
61
|
+
"scheme": str,
|
|
62
|
+
"username": str,
|
|
63
|
+
"password": str,
|
|
64
|
+
"userinfo": bytes,
|
|
65
|
+
"host": str,
|
|
66
|
+
"port": int,
|
|
67
|
+
"netloc": str,
|
|
68
|
+
"path": str,
|
|
69
|
+
"query": bytes,
|
|
70
|
+
"raw_path": bytes,
|
|
71
|
+
"fragment": str,
|
|
72
|
+
"params": object,
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
# Perform type checking for all supported keyword arguments.
|
|
76
|
+
for key, value in kwargs.items():
|
|
77
|
+
if key not in allowed:
|
|
78
|
+
message = f"{key!r} is an invalid keyword argument for URL()"
|
|
79
|
+
raise TypeError(message)
|
|
80
|
+
if value is not None and not isinstance(value, allowed[key]):
|
|
81
|
+
expected = allowed[key].__name__
|
|
82
|
+
seen = type(value).__name__
|
|
83
|
+
message = f"Argument {key!r} must be {expected} but got {seen}"
|
|
84
|
+
raise TypeError(message)
|
|
85
|
+
if isinstance(value, bytes):
|
|
86
|
+
kwargs[key] = value.decode("ascii")
|
|
87
|
+
|
|
88
|
+
if "params" in kwargs:
|
|
89
|
+
# Replace any "params" keyword with the raw "query" instead.
|
|
90
|
+
#
|
|
91
|
+
# Ensure that empty params use `kwargs["query"] = None` rather
|
|
92
|
+
# than `kwargs["query"] = ""`, so that generated URLs do not
|
|
93
|
+
# include an empty trailing "?".
|
|
94
|
+
params = kwargs.pop("params")
|
|
95
|
+
kwargs["query"] = None if not params else str(QueryParams(params))
|
|
96
|
+
|
|
97
|
+
if isinstance(url, str):
|
|
98
|
+
self._uri_reference = urlparse(url, **kwargs)
|
|
99
|
+
elif isinstance(url, URL):
|
|
100
|
+
self._uri_reference = url._uri_reference.copy_with(**kwargs)
|
|
101
|
+
else:
|
|
102
|
+
raise TypeError(
|
|
103
|
+
"Invalid type for url. Expected str or hip.URL,"
|
|
104
|
+
f" got {type(url)}: {url!r}"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
def scheme(self) -> str:
|
|
109
|
+
"""
|
|
110
|
+
The URL scheme, such as "http", "https".
|
|
111
|
+
Always normalised to lowercase.
|
|
112
|
+
"""
|
|
113
|
+
return self._uri_reference.scheme
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def userinfo(self) -> bytes:
|
|
117
|
+
"""
|
|
118
|
+
The URL userinfo as a raw bytestring.
|
|
119
|
+
For example: b"jo%40email.com:a%20secret".
|
|
120
|
+
"""
|
|
121
|
+
return self._uri_reference.userinfo.encode("ascii")
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def username(self) -> str:
|
|
125
|
+
"""
|
|
126
|
+
The URL username as a string, with URL decoding applied.
|
|
127
|
+
For example: "jo@email.com"
|
|
128
|
+
"""
|
|
129
|
+
userinfo = self._uri_reference.userinfo
|
|
130
|
+
return unquote(userinfo.partition(":")[0])
|
|
131
|
+
|
|
132
|
+
@property
|
|
133
|
+
def password(self) -> str:
|
|
134
|
+
"""
|
|
135
|
+
The URL password as a string, with URL decoding applied.
|
|
136
|
+
For example: "a secret"
|
|
137
|
+
"""
|
|
138
|
+
userinfo = self._uri_reference.userinfo
|
|
139
|
+
return unquote(userinfo.partition(":")[2])
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def host(self) -> str:
|
|
143
|
+
"""
|
|
144
|
+
The URL host as a string.
|
|
145
|
+
Always normalized to lowercase. Possibly IDNA encoded.
|
|
146
|
+
|
|
147
|
+
Examples:
|
|
148
|
+
|
|
149
|
+
url = hip.URL("http://www.EXAMPLE.org")
|
|
150
|
+
assert url.host == "www.example.org"
|
|
151
|
+
|
|
152
|
+
url = hip.URL("http://中国.icom.museum")
|
|
153
|
+
assert url.host == "xn--fiqs8s"
|
|
154
|
+
|
|
155
|
+
url = hip.URL("http://xn--fiqs8s.icom.museum")
|
|
156
|
+
assert url.host == "xn--fiqs8s"
|
|
157
|
+
|
|
158
|
+
url = hip.URL("https://[::ffff:192.168.0.1]")
|
|
159
|
+
assert url.host == "::ffff:192.168.0.1"
|
|
160
|
+
"""
|
|
161
|
+
return self._uri_reference.host
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def port(self) -> int | None:
|
|
165
|
+
"""
|
|
166
|
+
The URL port as an integer.
|
|
167
|
+
|
|
168
|
+
Note that the URL class performs port normalization as per the WHATWG spec.
|
|
169
|
+
Default ports for "http", "https", "ws", "wss", and "ftp" schemes are always
|
|
170
|
+
treated as `None`.
|
|
171
|
+
|
|
172
|
+
For example:
|
|
173
|
+
|
|
174
|
+
assert hip.URL("http://www.example.com") == hip.URL("http://www.example.com:80")
|
|
175
|
+
assert hip.URL("http://www.example.com:80").port is None
|
|
176
|
+
"""
|
|
177
|
+
return self._uri_reference.port
|
|
178
|
+
|
|
179
|
+
@property
|
|
180
|
+
def netloc(self) -> str:
|
|
181
|
+
"""
|
|
182
|
+
Either `<host>` or `<host>:<port>` as bytes.
|
|
183
|
+
Always normalized to lowercase, and IDNA encoded.
|
|
184
|
+
|
|
185
|
+
This property may be used for generating the value of a request
|
|
186
|
+
"Host" header.
|
|
187
|
+
"""
|
|
188
|
+
return self._uri_reference.netloc
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def path(self) -> str:
|
|
192
|
+
"""
|
|
193
|
+
The URL path as a string. Excluding the query string, and URL decoded.
|
|
194
|
+
|
|
195
|
+
For example:
|
|
196
|
+
|
|
197
|
+
url = hip.URL("https://example.com/pa%20th")
|
|
198
|
+
assert url.path == "/pa th"
|
|
199
|
+
"""
|
|
200
|
+
path = self._uri_reference.path or "/"
|
|
201
|
+
return unquote(path)
|
|
202
|
+
|
|
203
|
+
@property
|
|
204
|
+
def query(self) -> bytes:
|
|
205
|
+
"""
|
|
206
|
+
The URL query string, as raw bytes, excluding the leading b"?".
|
|
207
|
+
|
|
208
|
+
This is necessarily a bytewise interface, because we cannot
|
|
209
|
+
perform URL decoding of this representation until we've parsed
|
|
210
|
+
the keys and values into a QueryParams instance.
|
|
211
|
+
|
|
212
|
+
For example:
|
|
213
|
+
|
|
214
|
+
url = hip.URL("https://example.com/?filter=some%20search%20terms")
|
|
215
|
+
assert url.query == b"filter=some%20search%20terms"
|
|
216
|
+
"""
|
|
217
|
+
query = self._uri_reference.query or ""
|
|
218
|
+
return query.encode("ascii")
|
|
219
|
+
|
|
220
|
+
@property
|
|
221
|
+
def params(self) -> QueryParams:
|
|
222
|
+
"""
|
|
223
|
+
The URL query parameters, neatly parsed and packaged into an immutable
|
|
224
|
+
multidict representation.
|
|
225
|
+
"""
|
|
226
|
+
return QueryParams(self._uri_reference.query)
|
|
227
|
+
|
|
228
|
+
@property
|
|
229
|
+
def target(self) -> str:
|
|
230
|
+
"""
|
|
231
|
+
The complete URL path and query string as raw bytes.
|
|
232
|
+
Used as the target when constructing HTTP requests.
|
|
233
|
+
|
|
234
|
+
For example:
|
|
235
|
+
|
|
236
|
+
GET /users?search=some%20text HTTP/1.1
|
|
237
|
+
Host: www.example.org
|
|
238
|
+
Connection: close
|
|
239
|
+
"""
|
|
240
|
+
target = self._uri_reference.path or "/"
|
|
241
|
+
if self._uri_reference.query is not None:
|
|
242
|
+
target += "?" + self._uri_reference.query
|
|
243
|
+
return target
|
|
244
|
+
|
|
245
|
+
@property
|
|
246
|
+
def fragment(self) -> str:
|
|
247
|
+
"""
|
|
248
|
+
The URL fragments, as used in HTML anchors.
|
|
249
|
+
As a string, without the leading '#'.
|
|
250
|
+
"""
|
|
251
|
+
return unquote(self._uri_reference.fragment or "")
|
|
252
|
+
|
|
253
|
+
@property
|
|
254
|
+
def is_absolute_url(self) -> bool:
|
|
255
|
+
"""
|
|
256
|
+
Return `True` for absolute URLs such as 'http://example.com/path',
|
|
257
|
+
and `False` for relative URLs such as '/path'.
|
|
258
|
+
"""
|
|
259
|
+
# We don't use `.is_absolute` from `rfc3986` because it treats
|
|
260
|
+
# URLs with a fragment portion as not absolute.
|
|
261
|
+
# What we actually care about is if the URL provides
|
|
262
|
+
# a scheme and hostname to which connections should be made.
|
|
263
|
+
return bool(self._uri_reference.scheme and self._uri_reference.host)
|
|
264
|
+
|
|
265
|
+
@property
|
|
266
|
+
def is_relative_url(self) -> bool:
|
|
267
|
+
"""
|
|
268
|
+
Return `False` for absolute URLs such as 'http://example.com/path',
|
|
269
|
+
and `True` for relative URLs such as '/path'.
|
|
270
|
+
"""
|
|
271
|
+
return not self.is_absolute_url
|
|
272
|
+
|
|
273
|
+
def copy_with(self, **kwargs: typing.Any) -> URL:
|
|
274
|
+
"""
|
|
275
|
+
Copy this URL, returning a new URL with some components altered.
|
|
276
|
+
Accepts the same set of parameters as the components that are made
|
|
277
|
+
available via properties on the `URL` class.
|
|
278
|
+
|
|
279
|
+
For example:
|
|
280
|
+
|
|
281
|
+
url = hip.URL("https://www.example.com").copy_with(
|
|
282
|
+
username="jo@gmail.com", password="a secret"
|
|
283
|
+
)
|
|
284
|
+
assert url == "https://jo%40email.com:a%20secret@www.example.com"
|
|
285
|
+
"""
|
|
286
|
+
return URL(self, **kwargs)
|
|
287
|
+
|
|
288
|
+
def copy_set_param(self, key: str, value: typing.Any = None) -> URL:
|
|
289
|
+
return self.copy_with(params=self.params.copy_set(key, value))
|
|
290
|
+
|
|
291
|
+
def copy_append_param(self, key: str, value: typing.Any = None) -> URL:
|
|
292
|
+
return self.copy_with(params=self.params.copy_append(key, value))
|
|
293
|
+
|
|
294
|
+
def copy_remove_param(self, key: str) -> URL:
|
|
295
|
+
return self.copy_with(params=self.params.copy_remove(key))
|
|
296
|
+
|
|
297
|
+
def copy_merge_params(
|
|
298
|
+
self,
|
|
299
|
+
params: QueryParams | dict[str, str | list[str]] | list[tuple[str, str]] | None,
|
|
300
|
+
) -> URL:
|
|
301
|
+
return self.copy_with(params=self.params.copy_update(params))
|
|
302
|
+
|
|
303
|
+
def join(self, url: URL | str) -> URL:
|
|
304
|
+
"""
|
|
305
|
+
Return an absolute URL, using this URL as the base.
|
|
306
|
+
|
|
307
|
+
Eg.
|
|
308
|
+
|
|
309
|
+
url = hip.URL("https://www.example.com/test")
|
|
310
|
+
url = url.join("/new/path")
|
|
311
|
+
assert url == "https://www.example.com/new/path"
|
|
312
|
+
"""
|
|
313
|
+
from urllib.parse import urljoin
|
|
314
|
+
|
|
315
|
+
return URL(urljoin(str(self), str(URL(url))))
|
|
316
|
+
|
|
317
|
+
def __hash__(self) -> int:
|
|
318
|
+
return hash(str(self))
|
|
319
|
+
|
|
320
|
+
def __eq__(self, other: object) -> bool:
|
|
321
|
+
return isinstance(other, (URL, str)) and str(self) == str(URL(other))
|
|
322
|
+
|
|
323
|
+
def __str__(self) -> str:
|
|
324
|
+
return str(self._uri_reference)
|
|
325
|
+
|
|
326
|
+
def __repr__(self) -> str:
|
|
327
|
+
return f"<URL {str(self)!r}>"
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
class QueryParams(typing.Mapping[str, str]):
|
|
331
|
+
"""
|
|
332
|
+
URL query parameters, as a multi-dict.
|
|
333
|
+
"""
|
|
334
|
+
|
|
335
|
+
def __init__(
|
|
336
|
+
self,
|
|
337
|
+
params: (
|
|
338
|
+
QueryParams
|
|
339
|
+
| dict[str, str | list[str]]
|
|
340
|
+
| list[tuple[str, str]]
|
|
341
|
+
| str
|
|
342
|
+
| None
|
|
343
|
+
) = None,
|
|
344
|
+
) -> None:
|
|
345
|
+
d: dict[str, list[str]] = {}
|
|
346
|
+
|
|
347
|
+
if params is None:
|
|
348
|
+
d = {}
|
|
349
|
+
elif isinstance(params, str):
|
|
350
|
+
d = urldecode(params)
|
|
351
|
+
elif isinstance(params, QueryParams):
|
|
352
|
+
d = params.multi_dict()
|
|
353
|
+
elif isinstance(params, dict):
|
|
354
|
+
# Convert dict inputs like:
|
|
355
|
+
# {"a": "123", "b": ["456", "789"]}
|
|
356
|
+
# To dict inputs where values are always lists, like:
|
|
357
|
+
# {"a": ["123"], "b": ["456", "789"]}
|
|
358
|
+
d = {k: [v] if isinstance(v, str) else list(v) for k, v in params.items()}
|
|
359
|
+
else:
|
|
360
|
+
# Convert list inputs like:
|
|
361
|
+
# [("a", "123"), ("a", "456"), ("b", "789")]
|
|
362
|
+
# To a dict representation, like:
|
|
363
|
+
# {"a": ["123", "456"], "b": ["789"]}
|
|
364
|
+
for k, v in params:
|
|
365
|
+
d.setdefault(k, []).append(v)
|
|
366
|
+
|
|
367
|
+
self._dict = d
|
|
368
|
+
|
|
369
|
+
def keys(self) -> typing.KeysView[str]:
|
|
370
|
+
"""
|
|
371
|
+
Return all the keys in the query params.
|
|
372
|
+
|
|
373
|
+
Usage:
|
|
374
|
+
|
|
375
|
+
q = hip.QueryParams("a=123&a=456&b=789")
|
|
376
|
+
assert list(q.keys()) == ["a", "b"]
|
|
377
|
+
"""
|
|
378
|
+
return self._dict.keys()
|
|
379
|
+
|
|
380
|
+
def values(self) -> typing.ValuesView[str]:
|
|
381
|
+
"""
|
|
382
|
+
Return all the values in the query params. If a key occurs more than once
|
|
383
|
+
only the first item for that key is returned.
|
|
384
|
+
|
|
385
|
+
Usage:
|
|
386
|
+
|
|
387
|
+
q = hip.QueryParams("a=123&a=456&b=789")
|
|
388
|
+
assert list(q.values()) == ["123", "789"]
|
|
389
|
+
"""
|
|
390
|
+
return {k: v[0] for k, v in self._dict.items()}.values()
|
|
391
|
+
|
|
392
|
+
def items(self) -> typing.ItemsView[str, str]:
|
|
393
|
+
"""
|
|
394
|
+
Return all items in the query params. If a key occurs more than once
|
|
395
|
+
only the first item for that key is returned.
|
|
396
|
+
|
|
397
|
+
Usage:
|
|
398
|
+
|
|
399
|
+
q = hip.QueryParams("a=123&a=456&b=789")
|
|
400
|
+
assert list(q.items()) == [("a", "123"), ("b", "789")]
|
|
401
|
+
"""
|
|
402
|
+
return {k: v[0] for k, v in self._dict.items()}.items()
|
|
403
|
+
|
|
404
|
+
def multi_items(self) -> list[tuple[str, str]]:
|
|
405
|
+
"""
|
|
406
|
+
Return all items in the query params. Allow duplicate keys to occur.
|
|
407
|
+
|
|
408
|
+
Usage:
|
|
409
|
+
|
|
410
|
+
q = hip.QueryParams("a=123&a=456&b=789")
|
|
411
|
+
assert list(q.multi_items()) == [("a", "123"), ("a", "456"), ("b", "789")]
|
|
412
|
+
"""
|
|
413
|
+
multi_items: list[tuple[str, str]] = []
|
|
414
|
+
for k, v in self._dict.items():
|
|
415
|
+
multi_items.extend([(k, i) for i in v])
|
|
416
|
+
return multi_items
|
|
417
|
+
|
|
418
|
+
def multi_dict(self) -> dict[str, list[str]]:
|
|
419
|
+
return {k: list(v) for k, v in self._dict.items()}
|
|
420
|
+
|
|
421
|
+
def get(self, key: str, default: typing.Any = None) -> typing.Any:
|
|
422
|
+
"""
|
|
423
|
+
Get a value from the query param for a given key. If the key occurs
|
|
424
|
+
more than once, then only the first value is returned.
|
|
425
|
+
|
|
426
|
+
Usage:
|
|
427
|
+
|
|
428
|
+
q = hip.QueryParams("a=123&a=456&b=789")
|
|
429
|
+
assert q.get("a") == "123"
|
|
430
|
+
"""
|
|
431
|
+
if key in self._dict:
|
|
432
|
+
return self._dict[key][0]
|
|
433
|
+
return default
|
|
434
|
+
|
|
435
|
+
def get_list(self, key: str) -> list[str]:
|
|
436
|
+
"""
|
|
437
|
+
Get all values from the query param for a given key.
|
|
438
|
+
|
|
439
|
+
Usage:
|
|
440
|
+
|
|
441
|
+
q = hip.QueryParams("a=123&a=456&b=789")
|
|
442
|
+
assert q.get_list("a") == ["123", "456"]
|
|
443
|
+
"""
|
|
444
|
+
return list(self._dict.get(key, []))
|
|
445
|
+
|
|
446
|
+
def copy_set(self, key: str, value: str) -> QueryParams:
|
|
447
|
+
"""
|
|
448
|
+
Return a new QueryParams instance, setting the value of a key.
|
|
449
|
+
|
|
450
|
+
Usage:
|
|
451
|
+
|
|
452
|
+
q = hip.QueryParams("a=123")
|
|
453
|
+
q = q.set("a", "456")
|
|
454
|
+
assert q == hip.QueryParams("a=456")
|
|
455
|
+
"""
|
|
456
|
+
q = QueryParams()
|
|
457
|
+
q._dict = dict(self._dict)
|
|
458
|
+
q._dict[key] = [value]
|
|
459
|
+
return q
|
|
460
|
+
|
|
461
|
+
def copy_append(self, key: str, value: str) -> QueryParams:
|
|
462
|
+
"""
|
|
463
|
+
Return a new QueryParams instance, setting or appending the value of a key.
|
|
464
|
+
|
|
465
|
+
Usage:
|
|
466
|
+
|
|
467
|
+
q = hip.QueryParams("a=123")
|
|
468
|
+
q = q.append("a", "456")
|
|
469
|
+
assert q == hip.QueryParams("a=123&a=456")
|
|
470
|
+
"""
|
|
471
|
+
q = QueryParams()
|
|
472
|
+
q._dict = dict(self._dict)
|
|
473
|
+
q._dict[key] = q.get_list(key) + [value]
|
|
474
|
+
return q
|
|
475
|
+
|
|
476
|
+
def copy_remove(self, key: str) -> QueryParams:
|
|
477
|
+
"""
|
|
478
|
+
Return a new QueryParams instance, removing the value of a key.
|
|
479
|
+
|
|
480
|
+
Usage:
|
|
481
|
+
|
|
482
|
+
q = hip.QueryParams("a=123")
|
|
483
|
+
q = q.remove("a")
|
|
484
|
+
assert q == hip.QueryParams("")
|
|
485
|
+
"""
|
|
486
|
+
q = QueryParams()
|
|
487
|
+
q._dict = dict(self._dict)
|
|
488
|
+
q._dict.pop(str(key), None)
|
|
489
|
+
return q
|
|
490
|
+
|
|
491
|
+
def copy_update(
|
|
492
|
+
self,
|
|
493
|
+
params: (
|
|
494
|
+
QueryParams | dict[str, str | list[str]] | list[tuple[str, str]] | None
|
|
495
|
+
) = None,
|
|
496
|
+
) -> QueryParams:
|
|
497
|
+
"""
|
|
498
|
+
Return a new QueryParams instance, updated with.
|
|
499
|
+
|
|
500
|
+
Usage:
|
|
501
|
+
|
|
502
|
+
q = hip.QueryParams("a=123")
|
|
503
|
+
q = q.copy_update({"b": "456"})
|
|
504
|
+
assert q == hip.QueryParams("a=123&b=456")
|
|
505
|
+
|
|
506
|
+
q = hip.QueryParams("a=123")
|
|
507
|
+
q = q.copy_update({"a": "456", "b": "789"})
|
|
508
|
+
assert q == hip.QueryParams("a=456&b=789")
|
|
509
|
+
"""
|
|
510
|
+
q = QueryParams(params)
|
|
511
|
+
q._dict = {**self._dict, **q._dict}
|
|
512
|
+
return q
|
|
513
|
+
|
|
514
|
+
def __getitem__(self, key: str) -> str:
|
|
515
|
+
return self._dict[key][0]
|
|
516
|
+
|
|
517
|
+
def __contains__(self, key: typing.Any) -> bool:
|
|
518
|
+
return key in self._dict
|
|
519
|
+
|
|
520
|
+
def __iter__(self) -> typing.Iterator[str]:
|
|
521
|
+
return iter(self.keys())
|
|
522
|
+
|
|
523
|
+
def __len__(self) -> int:
|
|
524
|
+
return len(self._dict)
|
|
525
|
+
|
|
526
|
+
def __bool__(self) -> bool:
|
|
527
|
+
return bool(self._dict)
|
|
528
|
+
|
|
529
|
+
def __hash__(self) -> int:
|
|
530
|
+
return hash(str(self))
|
|
531
|
+
|
|
532
|
+
def __eq__(self, other: object) -> bool:
|
|
533
|
+
if not isinstance(other, self.__class__):
|
|
534
|
+
return False
|
|
535
|
+
return sorted(self.multi_items()) == sorted(other.multi_items())
|
|
536
|
+
|
|
537
|
+
def __str__(self) -> str:
|
|
538
|
+
return urlencode(self.multi_dict())
|
|
539
|
+
|
|
540
|
+
def __repr__(self) -> str:
|
|
541
|
+
return f"<QueryParams {str(self)!r}>"
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: hip
|
|
3
|
+
Version: 0.2
|
|
4
|
+
Summary: HTTP, for Python.
|
|
5
|
+
Author-email: Kim Christie <noreply@lovelydinosaur.com>
|
|
6
|
+
Classifier: Development Status :: 4 - Beta
|
|
7
|
+
Classifier: Environment :: Web Environment
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Requires-Dist: truststore
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
<p align="center">
|
|
21
|
+
<img width="350" height="208" src="img/butterfly.png" alt='hip'>
|
|
22
|
+
</p>
|
|
23
|
+
|
|
24
|
+
# [hip](README.md)
|
|
25
|
+
|
|
26
|
+
Engineered for simplicity, `hip` provides a complete Python web suite…
|
|
27
|
+
|
|
28
|
+
* Supports both client and server functionality.
|
|
29
|
+
* Presents a clear, simple, and tightly constrained API.
|
|
30
|
+
* A minimal, easily readable implementation.
|
|
31
|
+
|
|
32
|
+
...we're currently in early stages design, and would love for you to get involved.
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
Install the package from PyPI…
|
|
37
|
+
|
|
38
|
+
```shell
|
|
39
|
+
$ uv add hip --dev
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Client
|
|
43
|
+
|
|
44
|
+
Sending HTTP requests…
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
>>> import hip
|
|
48
|
+
|
|
49
|
+
>>> cli = hip.Client()
|
|
50
|
+
>>> r = cli.get('https://www.example.com/')
|
|
51
|
+
>>> r
|
|
52
|
+
<Response [200 OK]>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Server
|
|
56
|
+
|
|
57
|
+
Creating web apps…
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
>>> import hip
|
|
61
|
+
|
|
62
|
+
>>> def app(request):
|
|
63
|
+
... content = hip.HTML('<html><body>¡Hola, mundo!</body></html>')
|
|
64
|
+
... return hip.Response(200, content=content)
|
|
65
|
+
|
|
66
|
+
>>> s = hip.Server(app)
|
|
67
|
+
>>> s.serve()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Motivation
|
|
73
|
+
|
|
74
|
+
A clean modern design, with [a focused API](API.md) and an exceptionally light installation footprint.
|
|
75
|
+
|
|
76
|
+
For comparison, see `hip` <small>vs</small> `requests`…
|
|
77
|
+
|
|
78
|
+
```shell
|
|
79
|
+
$ uv add hip
|
|
80
|
+
$ uv tree --show-sizes --package hip
|
|
81
|
+
hip v0.2 (33.5KiB)
|
|
82
|
+
└── truststore v0.10.0 (17.8KiB)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
<p></p>
|
|
86
|
+
|
|
87
|
+
```shell
|
|
88
|
+
$ uv add requests
|
|
89
|
+
$ uv tree --show-sizes --package requests
|
|
90
|
+
requests v2.33.1 (63.4KiB)
|
|
91
|
+
├── certifi v2026.2.25 (150.1KiB)
|
|
92
|
+
├── charset-normalizer v3.4.7 (302.4KiB)
|
|
93
|
+
├── idna v3.11 (69.3KiB)
|
|
94
|
+
└── urllib3 v2.6.3 (128.5KiB)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Contribution
|
|
98
|
+
|
|
99
|
+
The `hip` redesign is currently in early development.
|
|
100
|
+
|
|
101
|
+
We are currently [seeking sponsorship](https://github.com/sponsors/encode), and looking to expand our team.
|
|
102
|
+
|
|
103
|
+
If you'd like to discuss getting involved, please [book in some time](https://calendly.com/encode-oss).
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
<p align="center">
|
|
108
|
+
<a href="license/">MIT Licensed</a>.<br/>Designed & Built with Love.
|
|
109
|
+
</p>
|
|
110
|
+
|
|
111
|
+
<p align="center">— 💖 —</p>
|
hip-0.2.dist-info/RECORD
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
hip/__init__.py,sha256=fMdwKymfRrkv5T_9kQO9maRQ5N4gqYdXDb9Ci6p180g,1432
|
|
2
|
+
hip/__version__.py,sha256=UVbVCjLXrHEPzO2Td-MVr7m6XJD-AUBnXekDZC0X-dI,38
|
|
3
|
+
hip/_body.py,sha256=P-EzmhSv72xLG4jiSYnx7DgkwuWRiHl5jrnkMk6GtCg,2029
|
|
4
|
+
hip/_client.py,sha256=eguyGsJH6fm-btNf5eYHB_376SzO8N9caqeaxgEiU18,5266
|
|
5
|
+
hip/_connection.py,sha256=ZfUjeVBU83K8PrCc7A8wLp5-P61tY3oUu0rnV0Nmk48,5449
|
|
6
|
+
hip/_content.py,sha256=5XBYaPyreh4CBWDsUFKJqFpk7tkQJBNj_ZXtkfbeIVs,15414
|
|
7
|
+
hip/_headers.py,sha256=AT4gyVSFdtjC0gr_XOx-vElwIo4oUREIt4oAZ8AIMZU,7745
|
|
8
|
+
hip/_network.py,sha256=HA30TdpjcWcewnzmTzVOKpk70wwJ3RDzEkyJvExSS88,7089
|
|
9
|
+
hip/_parsers.py,sha256=7MKEbs3ziJZHJxZ9DtYdNKxWfKC8KX2O0zP9SnySeYo,17947
|
|
10
|
+
hip/_pool.py,sha256=2bHfJQ2OAqRV2hnZj3bQc7WRCeQs0alcsKw0O2zTlqk,3109
|
|
11
|
+
hip/_request.py,sha256=kku77zFhY1L5GwOaF6DqgX40wNwf-TihYekIrhInY0o,3595
|
|
12
|
+
hip/_response.py,sha256=zXdH3sO4L6CoypJtY1q7Xi11Pwr4qGdPlkaLtGr93TQ,6432
|
|
13
|
+
hip/_server.py,sha256=yyUlrLqCF5lzyua903-CGP9KfykuiFV4yyLVHCAWGxs,4190
|
|
14
|
+
hip/_streams.py,sha256=qnPlHuMIzVkkYcw-FPZa134wFBLegiaKiouKjhTR4Qc,5871
|
|
15
|
+
hip/_urlencode.py,sha256=5aquNwuGOyDRalc9gfWjdoIoV68FQXJWkDfDIOihuA8,2332
|
|
16
|
+
hip/_urlparse.py,sha256=AiZE-dxoUQ3h4-gRE2nR6kBlTV6n0Z-kHNsfR7eOmZE,18727
|
|
17
|
+
hip/_urls.py,sha256=Q9ZynmDsDs23H9N2MOYoFqp_NIn0xCksnGscA6_ugNk,17396
|
|
18
|
+
hip-0.2.dist-info/METADATA,sha256=vS1e098IY_CwnyrD7rGYh4k7PHE_OkYTl_aUU_mXrFw,2625
|
|
19
|
+
hip-0.2.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
20
|
+
hip-0.2.dist-info/RECORD,,
|
hip-0.2.dist-info/WHEEL
ADDED