# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

if TYPE_CHECKING:
    from .._client import AsyncWorkOSClient, WorkOSClient

from .._types import RequestOptions
from .models import WidgetSessionTokenResponse
from workos.common.models.widget_session_token_scopes import WidgetSessionTokenScopes


class Widgets:
    """Widgets API resources."""

    def __init__(self, client: "WorkOSClient") -> None:
        self._client = client

    def create_token(
        self,
        *,
        organization_id: Optional[str] = None,
        user_id: Optional[str] = None,
        scopes: Optional[List[Union[WidgetSessionTokenScopes, str]]] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> WidgetSessionTokenResponse:
        """Generate a widget token

        Generate a widget token for a user, optionally scoped to an organization. When an organization is specified, org-scoped widgets are enabled; omitting it issues a user-only token for widgets like `UserProfile` and `UserSecurity`.

        Args:
            organization_id: The ID of the organization to scope the widget session to. Required when scopes are provided. Optional when issuing a token for user-only widgets (e.g. `UserProfile`, `UserSecurity`) that do not require organization context.
            user_id: The ID of the user to issue the widget session token for.
            scopes: The scopes to grant the widget session.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            WidgetSessionTokenResponse

        Raises:
            BadRequestError: If the request is malformed (400).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            k: v
            for k, v in {
                "organization_id": organization_id,
                "user_id": user_id,
                "scopes": scopes,
            }.items()
            if v is not None
        }
        return self._client.request(
            method="post",
            path=("widgets", "token"),
            body=body,
            model=WidgetSessionTokenResponse,
            request_options=request_options,
        )


class AsyncWidgets:
    """Widgets API resources (async)."""

    def __init__(self, client: "AsyncWorkOSClient") -> None:
        self._client = client

    async def create_token(
        self,
        *,
        organization_id: Optional[str] = None,
        user_id: Optional[str] = None,
        scopes: Optional[List[Union[WidgetSessionTokenScopes, str]]] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> WidgetSessionTokenResponse:
        """Generate a widget token

        Generate a widget token for a user, optionally scoped to an organization. When an organization is specified, org-scoped widgets are enabled; omitting it issues a user-only token for widgets like `UserProfile` and `UserSecurity`.

        Args:
            organization_id: The ID of the organization to scope the widget session to. Required when scopes are provided. Optional when issuing a token for user-only widgets (e.g. `UserProfile`, `UserSecurity`) that do not require organization context.
            user_id: The ID of the user to issue the widget session token for.
            scopes: The scopes to grant the widget session.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            WidgetSessionTokenResponse

        Raises:
            BadRequestError: If the request is malformed (400).
            NotFoundError: If the resource is not found (404).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            k: v
            for k, v in {
                "organization_id": organization_id,
                "user_id": user_id,
                "scopes": scopes,
            }.items()
            if v is not None
        }
        return await self._client.request(
            method="post",
            path=("widgets", "token"),
            body=body,
            model=WidgetSessionTokenResponse,
            request_options=request_options,
        )
