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

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..types.auth_response import AuthResponse

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


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

    def get_token(
        self,
        *,
        client_id: typing.Optional[str] = OMIT,
        client_secret: typing.Optional[str] = OMIT,
        grant_type: typing.Literal["client_credentials"] = "client_credentials",
        request_options: typing.Optional[RequestOptions] = None
    ) -> AuthResponse:
        """
        Parameters
        ----------
        client_id : typing.Optional[str]

        client_secret : typing.Optional[str]

        grant_type : typing.Optional[typing.Literal["client_credentials"]]

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

        Returns
        -------
        AuthResponse
            A response to a query.

        Examples
        --------
        from vectara.client import Vectara

        client = Vectara(
            api_key="YOUR_API_KEY",
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.auth.get_token()
        """
        _response = self._client_wrapper.httpx_client.request(
            "oauth2/token",
            base_url=self._client_wrapper.get_environment().auth,
            method="POST",
            data={"client_id": client_id, "client_secret": client_secret, "grant_type": grant_type},
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    AuthResponse,
                    parse_obj_as(
                        type_=AuthResponse,  # 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 AsyncAuthClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def get_token(
        self,
        *,
        client_id: typing.Optional[str] = OMIT,
        client_secret: typing.Optional[str] = OMIT,
        grant_type: typing.Optional[typing.Literal["client_credentials"]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None
    ) -> AuthResponse:
        """
        Parameters
        ----------
        client_id : typing.Optional[str]

        client_secret : typing.Optional[str]

        grant_type : typing.Optional[typing.Literal["client_credentials"]]

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

        Returns
        -------
        AuthResponse
            A response to a query.

        Examples
        --------
        import asyncio

        from vectara.client import AsyncVectara

        client = AsyncVectara(
            api_key="YOUR_API_KEY",
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )


        async def main() -> None:
            await client.auth.get_token()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "oauth2/token",
            base_url=self._client_wrapper.get_environment().auth,
            method="POST",
            json={"client_id": client_id, "client_secret": client_secret, "grant_type": grant_type},
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    AuthResponse,
                    parse_obj_as(
                        type_=AuthResponse,  # 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)
