Skip to content

Commit 62a12d1

Browse files
committed
feat: add method to create and store a shared access key for a specific case, and extend the constructor to allow the use of this key.
1 parent cac5e6b commit 62a12d1

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/sumo/wrapper/_auth_provider.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ def get_authorization(self):
6666

6767
return {"Authorization": "Bearer " + token}
6868

69+
def store_shared_access_key_for_case(self, case_uuid, token):
70+
with open(get_token_path(self._resource_id + "+" + case_uuid,
71+
".sharedkey"), "w") as f:
72+
f.write(token)
73+
6974
pass
7075

7176

@@ -394,6 +399,7 @@ def get_auth_provider(
394399
access_token=None,
395400
refresh_token=None,
396401
devicecode=False,
402+
case_uuid = None,
397403
):
398404
if refresh_token:
399405
return AuthProviderRefreshToken(
@@ -403,6 +409,9 @@ def get_auth_provider(
403409
if access_token:
404410
return AuthProviderAccessToken(access_token)
405411
# ELSE
412+
if case_uuid is not None and os.path.exists(get_token_path(resource_id + "+" + case_uuid, ".sharedkey")):
413+
return AuthProviderSumoToken(resource_id + "+" + case_uuid)
414+
# ELSE
406415
if os.path.exists(get_token_path(resource_id, ".sharedkey")):
407416
return AuthProviderSumoToken(resource_id)
408417
# ELSE

src/sumo/wrapper/sumo_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(
3333
verbosity: str = "CRITICAL",
3434
retry_strategy=RetryStrategy(),
3535
timeout=DEFAULT_TIMEOUT,
36+
case_uuid = None,
3637
):
3738
"""Initialize a new Sumo object
3839
@@ -85,6 +86,7 @@ def __init__(
8586
refresh_token=refresh_token,
8687
access_token=access_token,
8788
devicecode=devicecode,
89+
case_uuid = case_uuid,
8890
)
8991

9092
if env == "localhost":
@@ -398,6 +400,22 @@ def getLogger(self, name):
398400
pass
399401
return logger
400402

403+
def create_shared_access_key_for_case(self, case_uuid):
404+
"""Creates and stores a shared access key that can be used to access
405+
the case identified by *case_uuid*, in the current Sumo environment.
406+
407+
This shared access key can then be used by instantiating
408+
SumoClient with the parameter case_uuid set accordingly.
409+
410+
Args:
411+
case_uuid: the uuid for a case.
412+
413+
Side effects:
414+
Creates a new file in ~/.sumo, named {app_id}+{case_uuid}
415+
"""
416+
token = self.get(f"/objects('{case_uuid}')/make-shared-access-key").text
417+
self.auth.store_shared_access_key_for_case(case_uuid, token)
418+
401419
@raise_for_status_async
402420
async def get_async(self, path: str, params: dict = None):
403421
"""Performs an async GET-request to the Sumo API.

0 commit comments

Comments
 (0)