reqmachine 0.1.0__tar.gz → 0.1.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.
- {reqmachine-0.1.0 → reqmachine-0.1.2}/PKG-INFO +97 -87
- {reqmachine-0.1.0 → reqmachine-0.1.2}/README.md +95 -86
- {reqmachine-0.1.0 → reqmachine-0.1.2}/pyproject.toml +3 -2
- {reqmachine-0.1.0 → reqmachine-0.1.2}/pyproject.toml.orig +4 -3
- {reqmachine-0.1.0 → reqmachine-0.1.2}/reqmachine/__init__.py +4 -4
- {reqmachine-0.1.0 → reqmachine-0.1.2}/reqmachine/__main__.py +0 -3
- reqmachine-0.1.0/reqmachine/http_client.py → reqmachine-0.1.2/reqmachine/client.py +78 -53
- reqmachine-0.1.2/reqmachine/src/adapter.py +80 -0
- {reqmachine-0.1.0/reqmachine → reqmachine-0.1.2/reqmachine/src}/config.py +4 -1
- reqmachine-0.1.2/reqmachine/src/types.py +7 -0
- reqmachine-0.1.0/reqmachine/types.py +0 -12
- {reqmachine-0.1.0/reqmachine → reqmachine-0.1.2/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.2
|
|
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
|
|
@@ -33,9 +34,6 @@ upload/download helpers.
|
|
|
33
34
|
|
|
34
35
|
Requires Python **3.14+**.
|
|
35
36
|
|
|
36
|
-
> [!CAUTION]
|
|
37
|
-
> `reqmachine` is a Work In Progress (WIP)
|
|
38
|
-
|
|
39
37
|
## Install
|
|
40
38
|
|
|
41
39
|
```bash
|
|
@@ -46,105 +44,87 @@ pip install reqmachine
|
|
|
46
44
|
|
|
47
45
|
## Usage
|
|
48
46
|
|
|
49
|
-
|
|
47
|
+
`@http_client` injects a fresh `HttpClient` as the first argument.
|
|
48
|
+
`get/post/put/patch/delete` accept a type and return parsed data
|
|
49
|
+
(`None` for an empty body), raising `HttpClientError` subclasses on
|
|
50
|
+
failure. `http_get` / `http_post` are one-shot helpers.
|
|
50
51
|
|
|
51
52
|
```python
|
|
52
|
-
from
|
|
53
|
-
|
|
54
|
-
with HttpClient() as client:
|
|
55
|
-
users = client.get("https://jsonplaceholder.typicode.com/users")
|
|
56
|
-
posts = client.get("https://jsonplaceholder.typicode.com/posts")
|
|
57
|
-
```
|
|
53
|
+
from dataclasses import dataclass
|
|
54
|
+
from reqmachine import HttpClient, http_client
|
|
58
55
|
|
|
59
|
-
`get/post/put/patch/delete` return parsed JSON (`None` for an empty
|
|
60
|
-
response body) and raise `HttpClientError` subclasses on failure.
|
|
61
56
|
|
|
62
|
-
|
|
57
|
+
@dataclass
|
|
58
|
+
class Geo:
|
|
59
|
+
lat: str
|
|
60
|
+
lng: str
|
|
63
61
|
|
|
64
|
-
```python
|
|
65
|
-
from reqmachine import HttpClient, http_client, http_get
|
|
66
62
|
|
|
67
|
-
@
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
@dataclass
|
|
64
|
+
class Address:
|
|
65
|
+
street: str
|
|
66
|
+
suite: str
|
|
67
|
+
city: str
|
|
68
|
+
zipcode: str
|
|
69
|
+
geo: Geo
|
|
71
70
|
|
|
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
71
|
|
|
75
|
-
|
|
72
|
+
@dataclass
|
|
73
|
+
class Company:
|
|
74
|
+
name: str
|
|
75
|
+
catchPhrase: str # имя как в JSON (camelCase — валидный идентификатор Python)
|
|
76
|
+
bs: str
|
|
76
77
|
|
|
77
|
-
```python
|
|
78
|
-
from reqmachine import HttpClient
|
|
79
78
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
@dataclass
|
|
80
|
+
class User:
|
|
81
|
+
id: int
|
|
82
|
+
name: str
|
|
83
|
+
username: str
|
|
84
|
+
email: str
|
|
85
|
+
address: Address
|
|
86
|
+
phone: str
|
|
87
|
+
website: str
|
|
88
|
+
company: Company
|
|
87
89
|
|
|
88
|
-
Retries apply to `HEAD/GET/PUT/DELETE/OPTIONS/TRACE` on statuses
|
|
89
|
-
`429, 500, 502, 503, 504` (see `Config`).
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
@dataclass
|
|
92
|
+
class Post:
|
|
93
|
+
id: int
|
|
94
|
+
userId: int
|
|
95
|
+
title: str
|
|
96
|
+
body: str
|
|
92
97
|
|
|
93
|
-
```python
|
|
94
|
-
from pathlib import Path
|
|
95
|
-
from reqmachine import HttpClient
|
|
96
98
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
client.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
(`JSONData` by default) and uses a longer timeout.
|
|
104
|
-
|
|
105
|
-
## Error types
|
|
99
|
+
@http_client
|
|
100
|
+
def fetch_posts(client: HttpClient):
|
|
101
|
+
users = client.get("https://jsonplaceholder.typicode.com/users", User)
|
|
102
|
+
if isinstance(users, list):
|
|
103
|
+
for it in users:
|
|
104
|
+
print(it.company)
|
|
106
105
|
|
|
107
|
-
|
|
108
|
-
|
|
106
|
+
posts = client.get("https://jsonplaceholder.typicode.com/posts", Post)
|
|
107
|
+
if isinstance(posts, list):
|
|
108
|
+
for it in posts:
|
|
109
|
+
print(it.title)
|
|
109
110
|
|
|
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
111
|
|
|
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")
|
|
112
|
+
if __name__ == "__main__":
|
|
113
|
+
fetch_posts()
|
|
128
114
|
```
|
|
129
115
|
|
|
130
|
-
|
|
131
|
-
`True` on success. `is_url_available()` never raises — it returns
|
|
132
|
-
`False` on any network error.
|
|
133
|
-
|
|
134
|
-
## Development
|
|
135
|
-
|
|
136
|
-
```bash
|
|
137
|
-
uv sync
|
|
138
|
-
uv run ruff check .
|
|
139
|
-
uv run pytest
|
|
140
|
-
```
|
|
116
|
+
## Tuning
|
|
141
117
|
|
|
142
|
-
|
|
118
|
+
```python
|
|
119
|
+
from reqmachine import HttpClient
|
|
143
120
|
|
|
144
|
-
|
|
145
|
-
|
|
121
|
+
client = HttpClient(
|
|
122
|
+
verify=True, # TLS verification (default: off)
|
|
123
|
+
timeout=(3.0, 60.0), # (connect, read)
|
|
124
|
+
retries=3, # urllib3 Retry total
|
|
125
|
+
headers={"Authorization": "Bearer <token>"},
|
|
126
|
+
)
|
|
146
127
|
```
|
|
147
|
-
|
|
148
128
|
### Test service (Django)
|
|
149
129
|
|
|
150
130
|
`service/` contains a small Django app that implements every endpoint
|
|
@@ -172,18 +152,48 @@ uv run python service/manage.py runserver 8000
|
|
|
172
152
|
|
|
173
153
|
Example with the client:
|
|
174
154
|
|
|
155
|
+
|
|
156
|
+
## Files
|
|
157
|
+
|
|
175
158
|
```python
|
|
159
|
+
from pathlib import Path
|
|
176
160
|
from reqmachine import HttpClient
|
|
177
161
|
|
|
178
|
-
with HttpClient(
|
|
179
|
-
|
|
180
|
-
client.
|
|
181
|
-
client.download_file("http://127.0.0.1:8000/api/payload/", out)
|
|
162
|
+
with HttpClient() as client:
|
|
163
|
+
client.upload_file("https://example.com/upload", Path("data.bin"))
|
|
164
|
+
client.download_file("https://example.com/file", Path("out/file.bin"))
|
|
182
165
|
```
|
|
183
166
|
|
|
184
|
-
|
|
167
|
+
`upload_file` sends the file as multipart with a JSON metadata field
|
|
168
|
+
(`JSONData` by default). Both return `True` on success.
|
|
169
|
+
|
|
170
|
+
## Errors
|
|
171
|
+
|
|
172
|
+
All exceptions derive from `HttpClientError`:
|
|
173
|
+
|
|
174
|
+
| Exception | When |
|
|
175
|
+
|---|---|
|
|
176
|
+
| `HttpStatusError` | Status is not 2xx/3xx (`.status`, `.response`) |
|
|
177
|
+
| `HttpConnectionError` | Connection could not be established |
|
|
178
|
+
| `HttpTimeoutError` | Connect/read timeout |
|
|
179
|
+
| `HttpDecodeError` | Body is not valid JSON |
|
|
180
|
+
|
|
181
|
+
`is_url_available()` never raises — returns `False` on any network error.
|
|
182
|
+
|
|
183
|
+
## Development
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
uv sync
|
|
187
|
+
uv run ruff check .
|
|
188
|
+
uv run pytest
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Local Django test service (`service/`, serves every endpoint scenario):
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
uv run python service/manage.py runserver 8000
|
|
195
|
+
```
|
|
185
196
|
|
|
186
197
|
## License
|
|
187
198
|
|
|
188
199
|
MIT — see [LICENSE](LICENSE).
|
|
189
|
-
|
|
@@ -6,9 +6,6 @@ upload/download helpers.
|
|
|
6
6
|
|
|
7
7
|
Requires Python **3.14+**.
|
|
8
8
|
|
|
9
|
-
> [!CAUTION]
|
|
10
|
-
> `reqmachine` is a Work In Progress (WIP)
|
|
11
|
-
|
|
12
9
|
## Install
|
|
13
10
|
|
|
14
11
|
```bash
|
|
@@ -19,105 +16,87 @@ pip install reqmachine
|
|
|
19
16
|
|
|
20
17
|
## Usage
|
|
21
18
|
|
|
22
|
-
|
|
19
|
+
`@http_client` injects a fresh `HttpClient` as the first argument.
|
|
20
|
+
`get/post/put/patch/delete` accept a type and return parsed data
|
|
21
|
+
(`None` for an empty body), raising `HttpClientError` subclasses on
|
|
22
|
+
failure. `http_get` / `http_post` are one-shot helpers.
|
|
23
23
|
|
|
24
24
|
```python
|
|
25
|
-
from
|
|
26
|
-
|
|
27
|
-
with HttpClient() as client:
|
|
28
|
-
users = client.get("https://jsonplaceholder.typicode.com/users")
|
|
29
|
-
posts = client.get("https://jsonplaceholder.typicode.com/posts")
|
|
30
|
-
```
|
|
25
|
+
from dataclasses import dataclass
|
|
26
|
+
from reqmachine import HttpClient, http_client
|
|
31
27
|
|
|
32
|
-
`get/post/put/patch/delete` return parsed JSON (`None` for an empty
|
|
33
|
-
response body) and raise `HttpClientError` subclasses on failure.
|
|
34
28
|
|
|
35
|
-
|
|
29
|
+
@dataclass
|
|
30
|
+
class Geo:
|
|
31
|
+
lat: str
|
|
32
|
+
lng: str
|
|
36
33
|
|
|
37
|
-
```python
|
|
38
|
-
from reqmachine import HttpClient, http_client, http_get
|
|
39
34
|
|
|
40
|
-
@
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
35
|
+
@dataclass
|
|
36
|
+
class Address:
|
|
37
|
+
street: str
|
|
38
|
+
suite: str
|
|
39
|
+
city: str
|
|
40
|
+
zipcode: str
|
|
41
|
+
geo: Geo
|
|
44
42
|
|
|
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
43
|
|
|
48
|
-
|
|
44
|
+
@dataclass
|
|
45
|
+
class Company:
|
|
46
|
+
name: str
|
|
47
|
+
catchPhrase: str # имя как в JSON (camelCase — валидный идентификатор Python)
|
|
48
|
+
bs: str
|
|
49
49
|
|
|
50
|
-
```python
|
|
51
|
-
from reqmachine import HttpClient
|
|
52
50
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
@dataclass
|
|
52
|
+
class User:
|
|
53
|
+
id: int
|
|
54
|
+
name: str
|
|
55
|
+
username: str
|
|
56
|
+
email: str
|
|
57
|
+
address: Address
|
|
58
|
+
phone: str
|
|
59
|
+
website: str
|
|
60
|
+
company: Company
|
|
60
61
|
|
|
61
|
-
Retries apply to `HEAD/GET/PUT/DELETE/OPTIONS/TRACE` on statuses
|
|
62
|
-
`429, 500, 502, 503, 504` (see `Config`).
|
|
63
62
|
|
|
64
|
-
|
|
63
|
+
@dataclass
|
|
64
|
+
class Post:
|
|
65
|
+
id: int
|
|
66
|
+
userId: int
|
|
67
|
+
title: str
|
|
68
|
+
body: str
|
|
65
69
|
|
|
66
|
-
```python
|
|
67
|
-
from pathlib import Path
|
|
68
|
-
from reqmachine import HttpClient
|
|
69
70
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
client.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
(`JSONData` by default) and uses a longer timeout.
|
|
77
|
-
|
|
78
|
-
## Error types
|
|
71
|
+
@http_client
|
|
72
|
+
def fetch_posts(client: HttpClient):
|
|
73
|
+
users = client.get("https://jsonplaceholder.typicode.com/users", User)
|
|
74
|
+
if isinstance(users, list):
|
|
75
|
+
for it in users:
|
|
76
|
+
print(it.company)
|
|
79
77
|
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
posts = client.get("https://jsonplaceholder.typicode.com/posts", Post)
|
|
79
|
+
if isinstance(posts, list):
|
|
80
|
+
for it in posts:
|
|
81
|
+
print(it.title)
|
|
82
82
|
|
|
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
83
|
|
|
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")
|
|
84
|
+
if __name__ == "__main__":
|
|
85
|
+
fetch_posts()
|
|
101
86
|
```
|
|
102
87
|
|
|
103
|
-
|
|
104
|
-
`True` on success. `is_url_available()` never raises — it returns
|
|
105
|
-
`False` on any network error.
|
|
106
|
-
|
|
107
|
-
## Development
|
|
108
|
-
|
|
109
|
-
```bash
|
|
110
|
-
uv sync
|
|
111
|
-
uv run ruff check .
|
|
112
|
-
uv run pytest
|
|
113
|
-
```
|
|
88
|
+
## Tuning
|
|
114
89
|
|
|
115
|
-
|
|
90
|
+
```python
|
|
91
|
+
from reqmachine import HttpClient
|
|
116
92
|
|
|
117
|
-
|
|
118
|
-
|
|
93
|
+
client = HttpClient(
|
|
94
|
+
verify=True, # TLS verification (default: off)
|
|
95
|
+
timeout=(3.0, 60.0), # (connect, read)
|
|
96
|
+
retries=3, # urllib3 Retry total
|
|
97
|
+
headers={"Authorization": "Bearer <token>"},
|
|
98
|
+
)
|
|
119
99
|
```
|
|
120
|
-
|
|
121
100
|
### Test service (Django)
|
|
122
101
|
|
|
123
102
|
`service/` contains a small Django app that implements every endpoint
|
|
@@ -145,18 +124,48 @@ uv run python service/manage.py runserver 8000
|
|
|
145
124
|
|
|
146
125
|
Example with the client:
|
|
147
126
|
|
|
127
|
+
|
|
128
|
+
## Files
|
|
129
|
+
|
|
148
130
|
```python
|
|
131
|
+
from pathlib import Path
|
|
149
132
|
from reqmachine import HttpClient
|
|
150
133
|
|
|
151
|
-
with HttpClient(
|
|
152
|
-
|
|
153
|
-
client.
|
|
154
|
-
client.download_file("http://127.0.0.1:8000/api/payload/", out)
|
|
134
|
+
with HttpClient() as client:
|
|
135
|
+
client.upload_file("https://example.com/upload", Path("data.bin"))
|
|
136
|
+
client.download_file("https://example.com/file", Path("out/file.bin"))
|
|
155
137
|
```
|
|
156
138
|
|
|
157
|
-
|
|
139
|
+
`upload_file` sends the file as multipart with a JSON metadata field
|
|
140
|
+
(`JSONData` by default). Both return `True` on success.
|
|
141
|
+
|
|
142
|
+
## Errors
|
|
143
|
+
|
|
144
|
+
All exceptions derive from `HttpClientError`:
|
|
145
|
+
|
|
146
|
+
| Exception | When |
|
|
147
|
+
|---|---|
|
|
148
|
+
| `HttpStatusError` | Status is not 2xx/3xx (`.status`, `.response`) |
|
|
149
|
+
| `HttpConnectionError` | Connection could not be established |
|
|
150
|
+
| `HttpTimeoutError` | Connect/read timeout |
|
|
151
|
+
| `HttpDecodeError` | Body is not valid JSON |
|
|
152
|
+
|
|
153
|
+
`is_url_available()` never raises — returns `False` on any network error.
|
|
154
|
+
|
|
155
|
+
## Development
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
uv sync
|
|
159
|
+
uv run ruff check .
|
|
160
|
+
uv run pytest
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Local Django test service (`service/`, serves every endpoint scenario):
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
uv run python service/manage.py runserver 8000
|
|
167
|
+
```
|
|
158
168
|
|
|
159
169
|
## License
|
|
160
170
|
|
|
161
171
|
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.2"
|
|
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.2"
|
|
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
|