tango-python 1.18.0__py3-none-any.whl → 1.18.2__py3-none-any.whl
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.
- tango/__init__.py +1 -1
- tango/client.py +31 -1
- tango/exceptions.py +10 -4
- {tango_python-1.18.0.dist-info → tango_python-1.18.2.dist-info}/METADATA +1 -1
- {tango_python-1.18.0.dist-info → tango_python-1.18.2.dist-info}/RECORD +8 -8
- {tango_python-1.18.0.dist-info → tango_python-1.18.2.dist-info}/WHEEL +0 -0
- {tango_python-1.18.0.dist-info → tango_python-1.18.2.dist-info}/entry_points.txt +0 -0
- {tango_python-1.18.0.dist-info → tango_python-1.18.2.dist-info}/licenses/LICENSE +0 -0
tango/__init__.py
CHANGED
tango/client.py
CHANGED
|
@@ -75,6 +75,31 @@ from tango.shapes import (
|
|
|
75
75
|
_COUNT_TYPE_KEY = "_tango_count_type"
|
|
76
76
|
|
|
77
77
|
|
|
78
|
+
def _error_body(response: httpx.Response) -> dict[str, Any]:
|
|
79
|
+
"""The JSON object an error response carries, or {} for an empty, non-JSON (a proxy's HTML page) or non-object body."""
|
|
80
|
+
try:
|
|
81
|
+
data = response.json() if response.content else {}
|
|
82
|
+
except ValueError:
|
|
83
|
+
return {}
|
|
84
|
+
return data if isinstance(data, dict) else {}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _error_reason(error_data: dict[str, Any]) -> str | None:
|
|
88
|
+
"""The server's own explanation from an error body, e.g. why a 403 refused the request.
|
|
89
|
+
|
|
90
|
+
The API sends either ``{"detail": "..."}`` or a structured body such as a tier gate's ``{"error": "...", "code": "tier_required", ...}``, sometimes nested under ``detail``.
|
|
91
|
+
"""
|
|
92
|
+
for body in (error_data, error_data.get("detail")):
|
|
93
|
+
if isinstance(body, str) and body:
|
|
94
|
+
return body
|
|
95
|
+
if isinstance(body, dict):
|
|
96
|
+
for key in ("error", "message", "detail"):
|
|
97
|
+
value = body.get(key)
|
|
98
|
+
if isinstance(value, str) and value:
|
|
99
|
+
return value
|
|
100
|
+
return None
|
|
101
|
+
|
|
102
|
+
|
|
78
103
|
class TangoClient:
|
|
79
104
|
"""Tango API Client"""
|
|
80
105
|
|
|
@@ -232,8 +257,13 @@ class TangoClient:
|
|
|
232
257
|
detail = error_data.get("detail", "Rate limit exceeded")
|
|
233
258
|
raise TangoRateLimitError(detail, response.status_code, error_data)
|
|
234
259
|
elif not response.is_success:
|
|
260
|
+
error_data = _error_body(response)
|
|
261
|
+
reason = _error_reason(error_data)
|
|
262
|
+
message = f"API request failed with status {response.status_code}"
|
|
235
263
|
raise TangoAPIError(
|
|
236
|
-
f"
|
|
264
|
+
f"{message}: {reason}" if reason else message,
|
|
265
|
+
response.status_code,
|
|
266
|
+
error_data,
|
|
237
267
|
)
|
|
238
268
|
|
|
239
269
|
def _get(self, endpoint: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
|
tango/exceptions.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"""Tango API exceptions"""
|
|
2
2
|
|
|
3
|
+
import re
|
|
3
4
|
from typing import Any
|
|
4
5
|
|
|
6
|
+
# The API names the tripped rate in its 429 detail: "Rate limit exceeded for daily. Please try again in 16 hours, 58 minutes."
|
|
7
|
+
_RATE_NAME = re.compile(r"exceeded for ([\w-]+)", re.IGNORECASE)
|
|
8
|
+
|
|
5
9
|
|
|
6
10
|
class TangoAPIError(Exception):
|
|
7
11
|
"""Base exception for Tango API errors"""
|
|
@@ -88,11 +92,13 @@ class TangoRateLimitError(TangoAPIError):
|
|
|
88
92
|
d = self.detail
|
|
89
93
|
if not d:
|
|
90
94
|
return None
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if "daily" in
|
|
95
|
+
# Read the rate's name, not the whole sentence: a daily lockout's wait reads "16 hours, 58 minutes", which a match on "minute" took for burst.
|
|
96
|
+
match = _RATE_NAME.search(d)
|
|
97
|
+
text = match.group(1).lower() if match else d.lower()
|
|
98
|
+
if "daily" in text or re.search(r"\bday\b", text):
|
|
95
99
|
return "daily"
|
|
100
|
+
if "burst" in text or "minute" in text:
|
|
101
|
+
return "burst"
|
|
96
102
|
return None
|
|
97
103
|
|
|
98
104
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
tango/__init__.py,sha256=
|
|
2
|
-
tango/client.py,sha256=
|
|
3
|
-
tango/exceptions.py,sha256=
|
|
1
|
+
tango/__init__.py,sha256=EcmM-sPf6gUtZML4RQbgS31RPAi4oz91N6XIIvpRjbk,2739
|
|
2
|
+
tango/client.py,sha256=awauFCq-gd-dCBAvDPeEBBgxwhuc_2ZzIhK-I7ki0U8,245711
|
|
3
|
+
tango/exceptions.py,sha256=TL2MrSbcy99ogrJugLiP1I5J5qNih770knISycDTx-g,4661
|
|
4
4
|
tango/models.py,sha256=D9e8_AAAwVi1C4nOqdhWz-8ARBLPRsLGi8TDjucOMEY,72897
|
|
5
5
|
tango/shapes/__init__.py,sha256=7ea1WU74jp4znhNw-gXruag6m6eyPZtbVgbDFmFUWro,1072
|
|
6
6
|
tango/shapes/explicit_schemas.py,sha256=fwjHiFLOMpFyNLNM4M6kdK603s-t8gxHYqcSeL3jz7g,85674
|
|
@@ -16,8 +16,8 @@ tango/webhooks/cli.py,sha256=f_vQbJ2AeSQjKnQo7MxHFyUB2SAKcgHYmGQULS0isqQ,13858
|
|
|
16
16
|
tango/webhooks/receiver.py,sha256=5yhsVhlLyoxmOCGvmbynWAIlDB2OaCPVf1H4GA1SxmU,9279
|
|
17
17
|
tango/webhooks/signing.py,sha256=92Ee-0B6PR7ZkvY3Np3gzl88-mtfKkh-I7lxqCe2lGw,2374
|
|
18
18
|
tango/webhooks/simulate.py,sha256=g2Osa0FYU5mJuon07T2aUCtmkUoTEzsY261tlp76fF0,3165
|
|
19
|
-
tango_python-1.18.
|
|
20
|
-
tango_python-1.18.
|
|
21
|
-
tango_python-1.18.
|
|
22
|
-
tango_python-1.18.
|
|
23
|
-
tango_python-1.18.
|
|
19
|
+
tango_python-1.18.2.dist-info/METADATA,sha256=0kfKbSpvkJVxOpXsF7jerdb3dDyqc81PSAxbQRPN8fQ,22824
|
|
20
|
+
tango_python-1.18.2.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
21
|
+
tango_python-1.18.2.dist-info/entry_points.txt,sha256=kGLUbglUjuaAqEFvOZ1QuSW0vWb6VeSpCIFKaOFkKoQ,50
|
|
22
|
+
tango_python-1.18.2.dist-info/licenses/LICENSE,sha256=j2kYVHMwTkoGn3ZNScnrdIueG0k2XzB_LCPFoyBc2wk,1064
|
|
23
|
+
tango_python-1.18.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|