-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
280 lines (237 loc) · 9.56 KB
/
client.py
File metadata and controls
280 lines (237 loc) · 9.56 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
"""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 json
from urllib.parse import urlparse
import requests
from requests.exceptions import JSONDecodeError
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 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
):
"""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.
"""
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
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)
def structure_file(self, file_paths: list[str], custom_data: dict = None) -> dict:
"""Invokes the API deployed on the Unstract platform.
Args:
file_paths (list[str]): The file path to the file to be uploaded.
custom_data (dict, optional): Custom data to send with the request.
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,
}
data = {"timeout": self.api_timeout, "include_metadata": self.include_metadata}
# Add custom_data if provided
if custom_data is not None:
data["custom_data"] = json.dumps(custom_data) if isinstance(custom_data, dict) else custom_data
self.logger.debug("Custom data added to request")
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))
response = requests.post(
self.api_url,
headers=headers,
data=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
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 = requests.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 (
200 <= response.status_code < 500
and obj_to_return["execution_status"] in self.in_progress_statuses
):
obj_to_return["pending"] = True
return obj_to_return