diff --git a/src/sumo/wrapper/_auth_provider.py b/src/sumo/wrapper/_auth_provider.py index 20fcc2c..43d086b 100644 --- a/src/sumo/wrapper/_auth_provider.py +++ b/src/sumo/wrapper/_auth_provider.py @@ -66,6 +66,13 @@ def get_authorization(self): return {"Authorization": "Bearer " + token} + def store_shared_access_key_for_case(self, case_uuid, token): + with open( + get_token_path(self._resource_id + "+" + case_uuid, ".sharedkey"), + "w", + ) as f: + f.write(token) + pass @@ -394,6 +401,7 @@ def get_auth_provider( access_token=None, refresh_token=None, devicecode=False, + case_uuid=None, ): if refresh_token: return AuthProviderRefreshToken( @@ -403,6 +411,11 @@ def get_auth_provider( if access_token: return AuthProviderAccessToken(access_token) # ELSE + if case_uuid is not None and os.path.exists( + get_token_path(resource_id + "+" + case_uuid, ".sharedkey") + ): + return AuthProviderSumoToken(resource_id + "+" + case_uuid) + # ELSE if os.path.exists(get_token_path(resource_id, ".sharedkey")): return AuthProviderSumoToken(resource_id) # ELSE diff --git a/src/sumo/wrapper/sumo_client.py b/src/sumo/wrapper/sumo_client.py index 5096e1c..b5eddaa 100644 --- a/src/sumo/wrapper/sumo_client.py +++ b/src/sumo/wrapper/sumo_client.py @@ -33,6 +33,7 @@ def __init__( verbosity: str = "CRITICAL", retry_strategy=RetryStrategy(), timeout=DEFAULT_TIMEOUT, + case_uuid=None, ): """Initialize a new Sumo object @@ -49,6 +50,9 @@ def __init__( if env not in APP_REGISTRATION: raise ValueError(f"Invalid environment: {env}") + self.env = env + self._verbosity = verbosity + self._retry_strategy = retry_strategy self._client = httpx.Client() self._async_client = httpx.AsyncClient() @@ -85,6 +89,7 @@ def __init__( refresh_token=refresh_token, access_token=access_token, devicecode=devicecode, + case_uuid=case_uuid, ) if env == "localhost": @@ -398,6 +403,35 @@ def getLogger(self, name): 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*.""" + return SumoClient( + env=self.env, + verbosity=self._verbosity, + retry_strategy=self._retry_strategy, + timeout=self._timeout, + case_uuid=case_uuid, + ) + @raise_for_status_async async def get_async(self, path: str, params: dict = None): """Performs an async GET-request to the Sumo API.