-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsumo_client.py
More file actions
759 lines (594 loc) · 21 KB
/
sumo_client.py
File metadata and controls
759 lines (594 loc) · 21 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
import asyncio
import contextlib
import logging
import re
import time
from typing import Dict, Optional, Tuple
import httpx
import jwt
from ._auth_provider import cleanup_shared_keys, get_auth_provider
from ._blob_client import BlobClient
from ._decorators import (
raise_for_status,
raise_for_status_async,
)
from ._logging import LogHandlerSumo
from ._retry_strategy import RetryStrategy
from .config import APP_REGISTRATION, AUTHORITY_HOST_URI, TENANT_ID
logger = logging.getLogger("sumo.wrapper")
DEFAULT_TIMEOUT = httpx.Timeout(30.0)
class SumoClient:
"""Authenticate and perform requests to the Sumo API."""
_client: httpx.Client
_async_client: httpx.AsyncClient
def __init__(
self,
env: str,
token: Optional[str] = None,
interactive: bool = True,
devicecode: bool = False,
verbosity: str = "CRITICAL",
retry_strategy=RetryStrategy(),
timeout=DEFAULT_TIMEOUT,
case_uuid=None,
http_client=None,
async_http_client=None,
):
"""Initialize a new Sumo object
Args:
env: Sumo environment
token: Access token or refresh token.
interactive: Enable interactive authentication (in browser).
If not enabled, code grant flow will be used.
verbosity: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
"""
logger.setLevel(verbosity)
if env not in APP_REGISTRATION:
raise ValueError(f"Invalid environment: {env}")
self.env = env
self._verbosity = verbosity
self._retry_strategy = retry_strategy
if http_client is None:
self._client = httpx.Client()
self._borrowed_client = False
else:
self._client = http_client
self._borrowed_client = True
if async_http_client is None:
self._async_client = httpx.AsyncClient()
self._borrowed_async_client = False
else:
self._async_client = async_http_client
self._borrowed_async_client = True
self._timeout = timeout
access_token = None
refresh_token = None
if token:
logger.debug("Token provided")
payload = None
with contextlib.suppress(jwt.InvalidTokenError):
payload = jwt.decode(
token, options={"verify_signature": False}
)
if payload:
logger.debug(f"Token decoded as JWT, payload: {payload}")
access_token = token
else:
logger.debug(
"Unable to decode token as JWT, "
"treating it as a refresh token"
)
refresh_token = token
pass
pass
cleanup_shared_keys()
self.auth = get_auth_provider(
client_id=APP_REGISTRATION[env]["CLIENT_ID"],
authority=f"{AUTHORITY_HOST_URI}/{TENANT_ID}",
resource_id=APP_REGISTRATION[env]["RESOURCE_ID"],
interactive=interactive,
refresh_token=refresh_token,
access_token=access_token,
devicecode=devicecode,
case_uuid=case_uuid,
)
if env == "localhost":
self.base_url = "http://localhost:8084/api/v1"
else:
self.base_url = f"https://main-sumo-{env}.radix.equinor.com/api/v1"
pass
return
def __enter__(self):
return self
def __exit__(self, *_):
if not self._borrowed_client:
self._client.close()
return False
async def __aenter__(self):
return self
async def __aexit__(self, *_):
if not self._borrowed_async_client:
await self._async_client.aclose()
return False
def __del__(self):
if self._client is not None and not self._borrowed_client:
self._client.close()
pass
if self._async_client is not None and not self._borrowed_async_client:
async def closeit(client):
await client.aclose()
return
try:
loop = asyncio.get_running_loop()
loop.create_task(closeit(self._async_client))
except RuntimeError:
pass
pass
def authenticate(self):
if self.auth is None:
return None
return self.auth.get_token()
@property
def blob_client(self) -> BlobClient:
"""Get blob_client
Used for uploading blob using a pre-authorized blob URL.
Examples:
Uploading blob::
blob = ...
blob_url = ...
sumo = SumoClient("dev")
sumo.blob_client.upload_blob(blob, blob_url)
Uploading blob async::
await sumo.blob_client.upload_blob_async(blob, blob_url)
"""
return BlobClient(
self._client,
self._async_client,
self._timeout,
self._retry_strategy,
)
@raise_for_status
def get(self, path: str, params: Optional[Dict] = None) -> httpx.Response:
"""Performs a GET-request to the Sumo API.
Args:
path: Path to a Sumo endpoint
params: query parameters, as dictionary
Returns:
Sumo JSON response as a dictionary
Examples:
Retrieving user data from Sumo::
sumo = SumoClient("dev")
userdata = sumo.get(path="/userdata")
Searching for cases::
sumo = SuomClient("dev")
cases = sumo.get(
path="/search",
query="class:case",
size=3
)
"""
headers = {
"Content-Type": "application/json",
}
headers.update(self.auth.get_authorization())
follow_redirects = False
if (
re.match(
r"^/objects\('[0-9a-fA-F-]{8}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{12}'\)/blob$", # noqa: E501
path,
)
is not None
or re.match(
r"^/tasks\('[0-9a-fA-F-]{8}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{12}'\)/result$", # noqa: E501
path,
)
is not None
):
follow_redirects = True
def _get():
return self._client.get(
f"{self.base_url}{path}",
params=params,
headers=headers,
follow_redirects=follow_redirects,
timeout=self._timeout,
)
retryer = self._retry_strategy.make_retryer()
return retryer(_get)
@raise_for_status
def post(
self,
path: str,
blob: Optional[bytes] = None,
json: Optional[dict] = None,
params: Optional[dict] = None,
) -> httpx.Response:
"""Performs a POST-request to the Sumo API.
Takes either blob or json as a payload,
will raise an error if both are provided.
Args:
path: Path to a Sumo endpoint
blob: Blob payload
json: Json payload
params: query parameters, as dictionary
Returns:
Sumo response object
Raises:
ValueError: If both blob and json parameters have been provided
Examples:
Uploading case metadata::
case_metadata = {...}
sumo = SumoClient("dev")
new_case = sumo.post(
path="/objects",
json=case_metadata
)
new_case_id = new_case.json()["_id"]
Uploading object metadata::
object_metadata = {...}
sumo = SumoClient("dev")
new_object = sumo.post(
path=f"/objects('{new_case_id}')",
json=object_metadata
)
"""
if blob and json:
raise ValueError("Both blob and json given to post.")
content_type = (
"application/octet-stream" if blob else "application/json"
)
headers = {
"Content-Type": content_type,
}
headers.update(self.auth.get_authorization())
def _post():
return self._client.post(
f"{self.base_url}{path}",
content=blob,
json=json,
headers=headers,
params=params,
timeout=self._timeout,
)
retryer = self._retry_strategy.make_retryer()
return retryer(_post)
@raise_for_status
def put(
self,
path: str,
blob: Optional[bytes] = None,
json: Optional[dict] = None,
) -> httpx.Response:
"""Performs a PUT-request to the Sumo API.
Takes either blob or json as a payload,
will raise an error if both are provided.
Args:
path: Path to a Sumo endpoint
blob: Blob payload
json: Json payload
Returns:
Sumo response object
"""
if blob and json:
raise ValueError("Both blob and json given to post")
content_type = (
"application/json"
if json is not None
else "application/octet-stream"
)
headers = {
"Content-Type": content_type,
}
headers.update(self.auth.get_authorization())
def _put():
return self._client.put(
f"{self.base_url}{path}",
content=blob,
json=json,
headers=headers,
timeout=self._timeout,
)
retryer = self._retry_strategy.make_retryer()
return retryer(_put)
@raise_for_status
def delete(
self, path: str, params: Optional[dict] = None
) -> httpx.Response:
"""Performs a DELETE-request to the Sumo API.
Args:
path: Path to a Sumo endpoint
params: query parameters, as dictionary
Returns:
Sumo JSON response as a dictionary
Examples:
Deleting object::
object_id = ...
sumo = SumoClient("dev")
sumo.delete(path=f"/objects('{object_id}')")
"""
headers = {
"Content-Type": "application/json",
}
headers.update(self.auth.get_authorization())
def _delete():
return self._client.delete(
f"{self.base_url}{path}",
headers=headers,
params=params,
timeout=self._timeout,
)
retryer = self._retry_strategy.make_retryer()
return retryer(_delete)
def _get_retry_details(self, response_in) -> Tuple[str, int]:
assert response_in.status_code == 202, (
"Incorrect status code; expcted 202"
)
headers = response_in.headers
location: str = headers.get("location")
assert location is not None, "Missing header: Location"
assert location.startswith(self.base_url)
retry_after = headers.get("retry-after")
assert retry_after is not None, "Missing header: Retry-After"
location = location[len(self.base_url) :]
retry_after = int(retry_after)
return location, retry_after
def poll(
self, response_in: httpx.Response, timeout=None
) -> httpx.Response:
"""Poll a specific endpoint until a result is obtained.
Args:
response_in: httpx.Response from a previous request, with 'location' and 'retry-after' headers.
Returns:
A new httpx.response object.
"""
location, retry_after = self._get_retry_details(response_in)
expiry = time.time() + timeout if timeout is not None else None
while True:
time.sleep(retry_after)
response = self.get(location)
if response.status_code != 202:
return response
if expiry is not None and time.time() > expiry:
raise httpx.TimeoutException(
"No response within specified timeout."
)
location, retry_after = self._get_retry_details(response)
pass
def getLogger(self, name):
"""Gets a logger object that sends log objects into the message_log
index for the Sumo instance.
Args:
name: string naming the logger instance
Returns:
logger instance
See Python documentation for logging.Logger for details.
"""
logger = logging.getLogger(name)
if len(logger.handlers) == 0:
handler = LogHandlerSumo(self)
logger.addHandler(handler)
pass
return logger
def create_shared_access_key_for_case(self, case_uuid):
"""Creates and stores a shared access key that can be used to access
the case identified by *case_uuid*, in the current Sumo environment.
This shared access key can then be used by instantiating
SumoClient with the parameter case_uuid set accordingly.
Args:
case_uuid: the uuid for a case.
Side effects:
Creates a new file in ~/.sumo, named {app_id}+{case_uuid}
"""
token = self.get(
f"/objects('{case_uuid}')/make-shared-access-key"
).text
self.auth.store_shared_access_key_for_case(case_uuid, token)
def client_for_case(self, case_uuid):
"""Instantiate and return new SumoClient for accessing the
case identified by *case_uuid*."""
if self.auth.has_case_token(case_uuid):
return SumoClient(
env=self.env,
verbosity=self._verbosity,
retry_strategy=self._retry_strategy,
timeout=self._timeout,
case_uuid=case_uuid,
)
else:
return self
@raise_for_status_async
async def get_async(
self, path: str, params: Optional[dict] = None
) -> httpx.Response:
"""Performs an async GET-request to the Sumo API.
Args:
path: Path to a Sumo endpoint
params: query parameters, as dictionary
Returns:
Sumo JSON response as a dictionary
Examples:
Retrieving user data from Sumo::
sumo = SumoClient("dev")
userdata = await sumo.get_async(path="/userdata")
Searching for cases::
sumo = SuomClient("dev")
cases = await sumo.get_async(
path="/search",
query="class:case",
size=3
)
"""
headers = {
"Content-Type": "application/json",
}
headers.update(self.auth.get_authorization())
follow_redirects = False
if (
re.match(
r"^/objects\('[0-9a-fA-F-]{8}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{12}'\)/blob$", # noqa: E501
path,
)
is not None
or re.match(
r"^/tasks\('[0-9a-fA-F-]{8}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{4}-[0-9a-fA-F-]{12}'\)/result$", # noqa: E501
path,
)
is not None
):
follow_redirects = True
async def _get():
return await self._async_client.get(
f"{self.base_url}{path}",
params=params,
headers=headers,
follow_redirects=follow_redirects,
timeout=self._timeout,
)
retryer = self._retry_strategy.make_retryer_async()
return await retryer(_get)
@raise_for_status_async
async def post_async(
self,
path: str,
blob: Optional[bytes] = None,
json: Optional[dict] = None,
params: Optional[dict] = None,
) -> httpx.Response:
"""Performs an async POST-request to the Sumo API.
Takes either blob or json as a payload,
will raise an error if both are provided.
Args:
path: Path to a Sumo endpoint
blob: Blob payload
json: Json payload
params: query parameters, as dictionary
Returns:
Sumo response object
Raises:
ValueError: If both blob and json parameters have been provided
Examples:
Uploading case metadata::
case_metadata = {...}
sumo = SumoClient("dev")
new_case = await sumo.post_async(
path="/objects",
json=case_metadata
)
new_case_id = new_case.json()["_id"]
Uploading object metadata::
object_metadata = {...}
sumo = SumoClient("dev")
new_object = await sumo.post_async(
path=f"/objects('{new_case_id}')",
json=object_metadata
)
"""
if blob and json:
raise ValueError("Both blob and json given to post.")
content_type = (
"application/octet-stream" if blob else "application/json"
)
headers = {
"Content-Type": content_type,
}
headers.update(self.auth.get_authorization())
async def _post():
return await self._async_client.post(
url=f"{self.base_url}{path}",
content=blob,
json=json,
headers=headers,
params=params,
timeout=self._timeout,
)
retryer = self._retry_strategy.make_retryer_async()
return await retryer(_post)
@raise_for_status_async
async def put_async(
self,
path: str,
blob: Optional[bytes] = None,
json: Optional[dict] = None,
) -> httpx.Response:
"""Performs an async PUT-request to the Sumo API.
Takes either blob or json as a payload,
will raise an error if both are provided.
Args:
path: Path to a Sumo endpoint
blob: Blob payload
json: Json payload
Returns:
Sumo response object
"""
if blob and json:
raise ValueError("Both blob and json given to post")
content_type = (
"application/json"
if json is not None
else "application/octet-stream"
)
headers = {
"Content-Type": content_type,
}
headers.update(self.auth.get_authorization())
async def _put():
return await self._async_client.put(
url=f"{self.base_url}{path}",
content=blob,
json=json,
headers=headers,
timeout=self._timeout,
)
retryer = self._retry_strategy.make_retryer_async()
return await retryer(_put)
@raise_for_status_async
async def delete_async(
self, path: str, params: Optional[dict] = None
) -> httpx.Response:
"""Performs an async DELETE-request to the Sumo API.
Args:
path: Path to a Sumo endpoint
params: query parameters, as dictionary
Returns:
Sumo JSON response as a dictionary
Examples:
Deleting object::
object_id = ...
sumo = SumoClient("dev")
await sumo.delete_async(path=f"/objects('{object_id}')")
"""
headers = {
"Content-Type": "application/json",
}
headers.update(self.auth.get_authorization())
async def _delete():
return await self._async_client.delete(
url=f"{self.base_url}{path}",
headers=headers,
params=params,
timeout=self._timeout,
)
retryer = self._retry_strategy.make_retryer_async()
return await retryer(_delete)
async def poll_async(
self, response_in: httpx.Response, timeout=None
) -> httpx.Response:
"""Poll a specific endpoint until a result is obtained.
Args:
response_in: httpx.Response from a previous request, with 'location' and 'retry-after' headers.
Returns:
A new httpx.response object.
"""
location, retry_after = self._get_retry_details(response_in)
expiry = time.time() + timeout if timeout is not None else None
while True:
await asyncio.sleep(retry_after)
response = await self.get_async(location)
if response.status_code != 202:
return response
if expiry is not None and time.time() > expiry:
raise httpx.TimeoutException(
"No response within specified timeout."
)
location, retry_after = self._get_retry_details(response)
pass