# This file was auto-generated by Fern from our API Definition.

import typing
from ..core.client_wrapper import SyncClientWrapper
from .types.client_side_access_tokens_create_client_side_access_token_request_policy_item import (
    ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem,
)
from ..core.request_options import RequestOptions
from .types.client_side_access_tokens_create_client_side_access_token_response import (
    ClientSideAccessTokensCreateClientSideAccessTokenResponse,
)
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.unchecked_base_model import construct_type
from ..errors.bad_request_error import BadRequestError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..core.jsonable_encoder import jsonable_encoder
from ..core.client_wrapper import AsyncClientWrapper

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class ClientSideAccessTokensClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def client_side_access_tokens_create_client_side_access_token(
        self,
        *,
        policy: typing.Sequence[ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem],
        hostname: str,
        expires_at: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ClientSideAccessTokensCreateClientSideAccessTokenResponse:
        """
        Create a new client side access token with the specified configuration.

        Parameters
        ----------
        policy : typing.Sequence[ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem]

        hostname : str
            The hostname of the client side application. Please specify the full URL including the protocol (http or https).

        expires_at : typing.Optional[str]
            The expiration date of the token. If not provided, the token will expire in 5 minutes

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ClientSideAccessTokensCreateClientSideAccessTokenResponse
            201

        Examples
        --------
        from letta_client import Letta
        from letta_client.client_side_access_tokens import (
            ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem,
        )

        client = Letta(
            token="YOUR_TOKEN",
        )
        client.client_side_access_tokens.client_side_access_tokens_create_client_side_access_token(
            policy=[
                ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem(
                    id="id",
                    access=["read_messages"],
                )
            ],
            hostname="hostname",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/client-side-access-tokens",
            method="POST",
            json={
                "policy": convert_and_respect_annotation_metadata(
                    object_=policy,
                    annotation=typing.Sequence[ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem],
                    direction="write",
                ),
                "hostname": hostname,
                "expires_at": expires_at,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ClientSideAccessTokensCreateClientSideAccessTokenResponse,
                    construct_type(
                        type_=ClientSideAccessTokensCreateClientSideAccessTokenResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        construct_type(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def client_side_access_tokens_delete_client_side_access_token(
        self,
        token: str,
        *,
        request: typing.Optional[typing.Any] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Optional[typing.Any]:
        """
        Delete a client side access token.

        Parameters
        ----------
        token : str
            The access token to delete

        request : typing.Optional[typing.Any]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.Optional[typing.Any]
            204

        Examples
        --------
        from letta_client import Letta

        client = Letta(
            token="YOUR_TOKEN",
        )
        client.client_side_access_tokens.client_side_access_tokens_delete_client_side_access_token(
            token="token",
            request={"key": "value"},
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/client-side-access-tokens/{jsonable_encoder(token)}",
            method="DELETE",
            json=request,
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.Optional[typing.Any],
                    construct_type(
                        type_=typing.Optional[typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        construct_type(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncClientSideAccessTokensClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def client_side_access_tokens_create_client_side_access_token(
        self,
        *,
        policy: typing.Sequence[ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem],
        hostname: str,
        expires_at: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ClientSideAccessTokensCreateClientSideAccessTokenResponse:
        """
        Create a new client side access token with the specified configuration.

        Parameters
        ----------
        policy : typing.Sequence[ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem]

        hostname : str
            The hostname of the client side application. Please specify the full URL including the protocol (http or https).

        expires_at : typing.Optional[str]
            The expiration date of the token. If not provided, the token will expire in 5 minutes

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        ClientSideAccessTokensCreateClientSideAccessTokenResponse
            201

        Examples
        --------
        import asyncio

        from letta_client import AsyncLetta
        from letta_client.client_side_access_tokens import (
            ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem,
        )

        client = AsyncLetta(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.client_side_access_tokens.client_side_access_tokens_create_client_side_access_token(
                policy=[
                    ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem(
                        id="id",
                        access=["read_messages"],
                    )
                ],
                hostname="hostname",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/client-side-access-tokens",
            method="POST",
            json={
                "policy": convert_and_respect_annotation_metadata(
                    object_=policy,
                    annotation=typing.Sequence[ClientSideAccessTokensCreateClientSideAccessTokenRequestPolicyItem],
                    direction="write",
                ),
                "hostname": hostname,
                "expires_at": expires_at,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    ClientSideAccessTokensCreateClientSideAccessTokenResponse,
                    construct_type(
                        type_=ClientSideAccessTokensCreateClientSideAccessTokenResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        construct_type(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def client_side_access_tokens_delete_client_side_access_token(
        self,
        token: str,
        *,
        request: typing.Optional[typing.Any] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Optional[typing.Any]:
        """
        Delete a client side access token.

        Parameters
        ----------
        token : str
            The access token to delete

        request : typing.Optional[typing.Any]

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        typing.Optional[typing.Any]
            204

        Examples
        --------
        import asyncio

        from letta_client import AsyncLetta

        client = AsyncLetta(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.client_side_access_tokens.client_side_access_tokens_delete_client_side_access_token(
                token="token",
                request={"key": "value"},
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/client-side-access-tokens/{jsonable_encoder(token)}",
            method="DELETE",
            json=request,
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.Optional[typing.Any],
                    construct_type(
                        type_=typing.Optional[typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        construct_type(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
