cernisdata 0.1.0__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.
- cernisdata/__init__.py +65 -0
- cernisdata/_client.py +975 -0
- cernisdata/_errors.py +132 -0
- cernisdata/_generated.py +2935 -0
- cernisdata/_types.py +100 -0
- cernisdata/_version.py +1 -0
- cernisdata/_webhooks.py +46 -0
- cernisdata/py.typed +0 -0
- cernisdata-0.1.0.dist-info/METADATA +127 -0
- cernisdata-0.1.0.dist-info/RECORD +11 -0
- cernisdata-0.1.0.dist-info/WHEEL +4 -0
cernisdata/_errors.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Every error the SDK raises is a CernisDataError. ``code`` is the API error code."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import builtins
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class CernisDataError(Exception):
|
|
10
|
+
"""Base class. Carries the HTTP status (0 when there was no answer), the API error code and the request id."""
|
|
11
|
+
|
|
12
|
+
status: int
|
|
13
|
+
code: str
|
|
14
|
+
message: str
|
|
15
|
+
request_id: str | None
|
|
16
|
+
details: Any
|
|
17
|
+
checkout_url: str | None
|
|
18
|
+
body: Any
|
|
19
|
+
|
|
20
|
+
def __init__(self, status: int, body: Any = None, message: str | None = None) -> None:
|
|
21
|
+
err = body.get("error") if isinstance(body, dict) and isinstance(body.get("error"), dict) else {}
|
|
22
|
+
self.status = status
|
|
23
|
+
self.body = body
|
|
24
|
+
self.code = err.get("code") or "http_error"
|
|
25
|
+
self.message = message or err.get("message") or f"HTTP {status}"
|
|
26
|
+
self.request_id = body.get("request_id") if isinstance(body, dict) else None
|
|
27
|
+
self.details = err.get("details")
|
|
28
|
+
self.checkout_url = err.get("checkout_url")
|
|
29
|
+
Exception.__init__(self, self.message)
|
|
30
|
+
|
|
31
|
+
def __str__(self) -> str:
|
|
32
|
+
return f"{self.code}: {self.message}"
|
|
33
|
+
|
|
34
|
+
def __repr__(self) -> str:
|
|
35
|
+
return f"{type(self).__name__}(status={self.status}, code={self.code!r}, message={self.message!r})"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class InvalidInputError(CernisDataError):
|
|
39
|
+
"""400: the input failed validation. ``details`` lists {field, message}."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class AuthenticationError(CernisDataError):
|
|
43
|
+
"""401: missing, wrong or revoked API key."""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class InsufficientBalanceError(CernisDataError):
|
|
47
|
+
"""402: not enough balance. ``checkout_url`` tops up the wallet."""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class PermissionDeniedError(CernisDataError):
|
|
51
|
+
"""403: the key's scopes do not allow this call."""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class NotFoundError(CernisDataError):
|
|
55
|
+
"""404."""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ConflictError(CernisDataError):
|
|
59
|
+
"""409 / 422: an idempotency key conflict or an expired quote."""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class RateLimitError(CernisDataError):
|
|
63
|
+
"""429: slow down. ``retry_after`` is in seconds when the API sent one."""
|
|
64
|
+
|
|
65
|
+
retry_after: float | None = None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class EndpointNotAvailableError(CernisDataError):
|
|
69
|
+
"""501: the endpoint has no live data route yet."""
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class TimeoutError(CernisDataError, builtins.TimeoutError): # noqa: A001
|
|
73
|
+
"""504 from the API, or the SDK gave up waiting (status 0). Also a builtin TimeoutError."""
|
|
74
|
+
|
|
75
|
+
def __init__(self, status: int, body: Any = None, message: str | None = None) -> None:
|
|
76
|
+
CernisDataError.__init__(self, status, body, message)
|
|
77
|
+
|
|
78
|
+
def __str__(self) -> str:
|
|
79
|
+
return CernisDataError.__str__(self)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class ServerError(CernisDataError):
|
|
83
|
+
"""5xx: provider unavailable, no qualified route, or an internal error. You were not charged."""
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class APIConnectionError(CernisDataError):
|
|
87
|
+
"""The request never got an answer: DNS, TLS, connection reset."""
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class WebhookVerificationError(CernisDataError):
|
|
91
|
+
"""A webhook failed signature verification."""
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
_BY_CODE: dict[str, type[CernisDataError]] = {
|
|
95
|
+
"invalid_input": InvalidInputError,
|
|
96
|
+
"unauthorized": AuthenticationError,
|
|
97
|
+
"insufficient_balance": InsufficientBalanceError,
|
|
98
|
+
"trial_expired": InsufficientBalanceError,
|
|
99
|
+
"forbidden": PermissionDeniedError,
|
|
100
|
+
"not_found": NotFoundError,
|
|
101
|
+
"quote_expired": ConflictError,
|
|
102
|
+
"idempotency_in_progress": ConflictError,
|
|
103
|
+
"idempotency_key_reused": ConflictError,
|
|
104
|
+
"rate_limited": RateLimitError,
|
|
105
|
+
"endpoint_not_available": EndpointNotAvailableError,
|
|
106
|
+
"deadline_exceeded": TimeoutError,
|
|
107
|
+
"provider_unavailable": ServerError,
|
|
108
|
+
"no_qualified_route": ServerError,
|
|
109
|
+
"internal_error": ServerError,
|
|
110
|
+
}
|
|
111
|
+
_BY_STATUS: dict[int, type[CernisDataError]] = {
|
|
112
|
+
400: InvalidInputError,
|
|
113
|
+
401: AuthenticationError,
|
|
114
|
+
402: InsufficientBalanceError,
|
|
115
|
+
403: PermissionDeniedError,
|
|
116
|
+
404: NotFoundError,
|
|
117
|
+
409: ConflictError,
|
|
118
|
+
422: ConflictError,
|
|
119
|
+
429: RateLimitError,
|
|
120
|
+
501: EndpointNotAvailableError,
|
|
121
|
+
504: TimeoutError,
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def error_from_response(status: int, body: Any, retry_after: float | None = None) -> CernisDataError:
|
|
126
|
+
"""Build the right error subclass from an API error response."""
|
|
127
|
+
code = body.get("error", {}).get("code") if isinstance(body, dict) and isinstance(body.get("error"), dict) else None
|
|
128
|
+
cls = _BY_CODE.get(code or "") or _BY_STATUS.get(status) or (ServerError if status >= 500 else CernisDataError)
|
|
129
|
+
err = cls(status, body)
|
|
130
|
+
if isinstance(err, RateLimitError):
|
|
131
|
+
err.retry_after = retry_after
|
|
132
|
+
return err
|