t3api-utils 1.4.0__tar.gz → 1.5.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.
- {t3api_utils-1.4.0/t3api_utils.egg-info → t3api_utils-1.5.0}/PKG-INFO +1 -1
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/pyproject.toml +1 -1
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/api/operations.py +42 -14
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/http/utils.py +381 -70
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/openapi/spec_fetcher.py +9 -4
- {t3api_utils-1.4.0 → t3api_utils-1.5.0/t3api_utils.egg-info}/PKG-INFO +1 -1
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_api_operations.py +123 -4
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_http.py +210 -2
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_openapi.py +21 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/LICENSE +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/README.md +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/setup.cfg +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/api/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/api/client.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/api/interfaces.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/api/parallel.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/auth/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/auth/interfaces.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/auth/utils.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/cli/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/cli/consts.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/cli/utils.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/collection/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/collection/utils.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/db/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/db/consts.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/db/utils.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/exceptions.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/file/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/file/consts.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/file/utils.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/http/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/inspector/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/inspector/app.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/interfaces.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/intro.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/logging.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/main/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/main/handlers.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/main/utils.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/openapi/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/openapi/collection_picker.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/style/__init__.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/style/console.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/style/messages.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils/style/styles.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils.egg-info/SOURCES.txt +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils.egg-info/dependency_links.txt +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils.egg-info/requires.txt +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/t3api_utils.egg-info/top_level.txt +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_api_auth.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_api_client.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_api_interfaces.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_api_parallel.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_auth.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_cli.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_collection.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_db.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_exceptions.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_files.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_inspector.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_interfaces.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_logging.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_main.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_openapi_integration.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_smoke.py +0 -0
- {t3api_utils-1.4.0 → t3api_utils-1.5.0}/tests/test_style.py +0 -0
|
@@ -16,7 +16,8 @@ from typing import Any, Dict, List, Literal, Optional, Union, cast
|
|
|
16
16
|
|
|
17
17
|
from t3api_utils.api.client import T3APIClient
|
|
18
18
|
from t3api_utils.api.interfaces import MetrcCollectionResponse
|
|
19
|
-
from t3api_utils.http.utils import
|
|
19
|
+
from t3api_utils.http.utils import ResponseType, T3HTTPError
|
|
20
|
+
from t3api_utils.http import utils as _http_utils
|
|
20
21
|
|
|
21
22
|
|
|
22
23
|
def send_api_request(
|
|
@@ -29,6 +30,7 @@ def send_api_request(
|
|
|
29
30
|
files: Optional[Any] = None,
|
|
30
31
|
headers: Optional[Dict[str, str]] = None,
|
|
31
32
|
expected_status: Union[int, tuple[int, ...]] = 200,
|
|
33
|
+
response_type: ResponseType = "json",
|
|
32
34
|
) -> Any:
|
|
33
35
|
"""Send a request to any T3 API endpoint (sync wrapper).
|
|
34
36
|
|
|
@@ -48,9 +50,14 @@ def send_api_request(
|
|
|
48
50
|
``{"file": ("name.png", data, "image/png")}``).
|
|
49
51
|
headers: Additional headers (optional)
|
|
50
52
|
expected_status: Expected HTTP status code(s) (default: 200)
|
|
53
|
+
response_type: How to interpret the response body. ``"json"``
|
|
54
|
+
(default) parses as JSON, ``"bytes"`` returns raw bytes,
|
|
55
|
+
``"text"`` returns decoded text, ``"response"`` returns the
|
|
56
|
+
full ``httpx.Response``.
|
|
51
57
|
|
|
52
58
|
Returns:
|
|
53
|
-
|
|
59
|
+
Response data whose type depends on *response_type*:
|
|
60
|
+
JSON-parsed ``Any``, ``bytes``, ``str``, or ``httpx.Response``.
|
|
54
61
|
|
|
55
62
|
Raises:
|
|
56
63
|
ValueError: If both *json_body* and *files* are provided.
|
|
@@ -59,13 +66,20 @@ def send_api_request(
|
|
|
59
66
|
if json_body is not None and files is not None:
|
|
60
67
|
raise ValueError("json_body and files are mutually exclusive; provide one or neither.")
|
|
61
68
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
69
|
+
from t3api_utils.http.utils import (
|
|
70
|
+
request_bytes,
|
|
71
|
+
request_json,
|
|
72
|
+
request_raw,
|
|
73
|
+
request_text,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
_sync_dispatchers = {
|
|
77
|
+
"json": request_json,
|
|
78
|
+
"bytes": request_bytes,
|
|
79
|
+
"text": request_text,
|
|
80
|
+
"response": request_raw,
|
|
81
|
+
}
|
|
67
82
|
|
|
68
|
-
# Use the sync version of request_json directly with the client's underlying config
|
|
69
83
|
if not client.is_authenticated:
|
|
70
84
|
raise T3HTTPError("Client is not authenticated. Call authenticate_with_credentials() first.")
|
|
71
85
|
|
|
@@ -90,8 +104,10 @@ def send_api_request(
|
|
|
90
104
|
if client._config.proxies:
|
|
91
105
|
client_kwargs["proxies"] = client._config.proxies
|
|
92
106
|
|
|
107
|
+
dispatch_fn = _sync_dispatchers[response_type]
|
|
108
|
+
|
|
93
109
|
with httpx.Client(**client_kwargs) as sync_client:
|
|
94
|
-
return
|
|
110
|
+
return dispatch_fn(
|
|
95
111
|
client=sync_client,
|
|
96
112
|
method=method,
|
|
97
113
|
url=path,
|
|
@@ -253,7 +269,7 @@ async def get_collection_async(
|
|
|
253
269
|
params["filter"] = filter
|
|
254
270
|
|
|
255
271
|
try:
|
|
256
|
-
response_data = await arequest_json(
|
|
272
|
+
response_data = await _http_utils.arequest_json(
|
|
257
273
|
aclient=client._client,
|
|
258
274
|
method="GET",
|
|
259
275
|
url=path,
|
|
@@ -278,6 +294,7 @@ async def send_api_request_async(
|
|
|
278
294
|
files: Optional[Any] = None,
|
|
279
295
|
headers: Optional[Dict[str, str]] = None,
|
|
280
296
|
expected_status: Union[int, tuple[int, ...]] = 200,
|
|
297
|
+
response_type: ResponseType = "json",
|
|
281
298
|
) -> Any:
|
|
282
299
|
"""Send a request to any T3 API endpoint using an async client.
|
|
283
300
|
|
|
@@ -296,9 +313,14 @@ async def send_api_request_async(
|
|
|
296
313
|
``files`` parameter.
|
|
297
314
|
headers: Additional headers (optional)
|
|
298
315
|
expected_status: Expected HTTP status code(s) (default: 200)
|
|
316
|
+
response_type: How to interpret the response body. ``"json"``
|
|
317
|
+
(default) parses as JSON, ``"bytes"`` returns raw bytes,
|
|
318
|
+
``"text"`` returns decoded text, ``"response"`` returns the
|
|
319
|
+
full ``httpx.Response``.
|
|
299
320
|
|
|
300
321
|
Returns:
|
|
301
|
-
|
|
322
|
+
Response data whose type depends on *response_type*:
|
|
323
|
+
JSON-parsed ``Any``, ``bytes``, ``str``, or ``httpx.Response``.
|
|
302
324
|
|
|
303
325
|
Raises:
|
|
304
326
|
ValueError: If both *json_body* and *files* are provided.
|
|
@@ -310,8 +332,16 @@ async def send_api_request_async(
|
|
|
310
332
|
if not client.is_authenticated:
|
|
311
333
|
raise T3HTTPError("Client is not authenticated. Call authenticate_with_credentials() first.")
|
|
312
334
|
|
|
335
|
+
_async_dispatchers = {
|
|
336
|
+
"json": _http_utils.arequest_json,
|
|
337
|
+
"bytes": _http_utils.arequest_bytes,
|
|
338
|
+
"text": _http_utils.arequest_text,
|
|
339
|
+
"response": _http_utils.arequest_raw,
|
|
340
|
+
}
|
|
341
|
+
dispatch_fn = _async_dispatchers[response_type]
|
|
342
|
+
|
|
313
343
|
try:
|
|
314
|
-
|
|
344
|
+
return await dispatch_fn(
|
|
315
345
|
aclient=client._client,
|
|
316
346
|
method=method,
|
|
317
347
|
url=path,
|
|
@@ -323,7 +353,5 @@ async def send_api_request_async(
|
|
|
323
353
|
expected_status=expected_status,
|
|
324
354
|
)
|
|
325
355
|
|
|
326
|
-
return response_data
|
|
327
|
-
|
|
328
356
|
except T3HTTPError as e:
|
|
329
357
|
raise T3HTTPError(f"Failed to get data from {path}: {e}", response=e.response) from e
|
|
@@ -39,8 +39,8 @@ import logging
|
|
|
39
39
|
import random
|
|
40
40
|
import time
|
|
41
41
|
from dataclasses import dataclass, field
|
|
42
|
-
from typing import (Any, Dict, Iterable, List, Mapping, Optional, Sequence,
|
|
43
|
-
Union)
|
|
42
|
+
from typing import (Any, Dict, Iterable, List, Literal, Mapping, Optional, Sequence,
|
|
43
|
+
Tuple, Union)
|
|
44
44
|
|
|
45
45
|
import ssl
|
|
46
46
|
import certifi
|
|
@@ -50,15 +50,24 @@ from httpx._types import RequestFiles
|
|
|
50
50
|
# Import config manager for default values
|
|
51
51
|
from t3api_utils.cli.utils import config_manager
|
|
52
52
|
|
|
53
|
+
ResponseType = Literal["json", "bytes", "text", "response"]
|
|
54
|
+
|
|
53
55
|
__all__ = [
|
|
54
56
|
"HTTPConfig",
|
|
55
57
|
"RetryPolicy",
|
|
56
58
|
"LoggingHooks",
|
|
57
59
|
"T3HTTPError",
|
|
60
|
+
"ResponseType",
|
|
58
61
|
"build_client",
|
|
59
62
|
"build_async_client",
|
|
60
63
|
"request_json",
|
|
61
64
|
"arequest_json",
|
|
65
|
+
"request_bytes",
|
|
66
|
+
"arequest_bytes",
|
|
67
|
+
"request_text",
|
|
68
|
+
"arequest_text",
|
|
69
|
+
"request_raw",
|
|
70
|
+
"arequest_raw",
|
|
62
71
|
"set_bearer_token",
|
|
63
72
|
"clear_bearer_token",
|
|
64
73
|
]
|
|
@@ -534,7 +543,7 @@ def _format_http_error_message(resp: httpx.Response) -> str:
|
|
|
534
543
|
return f"HTTP {resp.status_code}: {text or '<no body>'}"
|
|
535
544
|
|
|
536
545
|
|
|
537
|
-
def
|
|
546
|
+
def _request_core(
|
|
538
547
|
*,
|
|
539
548
|
client: httpx.Client,
|
|
540
549
|
method: str,
|
|
@@ -547,12 +556,12 @@ def request_json(
|
|
|
547
556
|
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
548
557
|
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
549
558
|
request_id: Optional[str] = None,
|
|
550
|
-
) ->
|
|
551
|
-
"""
|
|
559
|
+
) -> httpx.Response:
|
|
560
|
+
"""Execute a synchronous HTTP request with retry logic.
|
|
552
561
|
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
562
|
+
Handles the retry loop, status validation, and error wrapping.
|
|
563
|
+
Returns the raw ``httpx.Response`` on success. Callers are
|
|
564
|
+
responsible for interpreting the response body.
|
|
556
565
|
|
|
557
566
|
Args:
|
|
558
567
|
client: An ``httpx.Client`` (typically from :func:`build_client`).
|
|
@@ -562,27 +571,20 @@ def request_json(
|
|
|
562
571
|
json_body: Optional JSON-serializable request body. Mutually
|
|
563
572
|
exclusive with *files*.
|
|
564
573
|
files: Optional multipart file upload data. Mutually exclusive
|
|
565
|
-
with *json_body*.
|
|
566
|
-
|
|
567
|
-
``{"field": ("name.png", data, "image/png")}``).
|
|
568
|
-
headers: Optional per-request headers merged on top of the
|
|
569
|
-
client's default headers.
|
|
574
|
+
with *json_body*.
|
|
575
|
+
headers: Optional per-request headers.
|
|
570
576
|
policy: Retry policy. Defaults to ``RetryPolicy()`` when ``None``.
|
|
571
|
-
expected_status: Status code(s) considered successful.
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
default.
|
|
575
|
-
request_id: Optional value set as the ``X-Request-ID`` header
|
|
576
|
-
(unless already present in *headers*).
|
|
577
|
+
expected_status: Status code(s) considered successful.
|
|
578
|
+
timeout: Per-request timeout override.
|
|
579
|
+
request_id: Optional ``X-Request-ID`` header value.
|
|
577
580
|
|
|
578
581
|
Returns:
|
|
579
|
-
|
|
582
|
+
The ``httpx.Response`` with a status code in *expected_status*.
|
|
580
583
|
|
|
581
584
|
Raises:
|
|
582
585
|
ValueError: If both *json_body* and *files* are provided.
|
|
583
586
|
T3HTTPError: If the response status is not in *expected_status*
|
|
584
|
-
after all retries are exhausted
|
|
585
|
-
cannot be decoded as JSON.
|
|
587
|
+
after all retries are exhausted.
|
|
586
588
|
"""
|
|
587
589
|
if json_body is not None and files is not None:
|
|
588
590
|
raise ValueError("json_body and files are mutually exclusive; provide one or neither.")
|
|
@@ -616,14 +618,7 @@ def request_json(
|
|
|
616
618
|
continue
|
|
617
619
|
raise T3HTTPError(_format_http_error_message(resp), response=resp)
|
|
618
620
|
|
|
619
|
-
|
|
620
|
-
return None
|
|
621
|
-
if not resp.content:
|
|
622
|
-
return None
|
|
623
|
-
try:
|
|
624
|
-
return resp.json()
|
|
625
|
-
except json.JSONDecodeError as e:
|
|
626
|
-
raise T3HTTPError("Failed to decode JSON response.", response=resp) from e
|
|
621
|
+
return resp
|
|
627
622
|
except httpx.HTTPError as e:
|
|
628
623
|
if _should_retry(policy=pol, attempt=attempt, method=method, exc=e, resp=None):
|
|
629
624
|
_sleep_with_backoff(pol, attempt)
|
|
@@ -631,6 +626,261 @@ def request_json(
|
|
|
631
626
|
raise T3HTTPError(str(e)) from e
|
|
632
627
|
|
|
633
628
|
|
|
629
|
+
async def _arequest_core(
|
|
630
|
+
*,
|
|
631
|
+
aclient: httpx.AsyncClient,
|
|
632
|
+
method: str,
|
|
633
|
+
url: str,
|
|
634
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
635
|
+
json_body: Optional[Any] = None,
|
|
636
|
+
files: Optional[RequestFiles] = None,
|
|
637
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
638
|
+
policy: Optional[RetryPolicy] = None,
|
|
639
|
+
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
640
|
+
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
641
|
+
request_id: Optional[str] = None,
|
|
642
|
+
) -> httpx.Response:
|
|
643
|
+
"""Async equivalent of :func:`_request_core`."""
|
|
644
|
+
if json_body is not None and files is not None:
|
|
645
|
+
raise ValueError("json_body and files are mutually exclusive; provide one or neither.")
|
|
646
|
+
|
|
647
|
+
pol = policy or RetryPolicy()
|
|
648
|
+
exp: Tuple[int, ...] = (
|
|
649
|
+
(expected_status,) if isinstance(expected_status, int) else tuple(expected_status)
|
|
650
|
+
)
|
|
651
|
+
|
|
652
|
+
# Merge headers + optional request id
|
|
653
|
+
merged_headers = dict(headers or {})
|
|
654
|
+
if request_id and "X-Request-ID" not in merged_headers:
|
|
655
|
+
merged_headers["X-Request-ID"] = request_id
|
|
656
|
+
|
|
657
|
+
attempt = 0
|
|
658
|
+
while True:
|
|
659
|
+
attempt += 1
|
|
660
|
+
try:
|
|
661
|
+
resp = await aclient.request(
|
|
662
|
+
method.upper(),
|
|
663
|
+
url,
|
|
664
|
+
params=params,
|
|
665
|
+
json=json_body,
|
|
666
|
+
files=files,
|
|
667
|
+
headers=merged_headers or None,
|
|
668
|
+
timeout=timeout,
|
|
669
|
+
)
|
|
670
|
+
if resp.status_code not in exp:
|
|
671
|
+
if _should_retry(policy=pol, attempt=attempt, method=method, exc=None, resp=resp):
|
|
672
|
+
await _async_sleep_with_backoff(pol, attempt)
|
|
673
|
+
continue
|
|
674
|
+
raise T3HTTPError(_format_http_error_message(resp), response=resp)
|
|
675
|
+
|
|
676
|
+
return resp
|
|
677
|
+
except httpx.HTTPError as e:
|
|
678
|
+
if _should_retry(policy=pol, attempt=attempt, method=method, exc=e, resp=None):
|
|
679
|
+
await _async_sleep_with_backoff(pol, attempt)
|
|
680
|
+
continue
|
|
681
|
+
raise T3HTTPError(str(e)) from e
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
def _parse_json_response(resp: httpx.Response) -> Any:
|
|
685
|
+
"""Extract JSON from a successful response.
|
|
686
|
+
|
|
687
|
+
Returns ``None`` for 204 or empty bodies.
|
|
688
|
+
|
|
689
|
+
Raises:
|
|
690
|
+
T3HTTPError: If the body cannot be decoded as JSON.
|
|
691
|
+
"""
|
|
692
|
+
if resp.status_code == 204:
|
|
693
|
+
return None
|
|
694
|
+
if not resp.content:
|
|
695
|
+
return None
|
|
696
|
+
try:
|
|
697
|
+
return resp.json()
|
|
698
|
+
except json.JSONDecodeError as e:
|
|
699
|
+
raise T3HTTPError("Failed to decode JSON response.", response=resp) from e
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
def request_json(
|
|
703
|
+
*,
|
|
704
|
+
client: httpx.Client,
|
|
705
|
+
method: str,
|
|
706
|
+
url: str,
|
|
707
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
708
|
+
json_body: Optional[Any] = None,
|
|
709
|
+
files: Optional[RequestFiles] = None,
|
|
710
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
711
|
+
policy: Optional[RetryPolicy] = None,
|
|
712
|
+
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
713
|
+
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
714
|
+
request_id: Optional[str] = None,
|
|
715
|
+
) -> Any:
|
|
716
|
+
"""Issue a synchronous JSON request with automatic retries.
|
|
717
|
+
|
|
718
|
+
Sends the request via the provided ``httpx.Client``, retrying on
|
|
719
|
+
transient failures according to the supplied (or default) retry
|
|
720
|
+
policy. The response body is parsed as JSON and returned.
|
|
721
|
+
|
|
722
|
+
Args:
|
|
723
|
+
client: An ``httpx.Client`` (typically from :func:`build_client`).
|
|
724
|
+
method: HTTP method (e.g. ``"GET"``, ``"POST"``).
|
|
725
|
+
url: Request URL or path (resolved against the client's base URL).
|
|
726
|
+
params: Optional query-string parameters.
|
|
727
|
+
json_body: Optional JSON-serializable request body. Mutually
|
|
728
|
+
exclusive with *files*.
|
|
729
|
+
files: Optional multipart file upload data. Mutually exclusive
|
|
730
|
+
with *json_body*. Accepts the same formats as ``httpx``'s
|
|
731
|
+
``files`` parameter (e.g.
|
|
732
|
+
``{"field": ("name.png", data, "image/png")}``).
|
|
733
|
+
headers: Optional per-request headers merged on top of the
|
|
734
|
+
client's default headers.
|
|
735
|
+
policy: Retry policy. Defaults to ``RetryPolicy()`` when ``None``.
|
|
736
|
+
expected_status: Status code(s) considered successful. Defaults
|
|
737
|
+
to ``(200, 201, 202, 204)``.
|
|
738
|
+
timeout: Per-request timeout override. ``None`` uses the client
|
|
739
|
+
default.
|
|
740
|
+
request_id: Optional value set as the ``X-Request-ID`` header
|
|
741
|
+
(unless already present in *headers*).
|
|
742
|
+
|
|
743
|
+
Returns:
|
|
744
|
+
Parsed JSON response body, or ``None`` for 204 / empty responses.
|
|
745
|
+
|
|
746
|
+
Raises:
|
|
747
|
+
ValueError: If both *json_body* and *files* are provided.
|
|
748
|
+
T3HTTPError: If the response status is not in *expected_status*
|
|
749
|
+
after all retries are exhausted, or if the response body
|
|
750
|
+
cannot be decoded as JSON.
|
|
751
|
+
"""
|
|
752
|
+
resp = _request_core(
|
|
753
|
+
client=client,
|
|
754
|
+
method=method,
|
|
755
|
+
url=url,
|
|
756
|
+
params=params,
|
|
757
|
+
json_body=json_body,
|
|
758
|
+
files=files,
|
|
759
|
+
headers=headers,
|
|
760
|
+
policy=policy,
|
|
761
|
+
expected_status=expected_status,
|
|
762
|
+
timeout=timeout,
|
|
763
|
+
request_id=request_id,
|
|
764
|
+
)
|
|
765
|
+
return _parse_json_response(resp)
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
def request_bytes(
|
|
769
|
+
*,
|
|
770
|
+
client: httpx.Client,
|
|
771
|
+
method: str,
|
|
772
|
+
url: str,
|
|
773
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
774
|
+
json_body: Optional[Any] = None,
|
|
775
|
+
files: Optional[RequestFiles] = None,
|
|
776
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
777
|
+
policy: Optional[RetryPolicy] = None,
|
|
778
|
+
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
779
|
+
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
780
|
+
request_id: Optional[str] = None,
|
|
781
|
+
) -> bytes:
|
|
782
|
+
"""Issue a synchronous request and return the response body as bytes.
|
|
783
|
+
|
|
784
|
+
Same retry and error handling as :func:`request_json`, but returns
|
|
785
|
+
raw bytes instead of parsing JSON. Useful for downloading PDFs,
|
|
786
|
+
images, and other binary content.
|
|
787
|
+
|
|
788
|
+
Returns:
|
|
789
|
+
Response body as ``bytes``. Empty ``b""`` for 204 / empty responses.
|
|
790
|
+
"""
|
|
791
|
+
resp = _request_core(
|
|
792
|
+
client=client,
|
|
793
|
+
method=method,
|
|
794
|
+
url=url,
|
|
795
|
+
params=params,
|
|
796
|
+
json_body=json_body,
|
|
797
|
+
files=files,
|
|
798
|
+
headers=headers,
|
|
799
|
+
policy=policy,
|
|
800
|
+
expected_status=expected_status,
|
|
801
|
+
timeout=timeout,
|
|
802
|
+
request_id=request_id,
|
|
803
|
+
)
|
|
804
|
+
return resp.content
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
def request_text(
|
|
808
|
+
*,
|
|
809
|
+
client: httpx.Client,
|
|
810
|
+
method: str,
|
|
811
|
+
url: str,
|
|
812
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
813
|
+
json_body: Optional[Any] = None,
|
|
814
|
+
files: Optional[RequestFiles] = None,
|
|
815
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
816
|
+
policy: Optional[RetryPolicy] = None,
|
|
817
|
+
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
818
|
+
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
819
|
+
request_id: Optional[str] = None,
|
|
820
|
+
) -> str:
|
|
821
|
+
"""Issue a synchronous request and return the response body as text.
|
|
822
|
+
|
|
823
|
+
Same retry and error handling as :func:`request_json`, but returns
|
|
824
|
+
decoded text instead of parsing JSON. Useful for CSV, HTML, and
|
|
825
|
+
other text content.
|
|
826
|
+
|
|
827
|
+
Returns:
|
|
828
|
+
Response body as ``str``. Empty ``""`` for 204 / empty responses.
|
|
829
|
+
"""
|
|
830
|
+
resp = _request_core(
|
|
831
|
+
client=client,
|
|
832
|
+
method=method,
|
|
833
|
+
url=url,
|
|
834
|
+
params=params,
|
|
835
|
+
json_body=json_body,
|
|
836
|
+
files=files,
|
|
837
|
+
headers=headers,
|
|
838
|
+
policy=policy,
|
|
839
|
+
expected_status=expected_status,
|
|
840
|
+
timeout=timeout,
|
|
841
|
+
request_id=request_id,
|
|
842
|
+
)
|
|
843
|
+
return resp.text
|
|
844
|
+
|
|
845
|
+
|
|
846
|
+
def request_raw(
|
|
847
|
+
*,
|
|
848
|
+
client: httpx.Client,
|
|
849
|
+
method: str,
|
|
850
|
+
url: str,
|
|
851
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
852
|
+
json_body: Optional[Any] = None,
|
|
853
|
+
files: Optional[RequestFiles] = None,
|
|
854
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
855
|
+
policy: Optional[RetryPolicy] = None,
|
|
856
|
+
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
857
|
+
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
858
|
+
request_id: Optional[str] = None,
|
|
859
|
+
) -> httpx.Response:
|
|
860
|
+
"""Issue a synchronous request and return the raw ``httpx.Response``.
|
|
861
|
+
|
|
862
|
+
Same retry and error handling as :func:`request_json`, but returns
|
|
863
|
+
the full response object for callers that need access to headers,
|
|
864
|
+
content-type, or streaming.
|
|
865
|
+
|
|
866
|
+
Returns:
|
|
867
|
+
The validated ``httpx.Response``.
|
|
868
|
+
"""
|
|
869
|
+
return _request_core(
|
|
870
|
+
client=client,
|
|
871
|
+
method=method,
|
|
872
|
+
url=url,
|
|
873
|
+
params=params,
|
|
874
|
+
json_body=json_body,
|
|
875
|
+
files=files,
|
|
876
|
+
headers=headers,
|
|
877
|
+
policy=policy,
|
|
878
|
+
expected_status=expected_status,
|
|
879
|
+
timeout=timeout,
|
|
880
|
+
request_id=request_id,
|
|
881
|
+
)
|
|
882
|
+
|
|
883
|
+
|
|
634
884
|
async def arequest_json(
|
|
635
885
|
*,
|
|
636
886
|
aclient: httpx.AsyncClient,
|
|
@@ -681,51 +931,112 @@ async def arequest_json(
|
|
|
681
931
|
after all retries are exhausted, or if the response body
|
|
682
932
|
cannot be decoded as JSON.
|
|
683
933
|
"""
|
|
684
|
-
|
|
685
|
-
|
|
934
|
+
resp = await _arequest_core(
|
|
935
|
+
aclient=aclient,
|
|
936
|
+
method=method,
|
|
937
|
+
url=url,
|
|
938
|
+
params=params,
|
|
939
|
+
json_body=json_body,
|
|
940
|
+
files=files,
|
|
941
|
+
headers=headers,
|
|
942
|
+
policy=policy,
|
|
943
|
+
expected_status=expected_status,
|
|
944
|
+
timeout=timeout,
|
|
945
|
+
request_id=request_id,
|
|
946
|
+
)
|
|
947
|
+
return _parse_json_response(resp)
|
|
686
948
|
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
949
|
+
|
|
950
|
+
async def arequest_bytes(
|
|
951
|
+
*,
|
|
952
|
+
aclient: httpx.AsyncClient,
|
|
953
|
+
method: str,
|
|
954
|
+
url: str,
|
|
955
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
956
|
+
json_body: Optional[Any] = None,
|
|
957
|
+
files: Optional[RequestFiles] = None,
|
|
958
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
959
|
+
policy: Optional[RetryPolicy] = None,
|
|
960
|
+
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
961
|
+
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
962
|
+
request_id: Optional[str] = None,
|
|
963
|
+
) -> bytes:
|
|
964
|
+
"""Async equivalent of :func:`request_bytes`."""
|
|
965
|
+
resp = await _arequest_core(
|
|
966
|
+
aclient=aclient,
|
|
967
|
+
method=method,
|
|
968
|
+
url=url,
|
|
969
|
+
params=params,
|
|
970
|
+
json_body=json_body,
|
|
971
|
+
files=files,
|
|
972
|
+
headers=headers,
|
|
973
|
+
policy=policy,
|
|
974
|
+
expected_status=expected_status,
|
|
975
|
+
timeout=timeout,
|
|
976
|
+
request_id=request_id,
|
|
690
977
|
)
|
|
978
|
+
return resp.content
|
|
691
979
|
|
|
692
|
-
# Merge headers + optional request id
|
|
693
|
-
merged_headers = dict(headers or {})
|
|
694
|
-
if request_id and "X-Request-ID" not in merged_headers:
|
|
695
|
-
merged_headers["X-Request-ID"] = request_id
|
|
696
980
|
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
981
|
+
async def arequest_text(
|
|
982
|
+
*,
|
|
983
|
+
aclient: httpx.AsyncClient,
|
|
984
|
+
method: str,
|
|
985
|
+
url: str,
|
|
986
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
987
|
+
json_body: Optional[Any] = None,
|
|
988
|
+
files: Optional[RequestFiles] = None,
|
|
989
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
990
|
+
policy: Optional[RetryPolicy] = None,
|
|
991
|
+
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
992
|
+
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
993
|
+
request_id: Optional[str] = None,
|
|
994
|
+
) -> str:
|
|
995
|
+
"""Async equivalent of :func:`request_text`."""
|
|
996
|
+
resp = await _arequest_core(
|
|
997
|
+
aclient=aclient,
|
|
998
|
+
method=method,
|
|
999
|
+
url=url,
|
|
1000
|
+
params=params,
|
|
1001
|
+
json_body=json_body,
|
|
1002
|
+
files=files,
|
|
1003
|
+
headers=headers,
|
|
1004
|
+
policy=policy,
|
|
1005
|
+
expected_status=expected_status,
|
|
1006
|
+
timeout=timeout,
|
|
1007
|
+
request_id=request_id,
|
|
1008
|
+
)
|
|
1009
|
+
return resp.text
|
|
715
1010
|
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
1011
|
+
|
|
1012
|
+
async def arequest_raw(
|
|
1013
|
+
*,
|
|
1014
|
+
aclient: httpx.AsyncClient,
|
|
1015
|
+
method: str,
|
|
1016
|
+
url: str,
|
|
1017
|
+
params: Optional[Mapping[str, Any]] = None,
|
|
1018
|
+
json_body: Optional[Any] = None,
|
|
1019
|
+
files: Optional[RequestFiles] = None,
|
|
1020
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
1021
|
+
policy: Optional[RetryPolicy] = None,
|
|
1022
|
+
expected_status: Union[int, Iterable[int]] = (200, 201, 202, 204),
|
|
1023
|
+
timeout: Optional[Union[float, httpx.Timeout]] = None,
|
|
1024
|
+
request_id: Optional[str] = None,
|
|
1025
|
+
) -> httpx.Response:
|
|
1026
|
+
"""Async equivalent of :func:`request_raw`."""
|
|
1027
|
+
return await _arequest_core(
|
|
1028
|
+
aclient=aclient,
|
|
1029
|
+
method=method,
|
|
1030
|
+
url=url,
|
|
1031
|
+
params=params,
|
|
1032
|
+
json_body=json_body,
|
|
1033
|
+
files=files,
|
|
1034
|
+
headers=headers,
|
|
1035
|
+
policy=policy,
|
|
1036
|
+
expected_status=expected_status,
|
|
1037
|
+
timeout=timeout,
|
|
1038
|
+
request_id=request_id,
|
|
1039
|
+
)
|
|
729
1040
|
|
|
730
1041
|
|
|
731
1042
|
# ----------------------
|