hyperapi-sdk 0.7.1__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.
- hyperapi/__init__.py +48 -0
- hyperapi/async_client.py +2181 -0
- hyperapi/client.py +2947 -0
- hyperapi/exceptions.py +250 -0
- hyperapi/py.typed +0 -0
- hyperapi_sdk-0.7.1.dist-info/METADATA +906 -0
- hyperapi_sdk-0.7.1.dist-info/RECORD +11 -0
- hyperapi_sdk-0.7.1.dist-info/WHEEL +5 -0
- hyperapi_sdk-0.7.1.dist-info/licenses/LICENSE +202 -0
- hyperapi_sdk-0.7.1.dist-info/licenses/NOTICE +4 -0
- hyperapi_sdk-0.7.1.dist-info/top_level.txt +1 -0
hyperapi/exceptions.py
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"""HyperAPI Exceptions.
|
|
2
|
+
|
|
3
|
+
The taxonomy is intentionally narrow. Every error carries `status_code` and
|
|
4
|
+
`request_id` so callers can branch on the specifics without a separate class
|
|
5
|
+
for every server-side failure mode.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Covers every issued key family: external `hk_live_*` / `hk_test_*`, plus
|
|
14
|
+
# service-account `hk_service_*` (per the platform's four auth strategies). A
|
|
15
|
+
# secret scrubber must fail safe — a new prefix should be added here the moment
|
|
16
|
+
# it ships, since an un-scrubbed key in an error body is a credential leak.
|
|
17
|
+
_API_KEY_PATTERN = re.compile(r"hk_(?:live|test|service)_[A-Za-z0-9_\-]+")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _strip_api_key(text: object) -> str | None:
|
|
21
|
+
"""Scrub `hk_live_*` / `hk_test_*` / `hk_service_*` substrings from server
|
|
22
|
+
messages before they end up in exception text. Defends against the rare
|
|
23
|
+
server response that echoes the request header into the error body.
|
|
24
|
+
|
|
25
|
+
Non-string input (a server that sends ``error`` as an object) is
|
|
26
|
+
converted with ``str()`` first, so it is scrubbed too instead of raising.
|
|
27
|
+
"""
|
|
28
|
+
if text is None:
|
|
29
|
+
return None
|
|
30
|
+
if not isinstance(text, str):
|
|
31
|
+
text = str(text)
|
|
32
|
+
if not text:
|
|
33
|
+
return text
|
|
34
|
+
return _API_KEY_PATTERN.sub("hk_***redacted", text)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _scrub_details(value: object) -> object:
|
|
38
|
+
"""Scrub API keys from every string inside ``error.details`` (nested dicts
|
|
39
|
+
and lists included). Non-string leaves are kept as they are."""
|
|
40
|
+
if isinstance(value, str):
|
|
41
|
+
return _strip_api_key(value)
|
|
42
|
+
if isinstance(value, dict):
|
|
43
|
+
return {k: _scrub_details(v) for k, v in value.items()}
|
|
44
|
+
if isinstance(value, (list, tuple)):
|
|
45
|
+
return [_scrub_details(v) for v in value]
|
|
46
|
+
return value
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _rebuild_error(cls: type, message: str) -> HyperAPIError:
|
|
50
|
+
"""Unpickle helper: recreate ``cls`` without running its ``__init__``.
|
|
51
|
+
|
|
52
|
+
Subclasses take keyword-only arguments (``job_id``, ``elapsed_s``, ...),
|
|
53
|
+
so the default ``cls(*args)`` unpickling would fail. The instance
|
|
54
|
+
attributes are restored from the pickled ``__dict__`` afterwards.
|
|
55
|
+
"""
|
|
56
|
+
err = cls.__new__(cls)
|
|
57
|
+
Exception.__init__(err, message)
|
|
58
|
+
return err
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class HyperAPIError(Exception):
|
|
62
|
+
"""Base exception for HyperAPI errors.
|
|
63
|
+
|
|
64
|
+
Attributes:
|
|
65
|
+
message: Human-readable description.
|
|
66
|
+
status_code: HTTP status code if the error originated from an HTTP
|
|
67
|
+
response; None for client-side errors.
|
|
68
|
+
request_id: The X-Request-ID we sent for the originating call. Use
|
|
69
|
+
this when filing a support ticket — the API's logs are keyed
|
|
70
|
+
by it.
|
|
71
|
+
code: The machine-readable ``error.code`` from the API's error
|
|
72
|
+
envelope (``{"error": {"code", "message", "details"?}}``), when
|
|
73
|
+
the response carried one; None for client-side errors or bodies
|
|
74
|
+
without an envelope. Stable across wording changes, so branch on
|
|
75
|
+
this rather than on ``message``.
|
|
76
|
+
details: The optional ``error.details`` object — the numbers behind
|
|
77
|
+
a refusal (for a quota refusal: ``pages_used``, ``pages_limit``,
|
|
78
|
+
``pages_pending``). None when the API sent none.
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def __init__(
|
|
82
|
+
self,
|
|
83
|
+
message: str,
|
|
84
|
+
status_code: int | None = None,
|
|
85
|
+
request_id: str | None = None,
|
|
86
|
+
*,
|
|
87
|
+
code: str | None = None,
|
|
88
|
+
details: dict | None = None,
|
|
89
|
+
):
|
|
90
|
+
self.message = _strip_api_key(message) or message
|
|
91
|
+
self.status_code = status_code
|
|
92
|
+
self.request_id = request_id
|
|
93
|
+
self.code = code
|
|
94
|
+
# A server that echoes the request header inside details must not leak the key.
|
|
95
|
+
self.details = _scrub_details(details) if details is not None else None
|
|
96
|
+
super().__init__(self.message)
|
|
97
|
+
|
|
98
|
+
def __reduce__(self):
|
|
99
|
+
# Exceptions cross process boundaries (multiprocessing, concurrent.futures,
|
|
100
|
+
# Celery); every attribute must survive the round trip.
|
|
101
|
+
return (_rebuild_error, (type(self), self.message), self.__dict__.copy())
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class AuthenticationError(HyperAPIError):
|
|
105
|
+
"""Raised when the API key is missing, malformed, or rejected by the server (401)."""
|
|
106
|
+
pass
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class RateLimitError(HyperAPIError):
|
|
110
|
+
"""Raised when the server returns 429 Too Many Requests.
|
|
111
|
+
|
|
112
|
+
A 429 means one of two things, and ``code`` tells them apart:
|
|
113
|
+
|
|
114
|
+
- A **transient rate limit** (requests per window). Wait ``retry_after``
|
|
115
|
+
seconds and retry — the server says when the window reopens. The SDK's
|
|
116
|
+
polling loops do this for you.
|
|
117
|
+
- The **monthly page quota is exhausted** (``code ==
|
|
118
|
+
"page_quota_exceeded"``, ``is_quota_exhausted`` is True). Retrying does
|
|
119
|
+
not help; the quota resets at the next billing period or on a plan
|
|
120
|
+
upgrade. The SDK's polling loops raise this immediately instead of
|
|
121
|
+
backing off.
|
|
122
|
+
|
|
123
|
+
Attributes:
|
|
124
|
+
retry_after: Seconds to wait before retrying (parsed from the
|
|
125
|
+
`Retry-After` response header).
|
|
126
|
+
tier: The plan tier on the API key (e.g., "free", "pro", "enterprise").
|
|
127
|
+
limit: The numeric limit that was exceeded (per the configured window).
|
|
128
|
+
window_seconds: Length of the sliding window the server's rate limiter
|
|
129
|
+
uses, in seconds. ``limit`` is "N requests per ``window_seconds``."
|
|
130
|
+
Not every response carries it; it stays ``None`` when absent.
|
|
131
|
+
degraded: True when the server reports temporary rate-limit service
|
|
132
|
+
degradation rather than a customer exceeding their configured
|
|
133
|
+
quota. Treat it as transient and retry after ``retry_after``.
|
|
134
|
+
code: See :class:`HyperAPIError`. ``"page_quota_exceeded"`` marks the
|
|
135
|
+
quota case; ``details`` then carries ``pages_used``,
|
|
136
|
+
``pages_limit`` and ``pages_pending`` when the API reports them.
|
|
137
|
+
"""
|
|
138
|
+
|
|
139
|
+
QUOTA_EXHAUSTED_CODE = "page_quota_exceeded"
|
|
140
|
+
|
|
141
|
+
def __init__(
|
|
142
|
+
self,
|
|
143
|
+
message: str = "Rate limit exceeded",
|
|
144
|
+
*,
|
|
145
|
+
retry_after: int = 60,
|
|
146
|
+
tier: str | None = None,
|
|
147
|
+
limit: int | None = None,
|
|
148
|
+
window_seconds: int | None = None,
|
|
149
|
+
degraded: bool = False,
|
|
150
|
+
status_code: int | None = 429,
|
|
151
|
+
request_id: str | None = None,
|
|
152
|
+
code: str | None = None,
|
|
153
|
+
details: dict | None = None,
|
|
154
|
+
):
|
|
155
|
+
super().__init__(
|
|
156
|
+
message,
|
|
157
|
+
status_code=status_code,
|
|
158
|
+
request_id=request_id,
|
|
159
|
+
code=code,
|
|
160
|
+
details=details,
|
|
161
|
+
)
|
|
162
|
+
self.retry_after = retry_after
|
|
163
|
+
self.tier = tier
|
|
164
|
+
self.limit = limit
|
|
165
|
+
self.window_seconds = window_seconds
|
|
166
|
+
self.degraded = degraded
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def is_quota_exhausted(self) -> bool:
|
|
170
|
+
"""True when this 429 is the monthly page quota, not a rate window.
|
|
171
|
+
|
|
172
|
+
Waiting ``retry_after`` will not clear it; do not retry automatically.
|
|
173
|
+
"""
|
|
174
|
+
return self.code == self.QUOTA_EXHAUSTED_CODE
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class JobTimeoutError(HyperAPIError):
|
|
178
|
+
"""Raised when a polling loop exceeds its `poll_timeout` budget.
|
|
179
|
+
|
|
180
|
+
The job is still running on the server — the SDK has just stopped waiting.
|
|
181
|
+
Customers can resume by passing the `job_id` to `client.get_job()` or
|
|
182
|
+
`client.wait_for_job()` with a larger timeout.
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
def __init__(
|
|
186
|
+
self,
|
|
187
|
+
message: str,
|
|
188
|
+
*,
|
|
189
|
+
job_id: str,
|
|
190
|
+
elapsed_s: float,
|
|
191
|
+
request_id: str | None = None,
|
|
192
|
+
):
|
|
193
|
+
super().__init__(message, status_code=None, request_id=request_id)
|
|
194
|
+
self.job_id = job_id
|
|
195
|
+
self.elapsed_s = elapsed_s
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
class JobCancelledError(HyperAPIError):
|
|
199
|
+
"""Raised by a polling loop when the job was cancelled (``DELETE /v1/jobs/{id}``).
|
|
200
|
+
|
|
201
|
+
The job's own status report does not use a separate ``"cancelled"``
|
|
202
|
+
state: ``GET /v1/jobs/{id}`` returns ``status="failed"`` with an
|
|
203
|
+
``error`` that begins ``"cancelled:"``. The SDK decodes that into this
|
|
204
|
+
exception so a deliberate cancel is never mistaken for a processing
|
|
205
|
+
failure. ``list_recent_jobs()`` summaries do show such jobs as
|
|
206
|
+
``status="cancelled"``.
|
|
207
|
+
|
|
208
|
+
Attributes:
|
|
209
|
+
job_id: The cancelled job.
|
|
210
|
+
"""
|
|
211
|
+
|
|
212
|
+
def __init__(
|
|
213
|
+
self,
|
|
214
|
+
message: str,
|
|
215
|
+
*,
|
|
216
|
+
job_id: str,
|
|
217
|
+
request_id: str | None = None,
|
|
218
|
+
):
|
|
219
|
+
super().__init__(message, status_code=None, request_id=request_id)
|
|
220
|
+
self.job_id = job_id
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
class ParseError(HyperAPIError):
|
|
224
|
+
"""Raised when document parsing fails (sync HTTP error or job status=failed)."""
|
|
225
|
+
pass
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
class ExtractError(HyperAPIError):
|
|
229
|
+
"""Raised when field extraction fails (sync HTTP error or job status=failed)."""
|
|
230
|
+
pass
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
class ClassifyError(HyperAPIError):
|
|
234
|
+
"""Raised when document classification fails (sync HTTP error or job status=failed)."""
|
|
235
|
+
pass
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
class SplitError(HyperAPIError):
|
|
239
|
+
"""Raised when document splitting fails (sync HTTP error or job status=failed)."""
|
|
240
|
+
pass
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class RedactError(HyperAPIError):
|
|
244
|
+
"""Raised when redaction/deidentification fails (sync HTTP error or job status=failed)."""
|
|
245
|
+
pass
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class DocumentUploadError(HyperAPIError):
|
|
249
|
+
"""Raised when the presigned-URL flow fails (presigned fetch or S3 PUT)."""
|
|
250
|
+
pass
|
hyperapi/py.typed
ADDED
|
File without changes
|