-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathma_session.py
More file actions
323 lines (281 loc) · 11.5 KB
/
ma_session.py
File metadata and controls
323 lines (281 loc) · 11.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
from json.decoder import JSONDecodeError
from time import sleep
import typing as t_
import json
import requests
import numpy as np
from requests.structures import CaseInsensitiveDict
from requests.sessions import Session
from smsdk import config
try:
import importlib.resources
RESOURCE_CONFIG = json.loads(
importlib.resources.read_text(config, "message_config.json")
)
except ImportError:
# Try backported to PY<37 `importlib_resources`.
import importlib_resources
RESOURCE_CONFIG = json.loads(
importlib_resources.read_text(config, "message_config.json")
)
from smsdk.custom_exception.errors import NotFound
SM_AUTH_HEADER_SECRET_ID = RESOURCE_CONFIG["auth_header-api-secret"]
SM_AUTH_HEADER_SECRET_ID_OLD = RESOURCE_CONFIG["auth_header-api-secret_old"]
SM_AUTH_HEADER_KEY_ID = RESOURCE_CONFIG["auth_header-api-key"]
X_SM_DB_SCHEMA = RESOURCE_CONFIG["x_sm_db_schema"]
X_SM_WORKSPACE_ID = RESOURCE_CONFIG["x_sm_workspace_id"]
import logging
log = logging.getLogger(__name__)
try:
NPINFINITY = np.Inf
except AttributeError:
# numpy 2.0
NPINFINITY = np.inf
class MaSession:
def __init__(self) -> None:
self.requests = requests
self.session = Session()
def _get_records(
self,
endpoint: str,
method: str = "get",
_limit: float = NPINFINITY,
_offset: int = 0,
**url_params: t_.Any,
) -> t_.List[t_.Dict[str, t_.Any]]:
"""
Function to get api call and fetch data from MA APIs
:param endpoint: complete url endpoint
:param method: Reqested method. Default = get
:param enable_pagination: if pagination is enabled then
the records are fetched with limit offset pagination
:param limit: Limit the number of records for pagination
:param offset: pagination offset
:param url_params: dict of params for API ex filtering, columns etc
:return: List of records
"""
if "machine_type" in url_params:
url_params.pop("machine_type")
max_page_size = 2000
records: t_.List[t_.Dict[str, t_.Any]] = []
while True:
try:
remaining_limit = _limit - len(records)
this_loop_limit = int(min(remaining_limit, max_page_size))
# If we exactly hit our desired number of records -- limit is 0 -- then can stop
if this_loop_limit <= 0:
return records
url_params["_offset"] = _offset
url_params["_limit"] = this_loop_limit
response = getattr(self.session, method.lower())(
endpoint, params=url_params
)
if response.text:
if response.status_code == 404:
raise NotFound(response.text)
elif response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
try:
data = response.json()
if "results" in data:
data = data["results"]
except JSONDecodeError as e:
# No need to raise an error as this will still continue execution.
print(f"No valid JSON returned, but continuing. {e}")
continue
else:
return []
records.extend(data)
if len(data) < this_loop_limit:
# Cursor exhausted, so just return
return records
_offset += this_loop_limit
except NotFound as e:
raise e
except Exception as e:
# No need to raise an error as this will still continue execution.
print(f"Error getting data, but continuing. {e}")
continue
def _get_schema(
self, endpoint: str, method: str = "get", **url_params: t_.Any
) -> t_.Any:
"""
This function can be used to fetch HLO schemas like AIDP
Function to get api call and fetch data from MA APIs
:param endpoint: complete url endpoint
:param method: Reqested method. Default = get
:param url_params: dict of params for API ex filtering, columns etc
:return: List of records
"""
if "machine_type" in url_params:
url_params.pop("machine_type")
response = getattr(self.session, method.lower())(endpoint, params=url_params)
if response.text:
if response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
try:
data = response.json()
if "objects" in data:
data = data["objects"]
return data
except JSONDecodeError as e:
raise JSONDecodeError(
"Error decoding JSON response", response.text, e.pos
)
else:
return []
def _get_records_v1(
self,
endpoint: str,
method: str = "post",
limit: float = NPINFINITY,
offset: float = 0,
db_mode: str = "sql",
results_under: str = "results",
**url_params: t_.Any,
) -> t_.List[t_.Dict[str, t_.Any]]:
"""
Function to get api call and fetch data from MA APIs
:param endpoint: complete url endpoint
:param method: Reqested method. Default = get
:param enable_pagination: if pagination is enabled then
the records are fetched with limit offset pagination
:param limit: Limit the number of records for pagination
:param offset: pagination offset
:param url_params: dict of params for API ex filtering, columns etc
:return: List of records
"""
max_page_size = 50000
records: t_.List[t_.Dict[str, t_.Any]] = []
while True:
try:
if limit:
remaining_limit = limit - len(records)
this_loop_limit = min(remaining_limit, max_page_size)
# If we exactly hit our desired number of records -- limit is 0 -- then can stop
if this_loop_limit == 0:
return records
url_params["limit"] = this_loop_limit
if offset or url_params.get("model") == "line":
url_params["offset"] = offset
if db_mode:
url_params["db_mode"] = db_mode
# print(f'Pulling up to {this_loop_limit} records ({remaining_limit} remain)')
response = None
try:
response = getattr(self.session, method.lower())(
endpoint, json=url_params
)
except requests.exceptions.ConnectionError:
raise ValueError(
f"Error connecting to {endpoint}. Check your tenant name"
)
if response is not None and response.text:
if response.status_code not in [200, 201]:
raise ValueError(format(response.text))
try:
data = response.json()
if results_under:
data = data[results_under]
if isinstance(data, dict):
data = [data]
except JSONDecodeError as e:
raise JSONDecodeError(
"Error decoding JSON response", response.text, e.pos
)
else:
return []
records.extend(data)
if limit is None:
return records
if len(data) < this_loop_limit:
# Cursor exhausted, so just return
return records
offset += this_loop_limit
except ValueError as e:
raise e
except Exception as e:
# No need to raise an error as retrying with smaller page size.
print(f"Error getting data, retrying with smaller page size. {e}")
# Try throttling down the page size
max_page_size = int(max_page_size / 2)
continue
def _complete_async_task(
self,
endpoint: str,
method: str = "post",
db_mode: str = "sql",
**url_params: t_.Any,
) -> t_.Any:
is_analytics = url_params.get("is_analytics")
if url_params.get("db_mode") == None and not is_analytics:
url_params["db_mode"] = db_mode
try:
url_params.pop("is_analytics", None)
response = getattr(self.session, method.lower())(endpoint, json=url_params)
if response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
data = response.json()
task_id = data["response"]["task_id"]
while True:
try:
response = self.session.get(
endpoint + "/" + task_id, json=url_params
)
sleep(1)
if response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
data = response.json()
state = data["response"]["state"]
if state == "SUCCESS":
if not is_analytics:
return data["response"]["meta"]["results"]
else:
response = data["response"]["data"]
return response
if state == "FAILURE" or state == "REVOKED":
raise ValueError("Error - {}".format(response.text))
except Exception as e:
raise e
except Exception as e:
raise e
def _get_dashboard_panels(self, endpoint: str, method: str = "get") -> t_.Any:
try:
response = getattr(self.session, method.lower())(endpoint)
if response.status_code not in [200, 201]:
raise ValueError("Error - {}".format(response.text))
data = response.json()["panels"]
except Exception as e:
raise ValueError(e)
return data
def get_json_headers(self) -> CaseInsensitiveDict:
"""
Headers for json requests
"""
return CaseInsensitiveDict(
{
"Accept-Encoding": "*/*",
"Content-Type": "application/json",
"accept": "application/json",
}
)
def get_starttime_endtime_keys(self, **kwargs: t_.Any) -> t_.Tuple[str, str]:
"""
This function takes kwargs as input and tried to identify starttime and endtime key provided by user and returns
:param kwargs:
:return:
"""
starttime_key = ""
endtime_key = ""
times = {i: kwargs[i] for i in kwargs if "time" in i.lower()}
if times:
starttime = min(times.values())
endtime = max(times.values())
for key in times:
if times[key] == starttime:
starttime_key = key
elif times[key] == endtime:
endtime_key = key
else:
continue
return starttime_key, endtime_key