|
| 1 | +from typing import Protocol, Sequence |
| 2 | +from workos.types.widgets.widget_scope import WidgetScope |
| 3 | +from workos.types.widgets.widget_token_response import WidgetTokenResponse |
| 4 | +from workos.utils.http_client import SyncHTTPClient |
| 5 | +from workos.utils.request_helper import REQUEST_METHOD_POST |
| 6 | + |
| 7 | + |
| 8 | +WIDGETS_GENERATE_TOKEN_PATH = "widgets/token" |
| 9 | + |
| 10 | + |
| 11 | +class WidgetsModule(Protocol): |
| 12 | + def get_token( |
| 13 | + self, |
| 14 | + *, |
| 15 | + organization_id: str, |
| 16 | + user_id: str, |
| 17 | + scopes: Sequence[WidgetScope], |
| 18 | + ) -> WidgetTokenResponse: |
| 19 | + """Generate a new widget token for the specified organization and user with the provided scopes. |
| 20 | +
|
| 21 | + Kwargs: |
| 22 | + organization_id (str): The ID of the organization the widget token will be generated for. |
| 23 | + user_id (str): The ID of the AuthKit user the widget token will be generated for. |
| 24 | + scopes (Sequence[WidgetScope]): The widget scopes for the generated widget token. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + WidgetTokenResponse: WidgetTokenResponse object with token string. |
| 28 | + """ |
| 29 | + ... |
| 30 | + |
| 31 | + |
| 32 | +class Widgets(WidgetsModule): |
| 33 | + |
| 34 | + _http_client: SyncHTTPClient |
| 35 | + |
| 36 | + def __init__(self, http_client: SyncHTTPClient): |
| 37 | + self._http_client = http_client |
| 38 | + |
| 39 | + def get_token( |
| 40 | + self, |
| 41 | + *, |
| 42 | + organization_id: str, |
| 43 | + user_id: str, |
| 44 | + scopes: Sequence[WidgetScope], |
| 45 | + ) -> WidgetTokenResponse: |
| 46 | + json = { |
| 47 | + "organization_id": organization_id, |
| 48 | + "user_id": user_id, |
| 49 | + "scopes": scopes, |
| 50 | + } |
| 51 | + response = self._http_client.request( |
| 52 | + WIDGETS_GENERATE_TOKEN_PATH, method=REQUEST_METHOD_POST, json=json |
| 53 | + ) |
| 54 | + |
| 55 | + return WidgetTokenResponse.model_validate(response) |
0 commit comments