-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneric_client.py
More file actions
245 lines (201 loc) · 8.26 KB
/
generic_client.py
File metadata and controls
245 lines (201 loc) · 8.26 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
import logging
import time
from urllib.parse import parse_qs, urlparse
import requests
from .client import ApiHubClientException
class GenericUnstractClient:
"""
Client for interacting with generic Unstract APIs.
Handles dynamic endpoint processing operations including file upload
and result retrieval using execution_id tracking.
"""
logger = logging.getLogger(__name__)
def __init__(
self,
api_key: str,
base_url: str,
) -> None:
"""
Initialize the GenericUnstractClient.
Args:
api_key: API key for authentication
base_url: Base URL of the Unstract service
"""
self.api_key = api_key
self.base_url = base_url.rstrip("/")
self.headers = {"apikey": self.api_key}
def _extract_execution_id_from_url(self, url: str) -> str | None:
"""
Extract execution_id from a URL's query parameters.
Args:
url: URL containing execution_id parameter
Returns:
str | None: The execution_id if found, None otherwise
"""
try:
parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)
execution_ids = query_params.get("execution_id")
if execution_ids:
return execution_ids[0] # Get the first value
except Exception as e:
self.logger.warning("Failed to extract execution_id from URL: %s", e)
return None
def process(
self,
endpoint: str,
file_path: str,
wait_for_completion: bool = False,
polling_interval: int = 5,
timeout: int = 600,
) -> dict:
"""
Process a document using the specified endpoint.
Args:
endpoint: The endpoint name (e.g., 'invoice', 'contract', 'receipt')
file_path: Path to the file to upload
wait_for_completion: If True, polls for completion and returns final result
polling_interval: Seconds to wait between status checks (default: 5)
timeout: Maximum time to wait for completion in seconds (default: 600)
Returns:
dict: Response containing execution_id and processing information
Raises:
ApiHubClientException: If upload fails
"""
url = f"{self.base_url}/api/v1/{endpoint}"
self.logger.info("Processing file with endpoint %s: %s", endpoint, file_path)
try:
with open(file_path, "rb") as file:
files = {"files": file}
response = requests.post(url, headers=self.headers, files=files)
except FileNotFoundError as e:
raise ApiHubClientException(f"File not found: {file_path}", None) from e
self.logger.debug("Request Headers Sent: %s", response.request.headers)
self.logger.debug("Request URL: %s", response.request.url)
if response.status_code != 200:
self.logger.error("Processing failed: %s", response.text)
raise ApiHubClientException(response.text, response.status_code)
data = response.json()
execution_id = data.get("execution_id")
# If execution_id is not directly available, try to extract from status_api
if not execution_id:
status_api = data.get("message", {}).get("status_api")
if status_api:
execution_id = self._extract_execution_id_from_url(status_api)
self.logger.info(
"Processing started successfully. Execution ID: %s", execution_id
)
# If wait_for_completion is True, poll for status and return final result
if wait_for_completion:
if not execution_id:
self.logger.warning(
"No execution_id in response, returning initial data"
)
return data
return self.wait_for_completion(
endpoint,
execution_id,
polling_interval=polling_interval,
timeout=timeout,
)
return data
def get_result(self, endpoint: str, execution_id: str) -> dict:
"""
Get the result of a processing operation.
Args:
endpoint: The endpoint name used for processing
execution_id: The execution ID to get results for
Returns:
dict: Processing result or status information
Raises:
ApiHubClientException: If result retrieval fails
"""
url = f"{self.base_url}/api/v1/{endpoint}"
params = {"execution_id": execution_id}
self.logger.debug(
"Getting result for endpoint %s, execution ID: %s", endpoint, execution_id
)
response = requests.get(url, headers=self.headers, params=params)
if response.status_code == 422:
# Handle 422 status which may indicate processing in progress
try:
data = response.json()
if "status" in data:
return data
except (ValueError, KeyError):
# JSON parsing failed or status key missing, treat as error
pass
raise ApiHubClientException(response.text, response.status_code)
elif response.status_code != 200:
raise ApiHubClientException(response.text, response.status_code)
return response.json()
def wait_for_completion(
self,
endpoint: str,
execution_id: str,
timeout: int = 600,
polling_interval: int = 3,
) -> dict:
"""
Wait for a processing operation to complete by polling its status.
Args:
endpoint: The endpoint name used for processing
execution_id: The execution ID to wait for
timeout: Maximum time to wait in seconds (default: 600)
polling_interval: Seconds to wait between status checks (default: 3)
Returns:
dict: Final processing result when completed
Raises:
ApiHubClientException: If processing fails or times out
"""
self.logger.info(
"Waiting for completion. Polling every %d seconds", polling_interval
)
start_time = time.time()
while time.time() - start_time < timeout:
result = self.get_result(endpoint, execution_id)
status = result.get("status")
self.logger.info("Current status: %s", status)
# Check for completion - different APIs may use different status values
if status in ["COMPLETED", "SUCCESS", "FINISHED"]:
self.logger.info("Processing completed")
return result
elif status in ["FAILED", "ERROR"]:
self.logger.error("Processing failed")
error_message = result.get("error", "Unknown error")
raise ApiHubClientException(
(
f"Processing failed for execution_id: {execution_id}. "
f"Error: {error_message}"
),
None,
)
elif status in ["PROCESSING", "IN_PROGRESS", "RUNNING", "EXECUTING"]:
# Continue polling
pass
else:
# For unknown status, assume it's still processing
self.logger.debug("Unknown status: %s, continuing to poll", status)
time.sleep(polling_interval)
# If we reach here, we've timed out
raise ApiHubClientException(
f"Timeout waiting for completion. Execution ID: {execution_id}",
None,
)
def check_status(self, endpoint: str, execution_id: str) -> str | None:
"""
Check the current status of a processing operation.
Args:
endpoint: The endpoint name used for processing
execution_id: The execution ID to check status for
Returns:
str | None: Current status string, or None if not available
Raises:
ApiHubClientException: If status check fails
"""
try:
result = self.get_result(endpoint, execution_id)
return result.get("status")
except ApiHubClientException:
# Re-raise the exception to let caller handle it
raise