reqmachine 0.1.0__tar.gz → 0.1.1__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.
- {reqmachine-0.1.0 → reqmachine-0.1.1}/PKG-INFO +98 -84
- {reqmachine-0.1.0 → reqmachine-0.1.1}/README.md +96 -83
- {reqmachine-0.1.0 → reqmachine-0.1.1}/pyproject.toml +3 -2
- {reqmachine-0.1.0 → reqmachine-0.1.1}/pyproject.toml.orig +4 -3
- {reqmachine-0.1.0 → reqmachine-0.1.1}/reqmachine/__init__.py +4 -4
- {reqmachine-0.1.0 → reqmachine-0.1.1}/reqmachine/__main__.py +0 -3
- reqmachine-0.1.0/reqmachine/http_client.py → reqmachine-0.1.1/reqmachine/client.py +78 -53
- reqmachine-0.1.1/reqmachine/src/adapter.py +80 -0
- {reqmachine-0.1.0/reqmachine → reqmachine-0.1.1/reqmachine/src}/config.py +4 -1
- reqmachine-0.1.1/reqmachine/src/types.py +7 -0
- reqmachine-0.1.0/reqmachine/types.py +0 -12
- {reqmachine-0.1.0/reqmachine → reqmachine-0.1.1/reqmachine/src}/errors.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: reqmachine
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: A small requests-based HTTP client with retries, JSON parsing and file upload/download helpers.
|
|
5
5
|
Author: aligeromachine
|
|
6
6
|
Author-email: aligeromachine <aligeromachine@github.com>
|
|
@@ -13,6 +13,7 @@ Classifier: Programming Language :: Python :: 3.14
|
|
|
13
13
|
Classifier: Topic :: Internet :: WWW/HTTP
|
|
14
14
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
15
|
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Dist: msgspec>=0.22.0
|
|
16
17
|
Requires-Dist: orjson>=3.11.9
|
|
17
18
|
Requires-Dist: requests>=2.34.2
|
|
18
19
|
Requires-Dist: urllib3>=2.5.0
|
|
@@ -25,6 +26,7 @@ Project-URL: Repository, https://github.com/aligeromachine/reqmachine
|
|
|
25
26
|
Project-URL: Changelog, https://github.com/aligeromachine/reqmachine/blob/main/CHANGES.md
|
|
26
27
|
Description-Content-Type: text/markdown
|
|
27
28
|
|
|
29
|
+
```markdown
|
|
28
30
|
# Welcome to **`reqmachine`**
|
|
29
31
|
|
|
30
32
|
A small [requests](https://requests.readthedocs.io/)-based HTTP client with
|
|
@@ -46,105 +48,87 @@ pip install reqmachine
|
|
|
46
48
|
|
|
47
49
|
## Usage
|
|
48
50
|
|
|
49
|
-
|
|
51
|
+
`@http_client` injects a fresh `HttpClient` as the first argument.
|
|
52
|
+
`get/post/put/patch/delete` accept a type and return parsed data
|
|
53
|
+
(`None` for an empty body), raising `HttpClientError` subclasses on
|
|
54
|
+
failure. `http_get` / `http_post` are one-shot helpers.
|
|
50
55
|
|
|
51
56
|
```python
|
|
52
|
-
from
|
|
57
|
+
from dataclasses import dataclass
|
|
58
|
+
from reqmachine import HttpClient, http_client
|
|
53
59
|
|
|
54
|
-
with HttpClient() as client:
|
|
55
|
-
users = client.get("https://jsonplaceholder.typicode.com/users")
|
|
56
|
-
posts = client.get("https://jsonplaceholder.typicode.com/posts")
|
|
57
|
-
```
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
@dataclass
|
|
62
|
+
class Geo:
|
|
63
|
+
lat: str
|
|
64
|
+
lng: str
|
|
61
65
|
|
|
62
|
-
### Decorators
|
|
63
|
-
|
|
64
|
-
```python
|
|
65
|
-
from reqmachine import HttpClient, http_client, http_get
|
|
66
66
|
|
|
67
|
-
@
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
@dataclass
|
|
68
|
+
class Address:
|
|
69
|
+
street: str
|
|
70
|
+
suite: str
|
|
71
|
+
city: str
|
|
72
|
+
zipcode: str
|
|
73
|
+
geo: Geo
|
|
71
74
|
|
|
72
|
-
`@http_client` injects a fresh `HttpClient` as the first argument;
|
|
73
|
-
`http_get` / `http_post` are one-shot helpers that open a client per call.
|
|
74
75
|
|
|
75
|
-
|
|
76
|
+
@dataclass
|
|
77
|
+
class Company:
|
|
78
|
+
name: str
|
|
79
|
+
catchPhrase: str # имя как в JSON (camelCase — валидный идентификатор Python)
|
|
80
|
+
bs: str
|
|
76
81
|
|
|
77
|
-
```python
|
|
78
|
-
from reqmachine import HttpClient
|
|
79
82
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
@dataclass
|
|
84
|
+
class User:
|
|
85
|
+
id: int
|
|
86
|
+
name: str
|
|
87
|
+
username: str
|
|
88
|
+
email: str
|
|
89
|
+
address: Address
|
|
90
|
+
phone: str
|
|
91
|
+
website: str
|
|
92
|
+
company: Company
|
|
87
93
|
|
|
88
|
-
Retries apply to `HEAD/GET/PUT/DELETE/OPTIONS/TRACE` on statuses
|
|
89
|
-
`429, 500, 502, 503, 504` (see `Config`).
|
|
90
94
|
|
|
91
|
-
|
|
95
|
+
@dataclass
|
|
96
|
+
class Post:
|
|
97
|
+
id: int
|
|
98
|
+
userId: int
|
|
99
|
+
title: str
|
|
100
|
+
body: str
|
|
92
101
|
|
|
93
|
-
```python
|
|
94
|
-
from pathlib import Path
|
|
95
|
-
from reqmachine import HttpClient
|
|
96
102
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
client.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
(`JSONData` by default) and uses a longer timeout.
|
|
104
|
-
|
|
105
|
-
## Error types
|
|
103
|
+
@http_client
|
|
104
|
+
def fetch_posts(client: HttpClient):
|
|
105
|
+
users = client.get("https://jsonplaceholder.typicode.com/users", User)
|
|
106
|
+
if isinstance(users, list):
|
|
107
|
+
for it in users:
|
|
108
|
+
print(it.company)
|
|
106
109
|
|
|
107
|
-
|
|
108
|
-
|
|
110
|
+
posts = client.get("https://jsonplaceholder.typicode.com/posts", Post)
|
|
111
|
+
if isinstance(posts, list):
|
|
112
|
+
for it in posts:
|
|
113
|
+
print(it.title)
|
|
109
114
|
|
|
110
|
-
| Exception | When |
|
|
111
|
-
|---|---|
|
|
112
|
-
| `HttpStatusError` | Response status is not 2xx/3xx (`.status`, `.response`) |
|
|
113
|
-
| `HttpConnectionError` | Could not establish a connection |
|
|
114
|
-
| `HttpTimeoutError` | Connect/read timeout |
|
|
115
|
-
| `HttpDecodeError` | Response body is not valid JSON |
|
|
116
|
-
| `HttpClientError` | Base class / any other requests error |
|
|
117
115
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
with HttpClient() as client:
|
|
122
|
-
try:
|
|
123
|
-
data = client.get("https://example.com/api")
|
|
124
|
-
except HttpStatusError as e:
|
|
125
|
-
print("server said", e.status)
|
|
126
|
-
except HttpTimeoutError:
|
|
127
|
-
print("too slow")
|
|
116
|
+
if __name__ == "__main__":
|
|
117
|
+
fetch_posts()
|
|
128
118
|
```
|
|
129
119
|
|
|
130
|
-
|
|
131
|
-
`True` on success. `is_url_available()` never raises — it returns
|
|
132
|
-
`False` on any network error.
|
|
120
|
+
## Tuning
|
|
133
121
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
```bash
|
|
137
|
-
uv sync
|
|
138
|
-
uv run ruff check .
|
|
139
|
-
uv run pytest
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
Быстрая проверка проброса ошибок без pytest/responses/сети:
|
|
122
|
+
```python
|
|
123
|
+
from reqmachine import HttpClient
|
|
143
124
|
|
|
144
|
-
|
|
145
|
-
|
|
125
|
+
client = HttpClient(
|
|
126
|
+
verify=True, # TLS verification (default: off)
|
|
127
|
+
timeout=(3.0, 60.0), # (connect, read)
|
|
128
|
+
retries=3, # urllib3 Retry total
|
|
129
|
+
headers={"Authorization": "Bearer <token>"},
|
|
130
|
+
)
|
|
146
131
|
```
|
|
147
|
-
|
|
148
132
|
### Test service (Django)
|
|
149
133
|
|
|
150
134
|
`service/` contains a small Django app that implements every endpoint
|
|
@@ -172,18 +156,48 @@ uv run python service/manage.py runserver 8000
|
|
|
172
156
|
|
|
173
157
|
Example with the client:
|
|
174
158
|
|
|
159
|
+
|
|
160
|
+
## Files
|
|
161
|
+
|
|
175
162
|
```python
|
|
163
|
+
from pathlib import Path
|
|
176
164
|
from reqmachine import HttpClient
|
|
177
165
|
|
|
178
|
-
with HttpClient(
|
|
179
|
-
|
|
180
|
-
client.
|
|
181
|
-
|
|
166
|
+
with HttpClient() as client:
|
|
167
|
+
client.upload_file("https://example.com/upload", Path("data.bin"))
|
|
168
|
+
client.download_file("https://example.com/file", Path("out/file.bin"))
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
`upload_file` sends the file as multipart with a JSON metadata field
|
|
172
|
+
(`JSONData` by default). Both return `True` on success.
|
|
173
|
+
|
|
174
|
+
## Errors
|
|
175
|
+
|
|
176
|
+
All exceptions derive from `HttpClientError`:
|
|
177
|
+
|
|
178
|
+
| Exception | When |
|
|
179
|
+
|---|---|
|
|
180
|
+
| `HttpStatusError` | Status is not 2xx/3xx (`.status`, `.response`) |
|
|
181
|
+
| `HttpConnectionError` | Connection could not be established |
|
|
182
|
+
| `HttpTimeoutError` | Connect/read timeout |
|
|
183
|
+
| `HttpDecodeError` | Body is not valid JSON |
|
|
184
|
+
|
|
185
|
+
`is_url_available()` never raises — returns `False` on any network error.
|
|
186
|
+
|
|
187
|
+
## Development
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
uv sync
|
|
191
|
+
uv run ruff check .
|
|
192
|
+
uv run pytest
|
|
182
193
|
```
|
|
183
194
|
|
|
184
|
-
|
|
195
|
+
Local Django test service (`service/`, serves every endpoint scenario):
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
uv run python service/manage.py runserver 8000
|
|
199
|
+
```
|
|
185
200
|
|
|
186
201
|
## License
|
|
187
202
|
|
|
188
203
|
MIT — see [LICENSE](LICENSE).
|
|
189
|
-
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
```markdown
|
|
1
2
|
# Welcome to **`reqmachine`**
|
|
2
3
|
|
|
3
4
|
A small [requests](https://requests.readthedocs.io/)-based HTTP client with
|
|
@@ -19,105 +20,87 @@ pip install reqmachine
|
|
|
19
20
|
|
|
20
21
|
## Usage
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
`@http_client` injects a fresh `HttpClient` as the first argument.
|
|
24
|
+
`get/post/put/patch/delete` accept a type and return parsed data
|
|
25
|
+
(`None` for an empty body), raising `HttpClientError` subclasses on
|
|
26
|
+
failure. `http_get` / `http_post` are one-shot helpers.
|
|
23
27
|
|
|
24
28
|
```python
|
|
25
|
-
from
|
|
29
|
+
from dataclasses import dataclass
|
|
30
|
+
from reqmachine import HttpClient, http_client
|
|
26
31
|
|
|
27
|
-
with HttpClient() as client:
|
|
28
|
-
users = client.get("https://jsonplaceholder.typicode.com/users")
|
|
29
|
-
posts = client.get("https://jsonplaceholder.typicode.com/posts")
|
|
30
|
-
```
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
@dataclass
|
|
34
|
+
class Geo:
|
|
35
|
+
lat: str
|
|
36
|
+
lng: str
|
|
34
37
|
|
|
35
|
-
### Decorators
|
|
36
|
-
|
|
37
|
-
```python
|
|
38
|
-
from reqmachine import HttpClient, http_client, http_get
|
|
39
38
|
|
|
40
|
-
@
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
@dataclass
|
|
40
|
+
class Address:
|
|
41
|
+
street: str
|
|
42
|
+
suite: str
|
|
43
|
+
city: str
|
|
44
|
+
zipcode: str
|
|
45
|
+
geo: Geo
|
|
44
46
|
|
|
45
|
-
`@http_client` injects a fresh `HttpClient` as the first argument;
|
|
46
|
-
`http_get` / `http_post` are one-shot helpers that open a client per call.
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
@dataclass
|
|
49
|
+
class Company:
|
|
50
|
+
name: str
|
|
51
|
+
catchPhrase: str # имя как в JSON (camelCase — валидный идентификатор Python)
|
|
52
|
+
bs: str
|
|
49
53
|
|
|
50
|
-
```python
|
|
51
|
-
from reqmachine import HttpClient
|
|
52
54
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
@dataclass
|
|
56
|
+
class User:
|
|
57
|
+
id: int
|
|
58
|
+
name: str
|
|
59
|
+
username: str
|
|
60
|
+
email: str
|
|
61
|
+
address: Address
|
|
62
|
+
phone: str
|
|
63
|
+
website: str
|
|
64
|
+
company: Company
|
|
60
65
|
|
|
61
|
-
Retries apply to `HEAD/GET/PUT/DELETE/OPTIONS/TRACE` on statuses
|
|
62
|
-
`429, 500, 502, 503, 504` (see `Config`).
|
|
63
66
|
|
|
64
|
-
|
|
67
|
+
@dataclass
|
|
68
|
+
class Post:
|
|
69
|
+
id: int
|
|
70
|
+
userId: int
|
|
71
|
+
title: str
|
|
72
|
+
body: str
|
|
65
73
|
|
|
66
|
-
```python
|
|
67
|
-
from pathlib import Path
|
|
68
|
-
from reqmachine import HttpClient
|
|
69
74
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
client.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
(`JSONData` by default) and uses a longer timeout.
|
|
77
|
-
|
|
78
|
-
## Error types
|
|
75
|
+
@http_client
|
|
76
|
+
def fetch_posts(client: HttpClient):
|
|
77
|
+
users = client.get("https://jsonplaceholder.typicode.com/users", User)
|
|
78
|
+
if isinstance(users, list):
|
|
79
|
+
for it in users:
|
|
80
|
+
print(it.company)
|
|
79
81
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
posts = client.get("https://jsonplaceholder.typicode.com/posts", Post)
|
|
83
|
+
if isinstance(posts, list):
|
|
84
|
+
for it in posts:
|
|
85
|
+
print(it.title)
|
|
82
86
|
|
|
83
|
-
| Exception | When |
|
|
84
|
-
|---|---|
|
|
85
|
-
| `HttpStatusError` | Response status is not 2xx/3xx (`.status`, `.response`) |
|
|
86
|
-
| `HttpConnectionError` | Could not establish a connection |
|
|
87
|
-
| `HttpTimeoutError` | Connect/read timeout |
|
|
88
|
-
| `HttpDecodeError` | Response body is not valid JSON |
|
|
89
|
-
| `HttpClientError` | Base class / any other requests error |
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
with HttpClient() as client:
|
|
95
|
-
try:
|
|
96
|
-
data = client.get("https://example.com/api")
|
|
97
|
-
except HttpStatusError as e:
|
|
98
|
-
print("server said", e.status)
|
|
99
|
-
except HttpTimeoutError:
|
|
100
|
-
print("too slow")
|
|
88
|
+
if __name__ == "__main__":
|
|
89
|
+
fetch_posts()
|
|
101
90
|
```
|
|
102
91
|
|
|
103
|
-
|
|
104
|
-
`True` on success. `is_url_available()` never raises — it returns
|
|
105
|
-
`False` on any network error.
|
|
92
|
+
## Tuning
|
|
106
93
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
```bash
|
|
110
|
-
uv sync
|
|
111
|
-
uv run ruff check .
|
|
112
|
-
uv run pytest
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
Быстрая проверка проброса ошибок без pytest/responses/сети:
|
|
94
|
+
```python
|
|
95
|
+
from reqmachine import HttpClient
|
|
116
96
|
|
|
117
|
-
|
|
118
|
-
|
|
97
|
+
client = HttpClient(
|
|
98
|
+
verify=True, # TLS verification (default: off)
|
|
99
|
+
timeout=(3.0, 60.0), # (connect, read)
|
|
100
|
+
retries=3, # urllib3 Retry total
|
|
101
|
+
headers={"Authorization": "Bearer <token>"},
|
|
102
|
+
)
|
|
119
103
|
```
|
|
120
|
-
|
|
121
104
|
### Test service (Django)
|
|
122
105
|
|
|
123
106
|
`service/` contains a small Django app that implements every endpoint
|
|
@@ -145,18 +128,48 @@ uv run python service/manage.py runserver 8000
|
|
|
145
128
|
|
|
146
129
|
Example with the client:
|
|
147
130
|
|
|
131
|
+
|
|
132
|
+
## Files
|
|
133
|
+
|
|
148
134
|
```python
|
|
135
|
+
from pathlib import Path
|
|
149
136
|
from reqmachine import HttpClient
|
|
150
137
|
|
|
151
|
-
with HttpClient(
|
|
152
|
-
|
|
153
|
-
client.
|
|
154
|
-
|
|
138
|
+
with HttpClient() as client:
|
|
139
|
+
client.upload_file("https://example.com/upload", Path("data.bin"))
|
|
140
|
+
client.download_file("https://example.com/file", Path("out/file.bin"))
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
`upload_file` sends the file as multipart with a JSON metadata field
|
|
144
|
+
(`JSONData` by default). Both return `True` on success.
|
|
145
|
+
|
|
146
|
+
## Errors
|
|
147
|
+
|
|
148
|
+
All exceptions derive from `HttpClientError`:
|
|
149
|
+
|
|
150
|
+
| Exception | When |
|
|
151
|
+
|---|---|
|
|
152
|
+
| `HttpStatusError` | Status is not 2xx/3xx (`.status`, `.response`) |
|
|
153
|
+
| `HttpConnectionError` | Connection could not be established |
|
|
154
|
+
| `HttpTimeoutError` | Connect/read timeout |
|
|
155
|
+
| `HttpDecodeError` | Body is not valid JSON |
|
|
156
|
+
|
|
157
|
+
`is_url_available()` never raises — returns `False` on any network error.
|
|
158
|
+
|
|
159
|
+
## Development
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
uv sync
|
|
163
|
+
uv run ruff check .
|
|
164
|
+
uv run pytest
|
|
155
165
|
```
|
|
156
166
|
|
|
157
|
-
|
|
167
|
+
Local Django test service (`service/`, serves every endpoint scenario):
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
uv run python service/manage.py runserver 8000
|
|
171
|
+
```
|
|
158
172
|
|
|
159
173
|
## License
|
|
160
174
|
|
|
161
175
|
MIT — see [LICENSE](LICENSE).
|
|
162
|
-
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["
|
|
2
|
+
requires = ["uv-build>=0.12.0,<0.14.0"]
|
|
3
3
|
build-backend = "uv_build"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "reqmachine"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.1"
|
|
8
8
|
description = "A small requests-based HTTP client with retries, JSON parsing and file upload/download helpers."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -20,6 +20,7 @@ classifiers = [
|
|
|
20
20
|
"Typing :: Typed",
|
|
21
21
|
]
|
|
22
22
|
dependencies = [
|
|
23
|
+
"msgspec>=0.22.0",
|
|
23
24
|
"orjson>=3.11.9",
|
|
24
25
|
"requests>=2.34.2",
|
|
25
26
|
"urllib3>=2.5.0",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["
|
|
2
|
+
requires = ["uv-build>=0.12.0,<0.14.0"]
|
|
3
3
|
build-backend = "uv_build"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "reqmachine"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.1"
|
|
8
8
|
description = "A small requests-based HTTP client with retries, JSON parsing and file upload/download helpers."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -28,6 +28,7 @@ classifiers = [
|
|
|
28
28
|
"Typing :: Typed",
|
|
29
29
|
]
|
|
30
30
|
dependencies = [
|
|
31
|
+
"msgspec>=0.22.0",
|
|
31
32
|
"orjson>=3.11.9",
|
|
32
33
|
"requests>=2.34.2",
|
|
33
34
|
"urllib3>=2.5.0",
|
|
@@ -52,4 +53,4 @@ testpaths = ["tests"]
|
|
|
52
53
|
|
|
53
54
|
[tool.uv.build-backend]
|
|
54
55
|
module-root = ""
|
|
55
|
-
module-name = "reqmachine"
|
|
56
|
+
module-name = "reqmachine"
|
|
@@ -4,16 +4,16 @@ Retries, JSON parsing (orjson), file upload/download helpers and
|
|
|
4
4
|
convenience decorators on top of :mod:`requests`.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
from .
|
|
8
|
-
from .
|
|
7
|
+
from .client import HttpClient, http_client, http_get, http_post
|
|
8
|
+
from .src.config import Config
|
|
9
|
+
from .src.errors import (
|
|
9
10
|
HttpClientError,
|
|
10
11
|
HttpConnectionError,
|
|
11
12
|
HttpDecodeError,
|
|
12
13
|
HttpStatusError,
|
|
13
14
|
HttpTimeoutError,
|
|
14
15
|
)
|
|
15
|
-
from .
|
|
16
|
-
from .types import JsonDict, JsonList, JsonValue
|
|
16
|
+
from .src.types import JsonDict, JsonList, JsonValue
|
|
17
17
|
|
|
18
18
|
__version__ = "0.1.0"
|
|
19
19
|
|
|
@@ -1,32 +1,35 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
1
2
|
from functools import wraps
|
|
2
3
|
from pathlib import Path
|
|
3
|
-
from typing import
|
|
4
|
+
from typing import Any, Concatenate, Self
|
|
4
5
|
|
|
5
6
|
import orjson
|
|
6
7
|
import requests
|
|
7
8
|
import urllib3
|
|
8
|
-
from urllib3.util.retry import Retry
|
|
9
9
|
from requests.adapters import HTTPAdapter
|
|
10
10
|
from requests.exceptions import (
|
|
11
11
|
ConnectionError as ReqConnectionError,
|
|
12
|
+
)
|
|
13
|
+
from requests.exceptions import (
|
|
12
14
|
HTTPError,
|
|
13
15
|
RequestException,
|
|
16
|
+
)
|
|
17
|
+
from requests.exceptions import (
|
|
14
18
|
Timeout as ReqTimeout,
|
|
15
19
|
)
|
|
20
|
+
from urllib3.util.retry import Retry
|
|
16
21
|
|
|
17
|
-
from .
|
|
18
|
-
|
|
22
|
+
from reqmachine.src.adapter import DictValidator, ListValidator
|
|
23
|
+
|
|
24
|
+
from .src.config import Config
|
|
25
|
+
from .src.errors import (
|
|
19
26
|
HttpClientError,
|
|
20
27
|
HttpConnectionError,
|
|
21
28
|
HttpDecodeError,
|
|
22
29
|
HttpStatusError,
|
|
23
30
|
HttpTimeoutError,
|
|
24
31
|
)
|
|
25
|
-
from .types import
|
|
26
|
-
ResponseFunc,
|
|
27
|
-
ResponseRequest,
|
|
28
|
-
P
|
|
29
|
-
)
|
|
32
|
+
from .src.types import ResponseRequest
|
|
30
33
|
|
|
31
34
|
# ─────────────────────────── Клиент ───────────────────────────
|
|
32
35
|
|
|
@@ -103,9 +106,25 @@ class HttpClient:
|
|
|
103
106
|
response=response,
|
|
104
107
|
) from exc
|
|
105
108
|
|
|
109
|
+
# ─── Валидация ───
|
|
110
|
+
|
|
111
|
+
def _validate[T](self, response: ResponseRequest, Model: type[T]) -> T | list[T] | None:
|
|
112
|
+
if not response:
|
|
113
|
+
return None
|
|
114
|
+
|
|
115
|
+
if isinstance(response, dict):
|
|
116
|
+
validate_dict = DictValidator(Model)
|
|
117
|
+
return validate_dict(response)
|
|
118
|
+
|
|
119
|
+
if isinstance(response, list):
|
|
120
|
+
validate_list = ListValidator(Model)
|
|
121
|
+
return validate_list(response)
|
|
122
|
+
|
|
123
|
+
return None
|
|
124
|
+
|
|
106
125
|
# ─── Низкоуровневый запрос ───
|
|
107
126
|
|
|
108
|
-
def _request(self, method: str, url: str, **kwargs: Any) ->
|
|
127
|
+
def _request[T](self, method: str, url: str, Model: type[T], **kwargs: Any) -> T | list[T] | None:
|
|
109
128
|
if kwargs.get("stream"):
|
|
110
129
|
raise ValueError(
|
|
111
130
|
"stream=True is not supported here; "
|
|
@@ -115,8 +134,8 @@ class HttpClient:
|
|
|
115
134
|
kwargs.setdefault("timeout", self.timeout)
|
|
116
135
|
|
|
117
136
|
try:
|
|
118
|
-
|
|
119
|
-
|
|
137
|
+
rsp = self.session.request(method, url, **kwargs)
|
|
138
|
+
rsp.raise_for_status()
|
|
120
139
|
except HTTPError as exc:
|
|
121
140
|
r = exc.response
|
|
122
141
|
status = r.status_code if r is not None else None
|
|
@@ -139,57 +158,66 @@ class HttpClient:
|
|
|
139
158
|
f"Request failed {method} {url}: {exc}", url=url
|
|
140
159
|
) from exc
|
|
141
160
|
|
|
142
|
-
|
|
161
|
+
response = self._parse(rsp)
|
|
162
|
+
|
|
163
|
+
return self._validate(response=response, Model=Model)
|
|
143
164
|
|
|
144
165
|
# ─── HTTP-методы ───
|
|
145
166
|
|
|
146
|
-
def get(
|
|
167
|
+
def get[T](
|
|
147
168
|
self,
|
|
148
169
|
url: str,
|
|
170
|
+
Model: type[T],
|
|
149
171
|
params: dict[str, Any] | None = None,
|
|
150
172
|
**kwargs: Any,
|
|
151
|
-
) ->
|
|
152
|
-
return self._request("GET", url, params=params, **kwargs)
|
|
173
|
+
) -> T | list[T] | None:
|
|
174
|
+
return self._request("GET", url, Model=Model, params=params, **kwargs)
|
|
153
175
|
|
|
154
|
-
def post(
|
|
176
|
+
def post[T](
|
|
155
177
|
self,
|
|
156
178
|
url: str,
|
|
179
|
+
Model: type[T],
|
|
157
180
|
*,
|
|
158
181
|
json: Any = None,
|
|
159
182
|
data: Any = None,
|
|
160
183
|
files: Any = None,
|
|
161
184
|
**kwargs: Any,
|
|
162
|
-
) ->
|
|
185
|
+
) -> T | list[T] | None:
|
|
163
186
|
if json is not None and data is not None:
|
|
164
187
|
raise ValueError("Pass either json= or data=, not both")
|
|
165
|
-
return self._request("POST", url, json=json, data=data, files=files, **kwargs)
|
|
188
|
+
return self._request("POST", url, Model=Model, json=json, data=data, files=files, **kwargs)
|
|
166
189
|
|
|
167
|
-
def put(
|
|
190
|
+
def put[T](
|
|
168
191
|
self,
|
|
169
192
|
url: str,
|
|
193
|
+
Model: type[T],
|
|
170
194
|
*,
|
|
171
195
|
json: Any = None,
|
|
172
196
|
data: Any = None,
|
|
173
197
|
**kwargs: Any,
|
|
174
|
-
) ->
|
|
198
|
+
) -> T | list[T] | None:
|
|
175
199
|
if json is not None and data is not None:
|
|
176
200
|
raise ValueError("Pass either json= or data=, not both")
|
|
177
|
-
return self._request("PUT", url, json=json, data=data, **kwargs)
|
|
201
|
+
return self._request("PUT", url, Model=Model, json=json, data=data, **kwargs)
|
|
178
202
|
|
|
179
|
-
def patch(
|
|
203
|
+
def patch[T](
|
|
180
204
|
self,
|
|
181
205
|
url: str,
|
|
206
|
+
Model: type[T],
|
|
182
207
|
*,
|
|
183
208
|
json: Any = None,
|
|
184
209
|
data: Any = None,
|
|
185
210
|
**kwargs: Any,
|
|
186
|
-
) ->
|
|
211
|
+
) -> T | list[T] | None:
|
|
187
212
|
if json is not None and data is not None:
|
|
188
213
|
raise ValueError("Pass either json= or data=, not both")
|
|
189
|
-
return self._request("PATCH", url, json=json, data=data, **kwargs)
|
|
214
|
+
return self._request("PATCH", url, Model=Model, json=json, data=data, **kwargs)
|
|
190
215
|
|
|
191
|
-
def delete(
|
|
192
|
-
|
|
216
|
+
def delete[T](
|
|
217
|
+
self, url: str,
|
|
218
|
+
Model: type[T],
|
|
219
|
+
**kwargs: Any) -> T | list[T] | None:
|
|
220
|
+
return self._request("DELETE", url, Model=Model, **kwargs)
|
|
193
221
|
|
|
194
222
|
# ─── Проверка доступности ───
|
|
195
223
|
|
|
@@ -216,18 +244,14 @@ class HttpClient:
|
|
|
216
244
|
self,
|
|
217
245
|
url: str,
|
|
218
246
|
filein: Path,
|
|
219
|
-
|
|
220
|
-
|
|
247
|
+
key: str | None = None,
|
|
248
|
+
meta: dict | None = None,
|
|
221
249
|
) -> bool:
|
|
222
250
|
"""Загружает файл. Бросает HttpClientError при ошибке."""
|
|
223
251
|
try:
|
|
224
252
|
with filein.open("rb") as fr:
|
|
225
253
|
files = {"file": (filein.name, fr, "application/octet-stream")}
|
|
226
|
-
data = {
|
|
227
|
-
meta_field: orjson.dumps(
|
|
228
|
-
{"filename": filein.name, "tags": tags}
|
|
229
|
-
)
|
|
230
|
-
}
|
|
254
|
+
data = {key: orjson.dumps(meta)} if key else None
|
|
231
255
|
rsp = self.session.post(
|
|
232
256
|
url,
|
|
233
257
|
files=files,
|
|
@@ -261,7 +285,7 @@ class HttpClient:
|
|
|
261
285
|
def download_file(
|
|
262
286
|
self, url: str,
|
|
263
287
|
fileout: Path
|
|
264
|
-
|
|
288
|
+
) -> bool:
|
|
265
289
|
fileout.parent.mkdir(parents=True, exist_ok=True)
|
|
266
290
|
try:
|
|
267
291
|
with self.session.get(url, stream=True, timeout=self.upload_timeout) as rsp:
|
|
@@ -294,43 +318,44 @@ class HttpClient:
|
|
|
294
318
|
|
|
295
319
|
return True
|
|
296
320
|
|
|
297
|
-
def http_client(
|
|
298
|
-
|
|
299
|
-
) -> Callable[P,
|
|
321
|
+
def http_client[**P, R](
|
|
322
|
+
func: Callable[Concatenate[HttpClient, P], R]
|
|
323
|
+
) -> Callable[P, R]:
|
|
300
324
|
@wraps(func)
|
|
301
|
-
def wrapper(*args: P.args, **kwargs: P.kwargs) ->
|
|
325
|
+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
|
|
302
326
|
with HttpClient() as client:
|
|
303
|
-
# kwargs.setdefault('client', client)
|
|
304
327
|
return func(client, *args, **kwargs)
|
|
305
328
|
return wrapper
|
|
306
329
|
|
|
307
330
|
@http_client
|
|
308
|
-
def http_get(client: HttpClient, url: str, data: dict | None = None) ->
|
|
309
|
-
return client.get(url, data)
|
|
331
|
+
def http_get[T](client: HttpClient, url: str, Model: type[T], data: dict | None = None) -> T | list[T] | None:
|
|
332
|
+
return client.get(url, Model, data)
|
|
310
333
|
|
|
311
334
|
@http_client
|
|
312
|
-
def http_post(client: HttpClient, url: str, json: dict | None = None) ->
|
|
313
|
-
return client.post(url, json=json)
|
|
335
|
+
def http_post[T](client: HttpClient, url: str, Model: type[T], json: dict | None = None) -> T | list[T] | None:
|
|
336
|
+
return client.post(url, Model, json=json)
|
|
314
337
|
|
|
315
338
|
|
|
316
339
|
# USE CASE
|
|
317
340
|
# from typing import Any
|
|
318
341
|
# from request.http_client import HttpClient, http_client, http_get
|
|
319
342
|
|
|
343
|
+
# from dataclasses import dataclass
|
|
344
|
+
# from functools import partial
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
# @dataclass
|
|
348
|
+
# class User:
|
|
349
|
+
# id: int
|
|
350
|
+
# name: str
|
|
351
|
+
|
|
320
352
|
# @http_client
|
|
321
353
|
# def fetch_posts(client: HttpClient, *args: Any, **kwargs: Any):
|
|
322
|
-
# respo = client.get("https://jsonplaceholder.typicode.com/users")
|
|
354
|
+
# respo = client.get("https://jsonplaceholder.typicode.com/users", User)
|
|
323
355
|
# print(respo)
|
|
324
356
|
# respo = client.get("https://jsonplaceholder.typicode.com/posts")
|
|
325
357
|
# print(respo)
|
|
326
358
|
|
|
327
|
-
# def main():
|
|
328
|
-
|
|
329
|
-
# # respo = http_get("https://jsonplaceholder.typicode.com//users")
|
|
330
|
-
# # print(respo)
|
|
331
|
-
|
|
332
|
-
# respo = http_get("https://jsonplaceholder.typicode.com/posts2")
|
|
333
|
-
# print(respo)
|
|
334
359
|
|
|
335
360
|
# if __name__ == "__main__":
|
|
336
|
-
#
|
|
361
|
+
# fetch_posts()
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from functools import partial
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
import msgspec
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _ContainerValidator[T, I, O]:
|
|
10
|
+
"""Общая часть для валидаторов контейнеров.
|
|
11
|
+
|
|
12
|
+
- T — модель элемента;
|
|
13
|
+
- I — ожидаемый входной контейнер (dict / list / ...);
|
|
14
|
+
- O — тип результата (T для dict, list[T] для list, ...).
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
__slots__ = ("_convert", "_model", "_strict")
|
|
18
|
+
|
|
19
|
+
_expected_type: type # переопределяется в наследниках
|
|
20
|
+
|
|
21
|
+
def __init__(self, Model: type[T], *, strict: bool = False) -> None:
|
|
22
|
+
self._model = Model
|
|
23
|
+
self._convert = partial(msgspec.convert, type=self._resolve_target(Model))
|
|
24
|
+
self._strict = strict
|
|
25
|
+
|
|
26
|
+
@staticmethod
|
|
27
|
+
def _resolve_target(Model: type[T]) -> Any:
|
|
28
|
+
raise NotImplementedError
|
|
29
|
+
|
|
30
|
+
def __call__(
|
|
31
|
+
self,
|
|
32
|
+
response: I | None,
|
|
33
|
+
) -> O | None:
|
|
34
|
+
if response is None:
|
|
35
|
+
return None
|
|
36
|
+
|
|
37
|
+
if not isinstance(response, self._expected_type):
|
|
38
|
+
raise TypeError(
|
|
39
|
+
f"{type(self).__name__}: ожидался "
|
|
40
|
+
f"{self._expected_type.__name__} для {self._model.__name__}, "
|
|
41
|
+
f"получен {type(response).__name__}"
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
return self._convert(response, strict=self._strict)
|
|
46
|
+
except msgspec.ValidationError as e:
|
|
47
|
+
raise TypeError(
|
|
48
|
+
"Валидация %s[%s] не прошла: %s",
|
|
49
|
+
type(self).__name__,
|
|
50
|
+
self._model.__name__,
|
|
51
|
+
str(e), # уже содержит путь: Expected `int`, got `str` - at `$[2].id`
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class DictValidator[T](_ContainerValidator[T, dict, T]):
|
|
56
|
+
"""Валидатор dict → T.
|
|
57
|
+
|
|
58
|
+
Аналог pydantic.TypeAdapter для msgspec.convert.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
__slots__ = ()
|
|
62
|
+
_expected_type = dict
|
|
63
|
+
|
|
64
|
+
@staticmethod
|
|
65
|
+
def _resolve_target(Model: type[T]) -> Any:
|
|
66
|
+
return Model
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class ListValidator[T](_ContainerValidator[T, list, list[T]]):
|
|
70
|
+
"""Валидатор list → list[T].
|
|
71
|
+
|
|
72
|
+
Аналог pydantic.TypeAdapter(list[T]) для msgspec.convert.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
__slots__ = ()
|
|
76
|
+
_expected_type = list
|
|
77
|
+
|
|
78
|
+
@staticmethod
|
|
79
|
+
def _resolve_target(Model: type[T]) -> Any:
|
|
80
|
+
return list[Model]
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
# ─────────────────────────── Конфиг ───────────────────────────
|
|
2
2
|
|
|
3
|
+
from typing import ClassVar
|
|
4
|
+
|
|
5
|
+
|
|
3
6
|
class Config:
|
|
4
7
|
API_TIMEOUT: tuple[float, float] = (3.0, 60.0)
|
|
5
8
|
API_UPLOAD_TIMEOUT: tuple[float, float] = (10.0, 600.0)
|
|
6
|
-
API_HEADERS: dict[str, str] = {
|
|
9
|
+
API_HEADERS: ClassVar[dict[str, str]] = {
|
|
7
10
|
"Cache-Control": "no-cache",
|
|
8
11
|
"Accept": "*/*",
|
|
9
12
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# ─────────────────────────── Типы ───────────────────────────
|
|
2
|
+
|
|
3
|
+
type JsonDict = dict[str, bool | int | str | list | dict]
|
|
4
|
+
type JsonList = list[JsonDict | str | int | float | bool]
|
|
5
|
+
type JsonValue = bool | int | float | str | bytes | None | dict | list
|
|
6
|
+
type ResponseRequest = JsonValue
|
|
7
|
+
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
from typing import ParamSpec, TypeAlias
|
|
2
|
-
|
|
3
|
-
# ─────────────────────────── Типы ───────────────────────────
|
|
4
|
-
|
|
5
|
-
JsonDict: TypeAlias = dict[str, bool | int | str | list | dict]
|
|
6
|
-
JsonList: TypeAlias = list[JsonDict | str | int | float | bool]
|
|
7
|
-
JsonValue: TypeAlias = bool | int | float | str | bytes | None | dict | list
|
|
8
|
-
ResponseRequest: TypeAlias = JsonValue
|
|
9
|
-
|
|
10
|
-
ResponseFunc: TypeAlias = JsonValue
|
|
11
|
-
|
|
12
|
-
P = ParamSpec("P")
|
|
File without changes
|