Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method to create and store a shared access key... #219

Merged
merged 6 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/sumo/wrapper/_auth_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -394,6 +401,7 @@ def get_auth_provider(
access_token=None,
refresh_token=None,
devicecode=False,
case_uuid=None,
):
if refresh_token:
return AuthProviderRefreshToken(
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/sumo/wrapper/sumo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(
verbosity: str = "CRITICAL",
retry_strategy=RetryStrategy(),
timeout=DEFAULT_TIMEOUT,
case_uuid=None,
):
"""Initialize a new Sumo object

Expand All @@ -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()
Expand Down Expand Up @@ -85,6 +89,7 @@ def __init__(
refresh_token=refresh_token,
access_token=access_token,
devicecode=devicecode,
case_uuid=case_uuid,
)

if env == "localhost":
Expand Down Expand Up @@ -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*."""
rwiker marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down
Loading