reqmachine 0.1.0__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.
@@ -0,0 +1,189 @@
1
+ Metadata-Version: 2.4
2
+ Name: reqmachine
3
+ Version: 0.1.0
4
+ Summary: A small requests-based HTTP client with retries, JSON parsing and file upload/download helpers.
5
+ Author: aligeromachine
6
+ Author-email: aligeromachine <aligeromachine@github.com>
7
+ License-Expression: MIT
8
+ Classifier: Development Status :: 4 - Beta
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.14
13
+ Classifier: Topic :: Internet :: WWW/HTTP
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Classifier: Typing :: Typed
16
+ Requires-Dist: orjson>=3.11.9
17
+ Requires-Dist: requests>=2.34.2
18
+ Requires-Dist: urllib3>=2.5.0
19
+ Maintainer: aligeromachine
20
+ Maintainer-email: aligeromachine <aligeromachine@github.com>
21
+ Requires-Python: >=3.14
22
+ Project-URL: Homepage, https://reqmachine.org/
23
+ Project-URL: Documentation, https://github.com/aligeromachine/reqmachine/docs
24
+ Project-URL: Repository, https://github.com/aligeromachine/reqmachine
25
+ Project-URL: Changelog, https://github.com/aligeromachine/reqmachine/blob/main/CHANGES.md
26
+ Description-Content-Type: text/markdown
27
+
28
+ # Welcome to **`reqmachine`**
29
+
30
+ A small [requests](https://requests.readthedocs.io/)-based HTTP client with
31
+ retries, JSON parsing ([orjson](https://github.com/ijl/orjson)) and file
32
+ upload/download helpers.
33
+
34
+ Requires Python **3.14+**.
35
+
36
+ > [!CAUTION]
37
+ > `reqmachine` is a Work In Progress (WIP)
38
+
39
+ ## Install
40
+
41
+ ```bash
42
+ uv add reqmachine
43
+ # or
44
+ pip install reqmachine
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ### Context manager
50
+
51
+ ```python
52
+ from reqmachine import HttpClient
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
+ ```
58
+
59
+ `get/post/put/patch/delete` return parsed JSON (`None` for an empty
60
+ response body) and raise `HttpClientError` subclasses on failure.
61
+
62
+ ### Decorators
63
+
64
+ ```python
65
+ from reqmachine import HttpClient, http_client, http_get
66
+
67
+ @http_client
68
+ def fetch_users(client: HttpClient):
69
+ return client.get("https://jsonplaceholder.typicode.com/users")
70
+ ```
71
+
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
+ ### Tuning
76
+
77
+ ```python
78
+ from reqmachine import HttpClient
79
+
80
+ client = HttpClient(
81
+ verify=True, # TLS verification (default: off)
82
+ timeout=(3.0, 60.0), # (connect, read)
83
+ retries=3, # urllib3 Retry total
84
+ headers={"Authorization": "Bearer <token>"},
85
+ )
86
+ ```
87
+
88
+ Retries apply to `HEAD/GET/PUT/DELETE/OPTIONS/TRACE` on statuses
89
+ `429, 500, 502, 503, 504` (see `Config`).
90
+
91
+ ### Files
92
+
93
+ ```python
94
+ from pathlib import Path
95
+ from reqmachine import HttpClient
96
+
97
+ with HttpClient() as client:
98
+ client.upload_file("https://example.com/upload", Path("data.bin"), tags=["x"])
99
+ client.download_file("https://example.com/file", Path("out/file.bin"))
100
+ ```
101
+
102
+ `upload_file` sends the file as multipart with a JSON metadata field
103
+ (`JSONData` by default) and uses a longer timeout.
104
+
105
+ ## Error types
106
+
107
+ Errors are raised, not swallowed — all of them derive from
108
+ `HttpClientError`:
109
+
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
+
118
+ ```python
119
+ from reqmachine import HttpClient, HttpTimeoutError, HttpStatusError
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")
128
+ ```
129
+
130
+ `upload_file` / `download_file` raise the same exceptions and return
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
+ ```
141
+
142
+ Быстрая проверка проброса ошибок без pytest/responses/сети:
143
+
144
+ ```bash
145
+ python3 scripts/smoke_offline.py
146
+ ```
147
+
148
+ ### Test service (Django)
149
+
150
+ `service/` contains a small Django app that implements every endpoint
151
+ scenario the client tests exercise — useful for running the client
152
+ against a real local server:
153
+
154
+ ```bash
155
+ uv run python service/manage.py runserver 8000
156
+ ```
157
+
158
+ | Endpoint | Scenario |
159
+ |---|---|
160
+ | `GET /api/json/` | 200 + JSON |
161
+ | `GET /api/empty/` | 204, empty body → client returns `None` |
162
+ | `GET /api/html/` | non-JSON body → `HttpDecodeError` |
163
+ | `GET /api/invalid-json/` | broken JSON → `HttpDecodeError` |
164
+ | `GET /api/error/` | 500 + JSON → `HttpStatusError` |
165
+ | `/api/echo/` | GET/POST/PUT/PATCH/DELETE echo (POST → 201) |
166
+ | `GET,HEAD /api/ping/` | availability probe (200) |
167
+ | `ANY /api/redirect/` | 302 → `/api/ping/` |
168
+ | `GET /api/head-only/` | 405 on HEAD, 200 on GET |
169
+ | `POST /api/upload/` | multipart: `file` + `JSONData` metadata |
170
+ | `GET /api/download/<name>/` | streaming download of an uploaded file |
171
+ | `GET /api/payload/` | fixed 1 KiB binary payload |
172
+
173
+ Example with the client:
174
+
175
+ ```python
176
+ from reqmachine import HttpClient
177
+
178
+ with HttpClient(verify=False) as client:
179
+ print(client.get("http://127.0.0.1:8000/api/json/"))
180
+ client.upload_file("http://127.0.0.1:8000/api/upload/", path, tags=["demo"])
181
+ client.download_file("http://127.0.0.1:8000/api/payload/", out)
182
+ ```
183
+
184
+ Service tests: `uv run python service/manage.py test api`
185
+
186
+ ## License
187
+
188
+ MIT — see [LICENSE](LICENSE).
189
+
@@ -0,0 +1,162 @@
1
+ # Welcome to **`reqmachine`**
2
+
3
+ A small [requests](https://requests.readthedocs.io/)-based HTTP client with
4
+ retries, JSON parsing ([orjson](https://github.com/ijl/orjson)) and file
5
+ upload/download helpers.
6
+
7
+ Requires Python **3.14+**.
8
+
9
+ > [!CAUTION]
10
+ > `reqmachine` is a Work In Progress (WIP)
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ uv add reqmachine
16
+ # or
17
+ pip install reqmachine
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### Context manager
23
+
24
+ ```python
25
+ from reqmachine import HttpClient
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
+ ```
31
+
32
+ `get/post/put/patch/delete` return parsed JSON (`None` for an empty
33
+ response body) and raise `HttpClientError` subclasses on failure.
34
+
35
+ ### Decorators
36
+
37
+ ```python
38
+ from reqmachine import HttpClient, http_client, http_get
39
+
40
+ @http_client
41
+ def fetch_users(client: HttpClient):
42
+ return client.get("https://jsonplaceholder.typicode.com/users")
43
+ ```
44
+
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
+
48
+ ### Tuning
49
+
50
+ ```python
51
+ from reqmachine import HttpClient
52
+
53
+ client = HttpClient(
54
+ verify=True, # TLS verification (default: off)
55
+ timeout=(3.0, 60.0), # (connect, read)
56
+ retries=3, # urllib3 Retry total
57
+ headers={"Authorization": "Bearer <token>"},
58
+ )
59
+ ```
60
+
61
+ Retries apply to `HEAD/GET/PUT/DELETE/OPTIONS/TRACE` on statuses
62
+ `429, 500, 502, 503, 504` (see `Config`).
63
+
64
+ ### Files
65
+
66
+ ```python
67
+ from pathlib import Path
68
+ from reqmachine import HttpClient
69
+
70
+ with HttpClient() as client:
71
+ client.upload_file("https://example.com/upload", Path("data.bin"), tags=["x"])
72
+ client.download_file("https://example.com/file", Path("out/file.bin"))
73
+ ```
74
+
75
+ `upload_file` sends the file as multipart with a JSON metadata field
76
+ (`JSONData` by default) and uses a longer timeout.
77
+
78
+ ## Error types
79
+
80
+ Errors are raised, not swallowed — all of them derive from
81
+ `HttpClientError`:
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
+
91
+ ```python
92
+ from reqmachine import HttpClient, HttpTimeoutError, HttpStatusError
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")
101
+ ```
102
+
103
+ `upload_file` / `download_file` raise the same exceptions and return
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
+ ```
114
+
115
+ Быстрая проверка проброса ошибок без pytest/responses/сети:
116
+
117
+ ```bash
118
+ python3 scripts/smoke_offline.py
119
+ ```
120
+
121
+ ### Test service (Django)
122
+
123
+ `service/` contains a small Django app that implements every endpoint
124
+ scenario the client tests exercise — useful for running the client
125
+ against a real local server:
126
+
127
+ ```bash
128
+ uv run python service/manage.py runserver 8000
129
+ ```
130
+
131
+ | Endpoint | Scenario |
132
+ |---|---|
133
+ | `GET /api/json/` | 200 + JSON |
134
+ | `GET /api/empty/` | 204, empty body → client returns `None` |
135
+ | `GET /api/html/` | non-JSON body → `HttpDecodeError` |
136
+ | `GET /api/invalid-json/` | broken JSON → `HttpDecodeError` |
137
+ | `GET /api/error/` | 500 + JSON → `HttpStatusError` |
138
+ | `/api/echo/` | GET/POST/PUT/PATCH/DELETE echo (POST → 201) |
139
+ | `GET,HEAD /api/ping/` | availability probe (200) |
140
+ | `ANY /api/redirect/` | 302 → `/api/ping/` |
141
+ | `GET /api/head-only/` | 405 on HEAD, 200 on GET |
142
+ | `POST /api/upload/` | multipart: `file` + `JSONData` metadata |
143
+ | `GET /api/download/<name>/` | streaming download of an uploaded file |
144
+ | `GET /api/payload/` | fixed 1 KiB binary payload |
145
+
146
+ Example with the client:
147
+
148
+ ```python
149
+ from reqmachine import HttpClient
150
+
151
+ with HttpClient(verify=False) as client:
152
+ print(client.get("http://127.0.0.1:8000/api/json/"))
153
+ client.upload_file("http://127.0.0.1:8000/api/upload/", path, tags=["demo"])
154
+ client.download_file("http://127.0.0.1:8000/api/payload/", out)
155
+ ```
156
+
157
+ Service tests: `uv run python service/manage.py test api`
158
+
159
+ ## License
160
+
161
+ MIT — see [LICENSE](LICENSE).
162
+
@@ -0,0 +1,55 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.12.0,<0.13.0"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "reqmachine"
7
+ version = "0.1.0"
8
+ description = "A small requests-based HTTP client with retries, JSON parsing and file upload/download helpers."
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ requires-python = ">=3.14"
12
+ classifiers = [
13
+ "Development Status :: 4 - Beta",
14
+ "Intended Audience :: Developers",
15
+ "Operating System :: OS Independent",
16
+ "Programming Language :: Python :: 3",
17
+ "Programming Language :: Python :: 3.14",
18
+ "Topic :: Internet :: WWW/HTTP",
19
+ "Topic :: Software Development :: Libraries :: Python Modules",
20
+ "Typing :: Typed",
21
+ ]
22
+ dependencies = [
23
+ "orjson>=3.11.9",
24
+ "requests>=2.34.2",
25
+ "urllib3>=2.5.0",
26
+ ]
27
+
28
+ [[project.authors]]
29
+ name = "aligeromachine"
30
+ email = "aligeromachine@github.com"
31
+
32
+ [[project.maintainers]]
33
+ name = "aligeromachine"
34
+ email = "aligeromachine@github.com"
35
+
36
+ [project.urls]
37
+ Homepage = "https://reqmachine.org/"
38
+ Documentation = "https://github.com/aligeromachine/reqmachine/docs"
39
+ Repository = "https://github.com/aligeromachine/reqmachine"
40
+ Changelog = "https://github.com/aligeromachine/reqmachine/blob/main/CHANGES.md"
41
+
42
+ [dependency-groups]
43
+ dev = [
44
+ "django>=5.2",
45
+ "pytest>=9.2.0",
46
+ "responses>=0.26.3",
47
+ "ruff>=0.8.0",
48
+ ]
49
+
50
+ [tool.pytest.ini_options]
51
+ testpaths = ["tests"]
52
+
53
+ [tool.uv.build-backend]
54
+ module-root = ""
55
+ module-name = "reqmachine"
@@ -0,0 +1,55 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.12.0,<0.13.0"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "reqmachine"
7
+ version = "0.1.0"
8
+ description = "A small requests-based HTTP client with retries, JSON parsing and file upload/download helpers."
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ # license-files убран — файла LICENSE нет.
12
+ # uv_build сам подхватит LICENSE*/COPYING*/NOTICE*, если они появятся в корне.
13
+ authors = [
14
+ { name = "aligeromachine", email = "aligeromachine@github.com" }
15
+ ]
16
+ maintainers = [
17
+ { name = "aligeromachine", email = "aligeromachine@github.com" }
18
+ ]
19
+ requires-python = ">=3.14"
20
+ classifiers = [
21
+ "Development Status :: 4 - Beta",
22
+ "Intended Audience :: Developers",
23
+ "Operating System :: OS Independent",
24
+ "Programming Language :: Python :: 3",
25
+ "Programming Language :: Python :: 3.14",
26
+ "Topic :: Internet :: WWW/HTTP",
27
+ "Topic :: Software Development :: Libraries :: Python Modules",
28
+ "Typing :: Typed",
29
+ ]
30
+ dependencies = [
31
+ "orjson>=3.11.9",
32
+ "requests>=2.34.2",
33
+ "urllib3>=2.5.0",
34
+ ]
35
+
36
+ [project.urls]
37
+ Homepage = "https://reqmachine.org/"
38
+ Documentation = "https://github.com/aligeromachine/reqmachine/docs"
39
+ Repository = "https://github.com/aligeromachine/reqmachine"
40
+ Changelog = "https://github.com/aligeromachine/reqmachine/blob/main/CHANGES.md"
41
+
42
+ [dependency-groups]
43
+ dev = [
44
+ "django>=5.2",
45
+ "pytest>=9.2.0",
46
+ "responses>=0.26.3",
47
+ "ruff>=0.8.0",
48
+ ]
49
+
50
+ [tool.pytest.ini_options]
51
+ testpaths = ["tests"]
52
+
53
+ [tool.uv.build-backend]
54
+ module-root = ""
55
+ module-name = "reqmachine"
@@ -0,0 +1,34 @@
1
+ """reqmachine — a small requests-based HTTP client.
2
+
3
+ Retries, JSON parsing (orjson), file upload/download helpers and
4
+ convenience decorators on top of :mod:`requests`.
5
+ """
6
+
7
+ from .config import Config
8
+ from .errors import (
9
+ HttpClientError,
10
+ HttpConnectionError,
11
+ HttpDecodeError,
12
+ HttpStatusError,
13
+ HttpTimeoutError,
14
+ )
15
+ from .http_client import HttpClient, http_client, http_get, http_post
16
+ from .types import JsonDict, JsonList, JsonValue
17
+
18
+ __version__ = "0.1.0"
19
+
20
+ __all__ = [
21
+ "Config",
22
+ "HttpClient",
23
+ "HttpClientError",
24
+ "HttpConnectionError",
25
+ "HttpDecodeError",
26
+ "HttpStatusError",
27
+ "HttpTimeoutError",
28
+ "JsonDict",
29
+ "JsonList",
30
+ "JsonValue",
31
+ "http_client",
32
+ "http_get",
33
+ "http_post",
34
+ ]
@@ -0,0 +1,7 @@
1
+ #! /usr/bin/env python3
2
+ # -*- encoding: utf-8 -*-
3
+
4
+ from .__init__ import __version__
5
+
6
+ print('name: reqmachine')
7
+ print(f'version: {__version__}')
@@ -0,0 +1,13 @@
1
+ # ─────────────────────────── Конфиг ───────────────────────────
2
+
3
+ class Config:
4
+ API_TIMEOUT: tuple[float, float] = (3.0, 60.0)
5
+ API_UPLOAD_TIMEOUT: tuple[float, float] = (10.0, 600.0)
6
+ API_HEADERS: dict[str, str] = {
7
+ "Cache-Control": "no-cache",
8
+ "Accept": "*/*",
9
+ }
10
+ RETRY_TOTAL: int = 0
11
+ RETRY_BACKOFF: float = 0.5
12
+ RETRY_STATUSES: frozenset[int] = frozenset({429, 500, 502, 503, 504})
13
+ CHUNK_SIZE: int = 5 * 1024 * 1024 # 5 MiB
@@ -0,0 +1,35 @@
1
+ import requests
2
+
3
+ # ─────────────────────────── Исключения ───────────────────────────
4
+
5
+ class HttpClientError(Exception):
6
+ """Базовая ошибка HTTP-клиента."""
7
+
8
+ def __init__(
9
+ self,
10
+ message: str,
11
+ *,
12
+ status: int | None = None,
13
+ url: str | None = None,
14
+ response: requests.Response | None = None,
15
+ ) -> None:
16
+ super().__init__(message)
17
+ self.status = status
18
+ self.url = url
19
+ self.response = response
20
+
21
+
22
+ class HttpStatusError(HttpClientError):
23
+ """Ответ получен, но код не 2xx/3xx."""
24
+
25
+
26
+ class HttpConnectionError(HttpClientError):
27
+ """Не удалось установить соединение."""
28
+
29
+
30
+ class HttpTimeoutError(HttpClientError):
31
+ """Таймаут соединения/чтения."""
32
+
33
+
34
+ class HttpDecodeError(HttpClientError):
35
+ """Тело ответа не является валидным JSON."""
@@ -0,0 +1,336 @@
1
+ from functools import wraps
2
+ from pathlib import Path
3
+ from typing import Callable, Any, Self, Concatenate
4
+
5
+ import orjson
6
+ import requests
7
+ import urllib3
8
+ from urllib3.util.retry import Retry
9
+ from requests.adapters import HTTPAdapter
10
+ from requests.exceptions import (
11
+ ConnectionError as ReqConnectionError,
12
+ HTTPError,
13
+ RequestException,
14
+ Timeout as ReqTimeout,
15
+ )
16
+
17
+ from .config import Config
18
+ from .errors import (
19
+ HttpClientError,
20
+ HttpConnectionError,
21
+ HttpDecodeError,
22
+ HttpStatusError,
23
+ HttpTimeoutError,
24
+ )
25
+ from .types import (
26
+ ResponseFunc,
27
+ ResponseRequest,
28
+ P
29
+ )
30
+
31
+ # ─────────────────────────── Клиент ───────────────────────────
32
+
33
+ class HttpClient:
34
+ def __init__(
35
+ self,
36
+ *,
37
+ verify: bool = False,
38
+ timeout: tuple[float, float] = Config.API_TIMEOUT,
39
+ upload_timeout: tuple[float, float] = Config.API_UPLOAD_TIMEOUT,
40
+ headers: dict[str, str] | None = None,
41
+ retries: int = Config.RETRY_TOTAL,
42
+ ) -> None:
43
+ self.timeout = timeout
44
+ self.upload_timeout = upload_timeout
45
+ self.chunk_size = Config.CHUNK_SIZE
46
+
47
+ self.session = requests.Session()
48
+ self.session.headers.update(Config.API_HEADERS)
49
+ if headers:
50
+ self.session.headers.update(headers)
51
+ self.session.verify = verify
52
+
53
+ if not verify:
54
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
55
+
56
+ retry = Retry(
57
+ total=retries,
58
+ backoff_factor=Config.RETRY_BACKOFF,
59
+ status_forcelist=tuple(Config.RETRY_STATUSES),
60
+ allowed_methods=["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"],
61
+ raise_on_status=False, # пусть raise_for_status сработает у нас
62
+ )
63
+ adapter = HTTPAdapter(max_retries=retry)
64
+ self.session.mount("http://", adapter)
65
+ self.session.mount("https://", adapter)
66
+
67
+ # ─── Жизненный цикл ───
68
+
69
+ def __enter__(self) -> Self:
70
+ return self
71
+
72
+ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
73
+ self.close()
74
+
75
+ def close(self) -> None:
76
+ self.session.close()
77
+
78
+ # ─── Парсинг ───
79
+
80
+ def _parse(self, response: requests.Response) -> ResponseRequest:
81
+ if not response.content:
82
+ return None
83
+
84
+ ctype = response.headers.get("Content-Type", "").lower()
85
+ head = response.content[:1]
86
+ looks_json = head in (b"{", b"[") or "json" in ctype
87
+
88
+ if not looks_json:
89
+ raise HttpDecodeError(
90
+ f"Non-JSON response from {response.url} "
91
+ f"(Content-Type: {ctype or 'unknown'}): "
92
+ f"{response.content[:200]!r}",
93
+ url=response.url,
94
+ response=response,
95
+ )
96
+
97
+ try:
98
+ return orjson.loads(response.content)
99
+ except orjson.JSONDecodeError as exc:
100
+ raise HttpDecodeError(
101
+ f"Invalid JSON from {response.url}: {exc}",
102
+ url=response.url,
103
+ response=response,
104
+ ) from exc
105
+
106
+ # ─── Низкоуровневый запрос ───
107
+
108
+ def _request(self, method: str, url: str, **kwargs: Any) -> ResponseRequest:
109
+ if kwargs.get("stream"):
110
+ raise ValueError(
111
+ "stream=True is not supported here; "
112
+ "use download_file() or access self.session directly"
113
+ )
114
+
115
+ kwargs.setdefault("timeout", self.timeout)
116
+
117
+ try:
118
+ response = self.session.request(method, url, **kwargs)
119
+ response.raise_for_status()
120
+ except HTTPError as exc:
121
+ r = exc.response
122
+ status = r.status_code if r is not None else None
123
+ raise HttpStatusError(
124
+ f"HTTP {method} {url} -> {status}",
125
+ status=status,
126
+ url=url,
127
+ response=r,
128
+ ) from exc
129
+ except ReqTimeout as exc:
130
+ raise HttpTimeoutError(
131
+ f"Timeout {method} {url}: {exc}", url=url
132
+ ) from exc
133
+ except ReqConnectionError as exc:
134
+ raise HttpConnectionError(
135
+ f"Connection error {method} {url}: {exc}", url=url
136
+ ) from exc
137
+ except RequestException as exc:
138
+ raise HttpClientError(
139
+ f"Request failed {method} {url}: {exc}", url=url
140
+ ) from exc
141
+
142
+ return self._parse(response)
143
+
144
+ # ─── HTTP-методы ───
145
+
146
+ def get(
147
+ self,
148
+ url: str,
149
+ params: dict[str, Any] | None = None,
150
+ **kwargs: Any,
151
+ ) -> ResponseRequest:
152
+ return self._request("GET", url, params=params, **kwargs)
153
+
154
+ def post(
155
+ self,
156
+ url: str,
157
+ *,
158
+ json: Any = None,
159
+ data: Any = None,
160
+ files: Any = None,
161
+ **kwargs: Any,
162
+ ) -> ResponseRequest:
163
+ if json is not None and data is not None:
164
+ raise ValueError("Pass either json= or data=, not both")
165
+ return self._request("POST", url, json=json, data=data, files=files, **kwargs)
166
+
167
+ def put(
168
+ self,
169
+ url: str,
170
+ *,
171
+ json: Any = None,
172
+ data: Any = None,
173
+ **kwargs: Any,
174
+ ) -> ResponseRequest:
175
+ if json is not None and data is not None:
176
+ raise ValueError("Pass either json= or data=, not both")
177
+ return self._request("PUT", url, json=json, data=data, **kwargs)
178
+
179
+ def patch(
180
+ self,
181
+ url: str,
182
+ *,
183
+ json: Any = None,
184
+ data: Any = None,
185
+ **kwargs: Any,
186
+ ) -> ResponseRequest:
187
+ if json is not None and data is not None:
188
+ raise ValueError("Pass either json= or data=, not both")
189
+ return self._request("PATCH", url, json=json, data=data, **kwargs)
190
+
191
+ def delete(self, url: str, **kwargs: Any) -> ResponseRequest:
192
+ return self._request("DELETE", url, **kwargs)
193
+
194
+ # ─── Проверка доступности ───
195
+
196
+ def is_url_available(self, url: str) -> bool:
197
+ try:
198
+ rsp = self.session.head(
199
+ url, timeout=self.timeout, allow_redirects=True
200
+ )
201
+ # Некоторые сервера отдают 405 на HEAD — пробуем GET
202
+ if rsp.status_code == 405:
203
+ rsp = self.session.get(
204
+ url, timeout=self.timeout, allow_redirects=True, stream=True
205
+ )
206
+ rsp.close()
207
+ return 200 <= rsp.status_code < 400
208
+ except RequestException:
209
+ # Это проверка доступности, а не запрос данных: любая
210
+ # сетевая ошибка означает "недоступен", без исключения.
211
+ return False
212
+
213
+ # ─── Файлы ───
214
+
215
+ def upload_file(
216
+ self,
217
+ url: str,
218
+ filein: Path,
219
+ meta_field: str = "JSONData",
220
+ tags: Any = None,
221
+ ) -> bool:
222
+ """Загружает файл. Бросает HttpClientError при ошибке."""
223
+ try:
224
+ with filein.open("rb") as fr:
225
+ files = {"file": (filein.name, fr, "application/octet-stream")}
226
+ data = {
227
+ meta_field: orjson.dumps(
228
+ {"filename": filein.name, "tags": tags}
229
+ )
230
+ }
231
+ rsp = self.session.post(
232
+ url,
233
+ files=files,
234
+ data=data,
235
+ timeout=self.upload_timeout,
236
+ )
237
+ rsp.raise_for_status()
238
+ except HTTPError as exc:
239
+ r = exc.response
240
+ raise HttpStatusError(
241
+ f"Upload failed {filein.name} -> {url}: "
242
+ f"{r.status_code if r is not None else '?'}",
243
+ status=r.status_code if r is not None else None,
244
+ url=url,
245
+ response=r,
246
+ ) from exc
247
+ except ReqTimeout as exc:
248
+ raise HttpTimeoutError(
249
+ f"Upload timed out {filein.name} -> {url}: {exc}", url=url
250
+ ) from exc
251
+ except ReqConnectionError as exc:
252
+ raise HttpConnectionError(
253
+ f"Upload connection error {filein.name} -> {url}: {exc}", url=url
254
+ ) from exc
255
+ except RequestException as exc:
256
+ raise HttpClientError(
257
+ f"Upload failed {filein.name} -> {url}: {exc}", url=url
258
+ ) from exc
259
+ return True
260
+
261
+ def download_file(
262
+ self, url: str,
263
+ fileout: Path
264
+ ) -> bool:
265
+ fileout.parent.mkdir(parents=True, exist_ok=True)
266
+ try:
267
+ with self.session.get(url, stream=True, timeout=self.upload_timeout) as rsp:
268
+ rsp.raise_for_status()
269
+ with fileout.open("wb") as fw:
270
+ for chunk in rsp.iter_content(chunk_size=self.chunk_size):
271
+ if chunk:
272
+ fw.write(chunk)
273
+ except HTTPError as exc:
274
+ r = exc.response
275
+ raise HttpStatusError(
276
+ f"Download failed {url} -> {fileout}: "
277
+ f"{r.status_code if r is not None else '?'}",
278
+ status=r.status_code if r is not None else None,
279
+ url=url,
280
+ response=r,
281
+ ) from exc
282
+ except ReqTimeout as exc:
283
+ raise HttpTimeoutError(
284
+ f"Download timed out {url} -> {fileout}: {exc}", url=url
285
+ ) from exc
286
+ except ReqConnectionError as exc:
287
+ raise HttpConnectionError(
288
+ f"Download connection error {url} -> {fileout}: {exc}", url=url
289
+ ) from exc
290
+ except RequestException as exc:
291
+ raise HttpClientError(
292
+ f"Download failed {url} -> {fileout}: {exc}", url=url
293
+ ) from exc
294
+
295
+ return True
296
+
297
+ def http_client(
298
+ func: Callable[Concatenate[HttpClient, P], ResponseFunc]
299
+ ) -> Callable[P, ResponseFunc]:
300
+ @wraps(func)
301
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> ResponseFunc:
302
+ with HttpClient() as client:
303
+ # kwargs.setdefault('client', client)
304
+ return func(client, *args, **kwargs)
305
+ return wrapper
306
+
307
+ @http_client
308
+ def http_get(client: HttpClient, url: str, data: dict | None = None) -> ResponseRequest:
309
+ return client.get(url, data)
310
+
311
+ @http_client
312
+ def http_post(client: HttpClient, url: str, json: dict | None = None) -> ResponseRequest:
313
+ return client.post(url, json=json)
314
+
315
+
316
+ # USE CASE
317
+ # from typing import Any
318
+ # from request.http_client import HttpClient, http_client, http_get
319
+
320
+ # @http_client
321
+ # def fetch_posts(client: HttpClient, *args: Any, **kwargs: Any):
322
+ # respo = client.get("https://jsonplaceholder.typicode.com/users")
323
+ # print(respo)
324
+ # respo = client.get("https://jsonplaceholder.typicode.com/posts")
325
+ # print(respo)
326
+
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
+
335
+ # if __name__ == "__main__":
336
+ # main()
@@ -0,0 +1,12 @@
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")