Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ dashboard.export(
This functionality is also available in the same manner for datasets


### Retrieve a Guest Token

You can retrieve a guest token using the `guest_token` method. This method requires the UUID of the resource (e.g., dashboard) and uses the user's first name, last name, and username from the client instance.

#### Example Usage:
```python3
# Retrieve a guest token
guest_token = client.guest_token(uuid="your-example-uuid")
```


# Contributing
Before committing to this repository, you must have [pre-commit](https://pre-commit.com) installed, and install
the following pre-commit hooks:
Expand Down
36 changes: 36 additions & 0 deletions supersetapiclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def __init__(
password=None,
provider="db",
verify=True,
firstname="Superset",
lastname="Admin",
):
self.host = host
self.base_url = self.join_urls(host, "api/v1")
Expand All @@ -50,6 +52,9 @@ def __init__(
self.provider = provider
if not verify:
self.http_adapter_cls = NoVerifyHTTPAdapter

self.firstname = firstname
self.lastname = lastname

# Related Objects
self.assets = self.assets_cls(self)
Expand Down Expand Up @@ -202,6 +207,10 @@ def login_endpoint(self) -> str:
@property
def refresh_endpoint(self) -> str:
return self.join_urls(self.base_url, "security/refresh")

@property
def guest_token_endpoint(self) -> str:
return self.join_urls(self.base_url, "security/guest_token/")

@property
def _sql_endpoint(self) -> str:
Expand All @@ -215,6 +224,33 @@ def csrf_token(self, session) -> str:
)
raise_for_status(csrf_response) # Check CSRF Token went well
return csrf_response.json().get("result")

def guest_token(self, uuid: str) -> dict:
"""Retrieve a guest token from the Superset API.

:param uuid: The UUID of the resource (e.g., dashboard).
:type uuid: str
:return: Guest token as a dictionary.
"""
# Construct the request body
request_body = {
"resources": [
{
"id": uuid,
"type": "dashboard"
}
],
"rls": [],
"user": {
"first_name": self.firstname,
"last_name": self.lastname,
"username": self.username
}
}

response = self.post(self.guest_token_endpoint, json=request_body)
raise_for_status(response) # Check for errors in the response
return response.json().get("token")


class NoVerifyHTTPAdapter(requests.adapters.HTTPAdapter):
Expand Down