-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add widgets module and widget get token API endpoint. (#380)
- Loading branch information
Showing
8 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pytest | ||
|
||
from workos.widgets import Widgets | ||
|
||
|
||
class TestWidgets(object): | ||
@pytest.fixture(autouse=True) | ||
def setup(self, sync_http_client_for_test): | ||
self.http_client = sync_http_client_for_test | ||
self.widgets = Widgets(http_client=self.http_client) | ||
|
||
@pytest.fixture | ||
def mock_widget_token(self): | ||
return {"token": "abc123456"} | ||
|
||
def test_get_token(self, mock_widget_token, mock_http_client_with_response): | ||
mock_http_client_with_response(self.http_client, mock_widget_token, 201) | ||
|
||
response = self.widgets.get_token( | ||
organization_id="org_01EHQMYV6MBK39QC5PZXHY59C3", | ||
user_id="user_01EHQMYV6MBK39QC5PZXHY59C3", | ||
scopes=["widgets:users-table:manage"], | ||
) | ||
|
||
assert response.token == "abc123456" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .widget_scope import * | ||
from .widget_token_response import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from typing import Literal | ||
|
||
|
||
WidgetScope = Literal["widgets:users-table:manage"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from workos.types.workos_model import WorkOSModel | ||
|
||
|
||
class WidgetTokenResponse(WorkOSModel): | ||
"""Representation of a WorkOS widget token response.""" | ||
|
||
token: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Protocol, Sequence | ||
from workos.types.widgets.widget_scope import WidgetScope | ||
from workos.types.widgets.widget_token_response import WidgetTokenResponse | ||
from workos.utils.http_client import SyncHTTPClient | ||
from workos.utils.request_helper import REQUEST_METHOD_POST | ||
|
||
|
||
WIDGETS_GENERATE_TOKEN_PATH = "widgets/token" | ||
|
||
|
||
class WidgetsModule(Protocol): | ||
def get_token( | ||
self, | ||
*, | ||
organization_id: str, | ||
user_id: str, | ||
scopes: Sequence[WidgetScope], | ||
) -> WidgetTokenResponse: | ||
"""Generate a new widget token for the specified organization and user with the provided scopes. | ||
Kwargs: | ||
organization_id (str): The ID of the organization the widget token will be generated for. | ||
user_id (str): The ID of the AuthKit user the widget token will be generated for. | ||
scopes (Sequence[WidgetScope]): The widget scopes for the generated widget token. | ||
Returns: | ||
WidgetTokenResponse: WidgetTokenResponse object with token string. | ||
""" | ||
... | ||
|
||
|
||
class Widgets(WidgetsModule): | ||
|
||
_http_client: SyncHTTPClient | ||
|
||
def __init__(self, http_client: SyncHTTPClient): | ||
self._http_client = http_client | ||
|
||
def get_token( | ||
self, | ||
*, | ||
organization_id: str, | ||
user_id: str, | ||
scopes: Sequence[WidgetScope], | ||
) -> WidgetTokenResponse: | ||
json = { | ||
"organization_id": organization_id, | ||
"user_id": user_id, | ||
"scopes": scopes, | ||
} | ||
response = self._http_client.request( | ||
WIDGETS_GENERATE_TOKEN_PATH, method=REQUEST_METHOD_POST, json=json | ||
) | ||
|
||
return WidgetTokenResponse.model_validate(response) |