hip 0.2__tar.gz
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-0.2/.gitignore +15 -0
- hip-0.2/PKG-INFO +111 -0
- hip-0.2/docs/README.md +92 -0
- hip-0.2/pyproject.toml +48 -0
- hip-0.2/src/hip/__init__.py +56 -0
- hip-0.2/src/hip/__version__.py +2 -0
- hip-0.2/src/hip/_body.py +73 -0
- hip-0.2/src/hip/_client.py +180 -0
- hip-0.2/src/hip/_connection.py +181 -0
- hip-0.2/src/hip/_content.py +557 -0
- hip-0.2/src/hip/_headers.py +251 -0
- hip-0.2/src/hip/_network.py +252 -0
- hip-0.2/src/hip/_parsers.py +531 -0
- hip-0.2/src/hip/_pool.py +102 -0
- hip-0.2/src/hip/_request.py +124 -0
- hip-0.2/src/hip/_response.py +207 -0
- hip-0.2/src/hip/_server.py +123 -0
- hip-0.2/src/hip/_streams.py +199 -0
- hip-0.2/src/hip/_urlencode.py +76 -0
- hip-0.2/src/hip/_urlparse.py +533 -0
- hip-0.2/src/hip/_urls.py +541 -0
hip-0.2/.gitignore
ADDED
hip-0.2/PKG-INFO
ADDED
|
@@ -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/docs/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="350" height="208" src="img/butterfly.png" alt='hip'>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# [hip](README.md)
|
|
6
|
+
|
|
7
|
+
Engineered for simplicity, `hip` provides a complete Python web suite…
|
|
8
|
+
|
|
9
|
+
* Supports both client and server functionality.
|
|
10
|
+
* Presents a clear, simple, and tightly constrained API.
|
|
11
|
+
* A minimal, easily readable implementation.
|
|
12
|
+
|
|
13
|
+
...we're currently in early stages design, and would love for you to get involved.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Install the package from PyPI…
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
$ uv add hip --dev
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Client
|
|
24
|
+
|
|
25
|
+
Sending HTTP requests…
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
>>> import hip
|
|
29
|
+
|
|
30
|
+
>>> cli = hip.Client()
|
|
31
|
+
>>> r = cli.get('https://www.example.com/')
|
|
32
|
+
>>> r
|
|
33
|
+
<Response [200 OK]>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Server
|
|
37
|
+
|
|
38
|
+
Creating web apps…
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
>>> import hip
|
|
42
|
+
|
|
43
|
+
>>> def app(request):
|
|
44
|
+
... content = hip.HTML('<html><body>¡Hola, mundo!</body></html>')
|
|
45
|
+
... return hip.Response(200, content=content)
|
|
46
|
+
|
|
47
|
+
>>> s = hip.Server(app)
|
|
48
|
+
>>> s.serve()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Motivation
|
|
54
|
+
|
|
55
|
+
A clean modern design, with [a focused API](API.md) and an exceptionally light installation footprint.
|
|
56
|
+
|
|
57
|
+
For comparison, see `hip` <small>vs</small> `requests`…
|
|
58
|
+
|
|
59
|
+
```shell
|
|
60
|
+
$ uv add hip
|
|
61
|
+
$ uv tree --show-sizes --package hip
|
|
62
|
+
hip v0.2 (33.5KiB)
|
|
63
|
+
└── truststore v0.10.0 (17.8KiB)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
<p></p>
|
|
67
|
+
|
|
68
|
+
```shell
|
|
69
|
+
$ uv add requests
|
|
70
|
+
$ uv tree --show-sizes --package requests
|
|
71
|
+
requests v2.33.1 (63.4KiB)
|
|
72
|
+
├── certifi v2026.2.25 (150.1KiB)
|
|
73
|
+
├── charset-normalizer v3.4.7 (302.4KiB)
|
|
74
|
+
├── idna v3.11 (69.3KiB)
|
|
75
|
+
└── urllib3 v2.6.3 (128.5KiB)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Contribution
|
|
79
|
+
|
|
80
|
+
The `hip` redesign is currently in early development.
|
|
81
|
+
|
|
82
|
+
We are currently [seeking sponsorship](https://github.com/sponsors/encode), and looking to expand our team.
|
|
83
|
+
|
|
84
|
+
If you'd like to discuss getting involved, please [book in some time](https://calendly.com/encode-oss).
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
<p align="center">
|
|
89
|
+
<a href="license/">MIT Licensed</a>.<br/>Designed & Built with Love.
|
|
90
|
+
</p>
|
|
91
|
+
|
|
92
|
+
<p align="center">— 💖 —</p>
|
hip-0.2/pyproject.toml
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "hip"
|
|
7
|
+
description = "HTTP, for Python."
|
|
8
|
+
readme = "docs/README.md"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "Kim Christie", email = "noreply@lovelydinosaur.com" },
|
|
12
|
+
]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Environment :: Web Environment",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"truststore",
|
|
27
|
+
]
|
|
28
|
+
dynamic = [
|
|
29
|
+
"version"
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.sdist]
|
|
33
|
+
include = [
|
|
34
|
+
"/src",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[tool.hatch.version]
|
|
38
|
+
path = "src/hip/__version__.py"
|
|
39
|
+
|
|
40
|
+
[dependency-groups]
|
|
41
|
+
dev = [
|
|
42
|
+
"mkdocs==2.0.dev6",
|
|
43
|
+
"mypy==2.3.1",
|
|
44
|
+
"pytest==8.3.5",
|
|
45
|
+
"pytest-cov==7.1.0",
|
|
46
|
+
"ruff",
|
|
47
|
+
"typing-extensions>=4.0",
|
|
48
|
+
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from .__version__ import __title__, __version__
|
|
2
|
+
from ._client import * # Client
|
|
3
|
+
from ._connection import * # Connection, Transport
|
|
4
|
+
from ._content import * # Binary, Content, File, Files, Form, HTML, JSON, MultiPart, Text
|
|
5
|
+
from ._headers import * # Headers
|
|
6
|
+
from ._network import * # NetworkBackend, NetworkStream, timeout
|
|
7
|
+
from ._parsers import * # HTTPParser, ProtocolError
|
|
8
|
+
from ._pool import * # ConnectionPool
|
|
9
|
+
from ._request import * # Method, Request
|
|
10
|
+
from ._response import * # StatusCode, Response
|
|
11
|
+
from ._server import * # Server # serve_http, run
|
|
12
|
+
from ._streams import * # ByteStream, DuplexStream, FileStream, Stream # ByteStream, DuplexStream, FileStream, Stream
|
|
13
|
+
from ._urlencode import * # quote, unquote, urldecode, urlencode
|
|
14
|
+
from ._urls import * # QueryParams, URL
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"HTML",
|
|
18
|
+
"JSON",
|
|
19
|
+
"URL",
|
|
20
|
+
"Binary",
|
|
21
|
+
"ByteStream",
|
|
22
|
+
"Client",
|
|
23
|
+
"Connection",
|
|
24
|
+
"ConnectionPool",
|
|
25
|
+
"Content",
|
|
26
|
+
"DuplexStream",
|
|
27
|
+
"File",
|
|
28
|
+
"FileStream",
|
|
29
|
+
"Files",
|
|
30
|
+
"Form",
|
|
31
|
+
"HTTPParser",
|
|
32
|
+
"Headers",
|
|
33
|
+
"Method",
|
|
34
|
+
"MultiPart",
|
|
35
|
+
"NetworkBackend",
|
|
36
|
+
"NetworkStream",
|
|
37
|
+
"ProtocolError",
|
|
38
|
+
"QueryParams",
|
|
39
|
+
"Request",
|
|
40
|
+
"Response",
|
|
41
|
+
"Server",
|
|
42
|
+
"StatusCode",
|
|
43
|
+
"Stream",
|
|
44
|
+
"Text",
|
|
45
|
+
"Transport",
|
|
46
|
+
"__title__",
|
|
47
|
+
"__version__",
|
|
48
|
+
"open_connection",
|
|
49
|
+
"quote",
|
|
50
|
+
"run",
|
|
51
|
+
"serve_http",
|
|
52
|
+
"timeout",
|
|
53
|
+
"unquote",
|
|
54
|
+
"urldecode",
|
|
55
|
+
"urlencode",
|
|
56
|
+
]
|
hip-0.2/src/hip/_body.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import io
|
|
2
|
+
|
|
3
|
+
from ._content import Content
|
|
4
|
+
from ._headers import Headers
|
|
5
|
+
from ._parsers import HTTPParser
|
|
6
|
+
from ._streams import Stream
|
|
7
|
+
|
|
8
|
+
__all__ = ["HTTPStream", "RequestContent", "ResponseContent"]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class RequestContent(Content):
|
|
12
|
+
def __init__(self, parser: HTTPParser):
|
|
13
|
+
self._parser = parser
|
|
14
|
+
|
|
15
|
+
def open(self) -> Stream:
|
|
16
|
+
return HTTPStream(self._parser, is_response=False)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ResponseContent(Content):
|
|
20
|
+
def __init__(self, parser: HTTPParser):
|
|
21
|
+
self._parser = parser
|
|
22
|
+
|
|
23
|
+
def open(self) -> Stream:
|
|
24
|
+
return HTTPStream(self._parser, is_response=True)
|
|
25
|
+
|
|
26
|
+
def headers(self) -> Headers:
|
|
27
|
+
return Headers()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class HTTPStream(Stream):
|
|
31
|
+
def __init__(self, parser: HTTPParser, is_response: bool):
|
|
32
|
+
self._parser = parser
|
|
33
|
+
self._is_response = is_response
|
|
34
|
+
self._buffer = io.BytesIO()
|
|
35
|
+
self._finished = False
|
|
36
|
+
|
|
37
|
+
def read(self, size=-1) -> bytes:
|
|
38
|
+
if self._finished:
|
|
39
|
+
return b""
|
|
40
|
+
|
|
41
|
+
sections = []
|
|
42
|
+
length = 0
|
|
43
|
+
|
|
44
|
+
# If we have any data in the buffer read that and clear the buffer.
|
|
45
|
+
buffered = self._buffer.read()
|
|
46
|
+
if buffered:
|
|
47
|
+
sections.append(buffered)
|
|
48
|
+
length += len(buffered)
|
|
49
|
+
self._buffer.seek(0)
|
|
50
|
+
self._buffer.truncate(0)
|
|
51
|
+
|
|
52
|
+
# Read each chunk in turn.
|
|
53
|
+
while (size < 0) or (length < size):
|
|
54
|
+
section = self._parser.recv_body()
|
|
55
|
+
sections.append(section)
|
|
56
|
+
length += len(section)
|
|
57
|
+
if section == b"":
|
|
58
|
+
self._finished = True
|
|
59
|
+
break
|
|
60
|
+
|
|
61
|
+
# If we've more data than requested, then push some back into the buffer.
|
|
62
|
+
output = b"".join(sections)
|
|
63
|
+
if size > -1 and len(output) > size:
|
|
64
|
+
output, remainder = output[:size], output[size:]
|
|
65
|
+
self._buffer.write(remainder)
|
|
66
|
+
self._buffer.seek(0)
|
|
67
|
+
|
|
68
|
+
return output
|
|
69
|
+
|
|
70
|
+
def close(self) -> None:
|
|
71
|
+
self._buffer.close()
|
|
72
|
+
if self._is_response:
|
|
73
|
+
self._parser.complete()
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import types
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
from ._content import HTML, JSON, Binary, Content, Text, select_media
|
|
5
|
+
from ._headers import Headers
|
|
6
|
+
from ._pool import ConnectionPool, Transport
|
|
7
|
+
from ._request import Method, Request
|
|
8
|
+
from ._response import Response
|
|
9
|
+
from ._urls import URL
|
|
10
|
+
|
|
11
|
+
__all__ = ["Client"]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Client:
|
|
15
|
+
def __init__(
|
|
16
|
+
self,
|
|
17
|
+
url: URL | str | None = None,
|
|
18
|
+
headers: Headers | typing.Mapping[str, str] | None = None,
|
|
19
|
+
):
|
|
20
|
+
if url is None:
|
|
21
|
+
url = ""
|
|
22
|
+
if headers is None:
|
|
23
|
+
headers = {"User-Agent": "dev"}
|
|
24
|
+
self.transport = ConnectionPool()
|
|
25
|
+
|
|
26
|
+
self.url = URL(url)
|
|
27
|
+
self.headers = Headers(headers)
|
|
28
|
+
self.via = RedirectMiddleware(self.transport)
|
|
29
|
+
self.media = {
|
|
30
|
+
"application/json": JSON,
|
|
31
|
+
"text/html": HTML,
|
|
32
|
+
"text/*": Text,
|
|
33
|
+
"*/*": Binary,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
def build_request(
|
|
37
|
+
self,
|
|
38
|
+
method: Method | str,
|
|
39
|
+
url: URL | str,
|
|
40
|
+
headers: Headers | dict[str, str] | None = None,
|
|
41
|
+
content: Content | bytes | None = None,
|
|
42
|
+
) -> Request:
|
|
43
|
+
return Request(
|
|
44
|
+
method=method,
|
|
45
|
+
url=self.url.join(url),
|
|
46
|
+
headers=self.headers.copy_update(headers),
|
|
47
|
+
content=content,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def request(
|
|
51
|
+
self,
|
|
52
|
+
method: Method | str,
|
|
53
|
+
url: URL | str,
|
|
54
|
+
headers: Headers | dict[str, str] | None = None,
|
|
55
|
+
content: Content | bytes | None = None,
|
|
56
|
+
) -> Response:
|
|
57
|
+
request = self.build_request(method, url, headers=headers, content=content)
|
|
58
|
+
with self.via.send(request) as stream:
|
|
59
|
+
ct = stream.headers.get("Content-Type", "application/octet-stream")
|
|
60
|
+
cls = select_media(self.media, ct)
|
|
61
|
+
content = cls.parse(stream)
|
|
62
|
+
return Response(
|
|
63
|
+
status_code=stream.status_code,
|
|
64
|
+
headers=stream.headers,
|
|
65
|
+
content=content,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def stream(
|
|
69
|
+
self,
|
|
70
|
+
method: Method | str,
|
|
71
|
+
url: URL | str,
|
|
72
|
+
headers: Headers | dict[str, str] | None = None,
|
|
73
|
+
content: Content | bytes | None = None,
|
|
74
|
+
) -> Response:
|
|
75
|
+
request = self.build_request(method, url, headers=headers, content=content)
|
|
76
|
+
return self.via.send(request)
|
|
77
|
+
|
|
78
|
+
def get(
|
|
79
|
+
self,
|
|
80
|
+
url: URL | str,
|
|
81
|
+
headers: Headers | dict[str, str] | None = None,
|
|
82
|
+
):
|
|
83
|
+
return self.request("GET", url, headers=headers)
|
|
84
|
+
|
|
85
|
+
def post(
|
|
86
|
+
self,
|
|
87
|
+
url: URL | str,
|
|
88
|
+
headers: Headers | dict[str, str] | None = None,
|
|
89
|
+
content: Content | bytes | None = None,
|
|
90
|
+
):
|
|
91
|
+
return self.request("POST", url, headers=headers, content=content)
|
|
92
|
+
|
|
93
|
+
def put(
|
|
94
|
+
self,
|
|
95
|
+
url: URL | str,
|
|
96
|
+
headers: Headers | dict[str, str] | None = None,
|
|
97
|
+
content: Content | bytes | None = None,
|
|
98
|
+
):
|
|
99
|
+
return self.request("PUT", url, headers=headers, content=content)
|
|
100
|
+
|
|
101
|
+
def patch(
|
|
102
|
+
self,
|
|
103
|
+
url: URL | str,
|
|
104
|
+
headers: Headers | dict[str, str] | None = None,
|
|
105
|
+
content: Content | bytes | None = None,
|
|
106
|
+
):
|
|
107
|
+
return self.request("PATCH", url, headers=headers, content=content)
|
|
108
|
+
|
|
109
|
+
def delete(
|
|
110
|
+
self,
|
|
111
|
+
url: URL | str,
|
|
112
|
+
headers: Headers | dict[str, str] | None = None,
|
|
113
|
+
):
|
|
114
|
+
return self.request("DELETE", url, headers=headers)
|
|
115
|
+
|
|
116
|
+
def close(self):
|
|
117
|
+
self.via.close()
|
|
118
|
+
|
|
119
|
+
def __enter__(self):
|
|
120
|
+
return self
|
|
121
|
+
|
|
122
|
+
def __exit__(
|
|
123
|
+
self,
|
|
124
|
+
exc_type: type[BaseException] | None = None,
|
|
125
|
+
exc_value: BaseException | None = None,
|
|
126
|
+
traceback: types.TracebackType | None = None,
|
|
127
|
+
):
|
|
128
|
+
self.close()
|
|
129
|
+
|
|
130
|
+
def __repr__(self):
|
|
131
|
+
return f"<Client [{self.transport.description()}]>"
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class RedirectMiddleware(Transport):
|
|
135
|
+
def __init__(self, transport: Transport) -> None:
|
|
136
|
+
self._transport = transport
|
|
137
|
+
|
|
138
|
+
def build_redirect_request(
|
|
139
|
+
self, request: Request, response: Response
|
|
140
|
+
) -> Request | None:
|
|
141
|
+
# Redirect status codes...
|
|
142
|
+
if response.status_code not in (301, 302, 303, 307, 308):
|
|
143
|
+
return None
|
|
144
|
+
|
|
145
|
+
# Redirects need a valid location header...
|
|
146
|
+
try:
|
|
147
|
+
location = URL(response.headers["Location"])
|
|
148
|
+
except (KeyError, ValueError):
|
|
149
|
+
return None
|
|
150
|
+
|
|
151
|
+
# Instantiate a redirect request...
|
|
152
|
+
method = request.method
|
|
153
|
+
url = request.url.join(location)
|
|
154
|
+
headers = request.headers
|
|
155
|
+
content = request.content
|
|
156
|
+
|
|
157
|
+
return Request(method, url, headers, content)
|
|
158
|
+
|
|
159
|
+
def send(self, request: Request) -> Response:
|
|
160
|
+
while True:
|
|
161
|
+
response = self._transport.send(request)
|
|
162
|
+
|
|
163
|
+
# Determine if we have a redirect or not.
|
|
164
|
+
redirect = self.build_redirect_request(request, response)
|
|
165
|
+
|
|
166
|
+
# If we don't have a redirect, we're done.
|
|
167
|
+
if redirect is None:
|
|
168
|
+
return response
|
|
169
|
+
|
|
170
|
+
# If we have a redirect, then we read the body of the response.
|
|
171
|
+
# Ensures that the HTTP connection is available for a new
|
|
172
|
+
# request/response cycle.
|
|
173
|
+
with response as stream:
|
|
174
|
+
stream.read()
|
|
175
|
+
|
|
176
|
+
# Make the next request
|
|
177
|
+
request = redirect
|
|
178
|
+
|
|
179
|
+
def close(self):
|
|
180
|
+
self._transport.close()
|