-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
473 lines (412 loc) · 16.5 KB
/
client.py
File metadata and controls
473 lines (412 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
"""This module provides an API client to invoke APIs deployed on the Unstract
platform.
Classes:
APIDeploymentsClient: A class to invoke APIs deployed on the Unstract platform.
APIDeploymentsClientException: A class to handle exceptions raised by the
APIDeploymentsClient class.
"""
import logging
import ntpath
import os
import time
from urllib.parse import urlparse
import requests
from requests.exceptions import ConnectionError, JSONDecodeError, Timeout
from tenacity import (
RetryCallState,
Retrying,
retry_if_exception_type,
retry_if_result,
stop_after_attempt,
wait_exponential_jitter,
)
from tenacity.wait import wait_base
from unstract.api_deployments.utils import UnstractUtils
class APIDeploymentsClientException(Exception):
"""A class to handle exceptions raised by the APIClient class."""
def __init__(self, message):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def error_message(self):
return self.value
class _WaitRetryAfterOrExponentialJitter(wait_base):
"""Wait strategy that respects Retry-After on 429, else exponential jitter.
For 429 responses with a valid ``Retry-After`` header the server-requested
delay is used. In every other case the strategy delegates to
``wait_exponential_jitter`` (additive jitter).
"""
def __init__(
self,
initial: float,
max: float,
exp_base: float,
jitter: float,
) -> None:
super().__init__()
self._exp_jitter = wait_exponential_jitter(
initial=initial, max=max, exp_base=exp_base, jitter=jitter
)
def __call__(self, retry_state: RetryCallState) -> float:
outcome = retry_state.outcome
if outcome and not outcome.failed:
response = outcome.result()
if response is not None and getattr(response, "status_code", None) == 429:
retry_after = response.headers.get("Retry-After")
if retry_after is not None:
try:
return float(retry_after)
except (ValueError, TypeError):
pass
return self._exp_jitter(retry_state)
class APIDeploymentsClient:
"""A class to invoke APIs deployed on the Unstract platform."""
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
log_stream_handler = logging.StreamHandler()
log_stream_handler.setFormatter(formatter)
logger.addHandler(log_stream_handler)
api_key = ""
api_timeout = 300
in_progress_statuses = ["PENDING", "EXECUTING", "READY", "QUEUED", "INITIATED"]
def __init__(
self,
api_url: str,
api_key: str,
api_timeout: int = 300,
logging_level: str = "INFO",
include_metadata: bool = False,
verify: bool = True,
max_retries: int = 4,
initial_delay: float = 2.0,
max_delay: float = 60.0,
backoff_factor: float = 2.0,
jitter: float = 1.0,
):
"""Initializes the APIClient class.
Args:
api_key (str): The API key to authenticate the API request.
api_timeout (int): The timeout to wait for the API response.
logging_level (str): The logging level to log messages.
max_retries (int): Maximum number of retry attempts for failed requests.
initial_delay (float): Initial delay in seconds before the first retry.
max_delay (float): Maximum delay in seconds between retries.
backoff_factor (float): Multiplier applied to delay for each retry.
jitter (float): Maximum additive jitter in seconds added to each delay.
"""
if logging_level == "":
logging_level = os.getenv("UNSTRACT_API_CLIENT_LOGGING_LEVEL", "INFO")
if logging_level == "DEBUG":
self.logger.setLevel(logging.DEBUG)
elif logging_level == "INFO":
self.logger.setLevel(logging.INFO)
elif logging_level == "WARNING":
self.logger.setLevel(logging.WARNING)
elif logging_level == "ERROR":
self.logger.setLevel(logging.ERROR)
# self.logger.setLevel(logging_level)
self.logger.debug("Logging level set to: " + logging_level)
if api_key == "":
self.api_key = os.getenv("UNSTRACT_API_DEPLOYMENT_KEY", "")
else:
self.api_key = api_key
self.logger.debug("API key set to: " + UnstractUtils.redact_key(self.api_key))
self.api_timeout = api_timeout
self.api_url = api_url
self.__save_base_url(api_url)
self.include_metadata = include_metadata
self.verify = verify
self.max_retries = max_retries
self.initial_delay = initial_delay
self.max_delay = max_delay
self.backoff_factor = backoff_factor
self.jitter = jitter
def _is_retryable_status(self, status_code: int) -> bool:
"""Checks whether a status code should trigger a retry.
Args:
status_code (int): The HTTP status code to check.
Returns:
bool: True if the request should be retried.
"""
return status_code >= 500 or status_code == 429
def __save_base_url(self, full_url: str):
"""Extracts the base URL from the full URL and saves it.
Args:
full_url (str): The full URL of the API.
"""
parsed_url = urlparse(full_url)
self.base_url = parsed_url.scheme + "://" + parsed_url.netloc
self.logger.debug("Base URL: " + self.base_url)
@staticmethod
def _rewind_files(files):
"""Rewinds file objects so they can be re-sent on retry."""
for file_tuple in files:
file_obj = file_tuple[1]
if hasattr(file_obj, "seek"):
file_obj.seek(0)
elif isinstance(file_obj, tuple) and len(file_obj) >= 2:
if hasattr(file_obj[1], "seek"):
file_obj[1].seek(0)
def _request_with_retry(self, method: str, url: str, **kwargs) -> requests.Response:
"""Makes an HTTP request with exponential backoff retry logic.
Uses ``tenacity`` with additive jitter and Retry-After support.
Args:
method (str): The HTTP method (e.g., "GET", "POST").
url (str): The request URL.
**kwargs: Additional keyword arguments passed to requests.request().
Returns:
requests.Response: The response from the request.
Raises:
ConnectionError: If a connection error persists after all retries.
Timeout: If a timeout persists after all retries.
"""
files = kwargs.get("files")
def _before_sleep(retry_state: RetryCallState):
attempt = retry_state.attempt_number
delay = retry_state.next_action.sleep
outcome = retry_state.outcome
if outcome.failed:
exc = outcome.exception()
self.logger.warning(
"%s during request to %s. Retrying in %.1fs (attempt %d/%d).",
type(exc).__name__,
url,
delay,
attempt,
self.max_retries,
)
else:
response = outcome.result()
self.logger.warning(
"Request to %s returned %d. Retrying in %.1fs (attempt %d/%d).",
url,
response.status_code,
delay,
attempt,
self.max_retries,
)
# Rewind file objects before next attempt
if files:
self._rewind_files(files)
def _retry_error_callback(retry_state: RetryCallState):
outcome = retry_state.outcome
if outcome.failed:
exc = outcome.exception()
self.logger.warning(
"%s during request to %s. Retries exhausted (%d/%d).",
type(exc).__name__,
url,
self.max_retries,
self.max_retries,
)
raise exc
response = outcome.result()
self.logger.warning(
"Request to %s returned %d. Retries exhausted (%d/%d).",
url,
response.status_code,
self.max_retries,
self.max_retries,
)
return response
retrier = Retrying(
stop=stop_after_attempt(self.max_retries + 1),
wait=_WaitRetryAfterOrExponentialJitter(
initial=self.initial_delay,
max=self.max_delay,
exp_base=self.backoff_factor,
jitter=self.jitter,
),
retry=(
retry_if_result(lambda r: self._is_retryable_status(r.status_code))
| retry_if_exception_type((ConnectionError, Timeout))
),
before_sleep=_before_sleep,
retry_error_callback=_retry_error_callback,
sleep=time.sleep,
reraise=False,
)
return retrier(requests.request, method, url, **kwargs)
def structure_file(self, file_paths: list[str]) -> dict:
"""Invokes the API deployed on the Unstract platform.
Args:
file_paths (list[str]): The file path to the file to be uploaded.
Returns:
dict: The response from the API.
"""
self.logger.debug("Invoking API: " + self.api_url)
self.logger.debug("File paths: " + str(file_paths))
headers = {
"Authorization": "Bearer " + self.api_key,
}
form_data = {
"timeout": self.api_timeout,
"include_metadata": self.include_metadata,
}
files = []
try:
for file_path in file_paths:
record = (
"files",
(
ntpath.basename(file_path),
open(file_path, "rb"),
"application/octet-stream",
),
)
files.append(record)
except FileNotFoundError as e:
raise APIDeploymentsClientException("File not found: " + str(e))
if self.api_timeout == 0:
# Async mode: server returns immediately after queuing.
# A 5xx means queuing failed — safe to retry.
response = self._request_with_retry(
"POST",
self.api_url,
headers=headers,
data=form_data,
files=files,
verify=self.verify,
)
else:
# Sync mode: server blocks during processing.
# A 5xx may mean it processed but response was lost — don't retry
# to avoid duplicate executions.
response = requests.post(
self.api_url,
headers=headers,
data=form_data,
files=files,
verify=self.verify,
)
self.logger.debug(response.status_code)
self.logger.debug(response.text)
# The returned object is wrapped in a "message" key.
# Let's simplify the response.
obj_to_return = {}
try:
response_data = response.json()
response_message = response_data.get("message", {})
except JSONDecodeError:
self.logger.error(
"Failed to decode JSON response. Raw response: %s",
response.text,
exc_info=True,
)
obj_to_return = {
"status_code": response.status_code,
"pending": False,
"execution_status": "",
"error": "Invalid JSON response from API",
"extraction_result": "",
}
return obj_to_return
if response.status_code == 401:
obj_to_return = {
"status_code": response.status_code,
"pending": False,
"execution_status": "",
"error": response_data.get("errors", [{}])[0].get(
"detail", "Unauthorized"
),
"extraction_result": "",
}
return obj_to_return
# If the execution status is pending, extract the execution ID from
# the response and return it in the response.
# Later, users can use the execution ID to check the status of the execution.
# The returned object is wrapped in a "message" key.
# Let's simplify the response.
# Construct response object
execution_status = response_message.get("execution_status", "")
error_message = response_message.get("error", "")
extraction_result = response_message.get("result", "")
status_api_endpoint = response_message.get("status_api")
obj_to_return = {
"status_code": response.status_code,
"pending": False,
"execution_status": execution_status,
"error": error_message,
"extraction_result": extraction_result,
}
# Check if the status is pending or if it's successful but lacks a result.
# The POST endpoint returns 200 for successful queuing (including
# PENDING/EXECUTING) and 422 only on setup errors — guard against
# incorrectly polling after an error response.
if 200 <= response.status_code < 300:
if execution_status in self.in_progress_statuses or (
execution_status == "SUCCESS" and not extraction_result
):
obj_to_return.update(
{
"status_check_api_endpoint": status_api_endpoint,
"pending": True,
}
)
return obj_to_return
def check_execution_status(self, status_check_api_endpoint: str) -> dict:
"""Checks the status of the execution.
Args:
status_check_api_endpoint (str):
The API endpoint to check the status of the execution.
Returns:
dict: The response from the API.
"""
headers = {
"Authorization": "Bearer " + self.api_key,
}
status_call_url = self.base_url + status_check_api_endpoint
self.logger.debug("Checking execution status via endpoint: " + status_call_url)
response = self._request_with_retry(
"GET",
status_call_url,
headers=headers,
params={"include_metadata": self.include_metadata},
verify=self.verify,
)
self.logger.debug(response.status_code)
self.logger.debug(response.text)
obj_to_return = {}
try:
response_data = response.json()
except JSONDecodeError:
self.logger.error(
"Failed to decode JSON response. Raw response: %s",
response.text,
exc_info=True,
)
obj_to_return = {
"status_code": response.status_code,
"pending": False,
"execution_status": "",
"error": "Invalid JSON response from API",
"extraction_result": "",
}
return obj_to_return
# Construct response object
execution_status = response_data.get("status", "")
error_message = response_data.get("error", "")
extraction_result = response_data.get("message", "")
obj_to_return = {
"status_code": response.status_code,
"pending": False,
"execution_status": execution_status,
"error": error_message,
"extraction_result": extraction_result,
}
# If the execution status is pending, extract the execution ID from the response
# and return it in the response.
# Later, users can use the execution ID to check the status of the execution.
if obj_to_return["execution_status"] in self.in_progress_statuses:
obj_to_return["pending"] = True
elif self._is_retryable_status(response.status_code):
obj_to_return["pending"] = True
self.logger.warning(
"Status check returned %d after retries; "
"marking as pending to continue polling.",
response.status_code,
)
return obj_to_return