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

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.pagination import AsyncPager, SyncPager
from ..core.request_options import RequestOptions
from ..types.api_key import ApiKey
from ..types.api_key_role import ApiKeyRole
from ..types.corpus_key import CorpusKey
from .raw_client import AsyncRawApiKeysClient, RawApiKeysClient

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


class ApiKeysClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawApiKeysClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawApiKeysClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawApiKeysClient
        """
        return self._raw_client

    def list(
        self,
        *,
        limit: typing.Optional[int] = None,
        page_key: typing.Optional[str] = None,
        corpus_key: typing.Optional[CorpusKey] = None,
        api_key_role: typing.Optional[ApiKeyRole] = None,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[ApiKey]:
        """
        Retrieve a list of API keys for the customer account with optional filtering.

        Parameters
        ----------
        limit : typing.Optional[int]
            Max number of API keys to return at one time.

        page_key : typing.Optional[str]
            Used to retrieve the next page of API keys after the limit has been reached.

        corpus_key : typing.Optional[CorpusKey]
            Filters the API keys to only those with permissions on the specified corpus key.

        api_key_role : typing.Optional[ApiKeyRole]
            Filter API keys by their role.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        SyncPager[ApiKey]
            An array of API keys.

        Examples
        --------
        from vectara import Vectara
        client = Vectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        response = client.api_keys.list(corpus_key='my-corpus', )
        for item in response:
            yield item
        # alternatively, you can paginate page-by-page
        for page in response.iter_pages():
            yield page
        """
        response = self._raw_client.list(
            limit=limit,
            page_key=page_key,
            corpus_key=corpus_key,
            api_key_role=api_key_role,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data

    def create(
        self,
        *,
        name: str,
        api_key_role: ApiKeyRole,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        corpus_keys: typing.Optional[typing.Sequence[CorpusKey]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ApiKey:
        """
        An API key is to authenticate when calling Vectara APIs.

        Parameters
        ----------
        name : str
            The human-readable name of the API key.

        api_key_role : ApiKeyRole

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

        corpus_keys : typing.Optional[typing.Sequence[CorpusKey]]
            Corpora this API key has roles on if it is not a Personal API key.
            This property should be null or missing if this `api_key_role` is
            `personal`.

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

        Returns
        -------
        ApiKey
            An API key object, used to query the Vectara API with the assigned roles.

        Examples
        --------
        from vectara import Vectara
        client = Vectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        client.api_keys.create(name='name', api_key_role="serving", )
        """
        response = self._raw_client.create(
            name=name,
            api_key_role=api_key_role,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            corpus_keys=corpus_keys,
            request_options=request_options,
        )
        return response.data

    def get(
        self,
        api_key_id: str,
        *,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ApiKey:
        """
        Retrieve details of a specific API key by its ID.

        Parameters
        ----------
        api_key_id : str
            The ID of the API key.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        ApiKey
            The API key.

        Examples
        --------
        from vectara import Vectara
        client = Vectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        client.api_keys.get(api_key_id='api_key_id', )
        """
        response = self._raw_client.get(
            api_key_id,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data

    def delete(
        self,
        api_key_id: str,
        *,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Delete API keys to help you manage the security and lifecycle of API keys in your application.

        Parameters
        ----------
        api_key_id : str
            The ID of the API key.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        None

        Examples
        --------
        from vectara import Vectara
        client = Vectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        client.api_keys.delete(api_key_id='api_key_id', )
        """
        response = self._raw_client.delete(
            api_key_id,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data

    def update(
        self,
        api_key_id: str,
        *,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        enabled: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ApiKey:
        """
        Update an API key such as the roles attached to the key.

        Parameters
        ----------
        api_key_id : str
            The ID of the API key.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

        enabled : typing.Optional[bool]
            Indicates whether to disable or enable an API key.

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

        Returns
        -------
        ApiKey
            The API key.

        Examples
        --------
        from vectara import Vectara
        client = Vectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        client.api_keys.update(api_key_id='api_key_id', )
        """
        response = self._raw_client.update(
            api_key_id,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            enabled=enabled,
            request_options=request_options,
        )
        return response.data


class AsyncApiKeysClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawApiKeysClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawApiKeysClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawApiKeysClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        limit: typing.Optional[int] = None,
        page_key: typing.Optional[str] = None,
        corpus_key: typing.Optional[CorpusKey] = None,
        api_key_role: typing.Optional[ApiKeyRole] = None,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[ApiKey]:
        """
        Retrieve a list of API keys for the customer account with optional filtering.

        Parameters
        ----------
        limit : typing.Optional[int]
            Max number of API keys to return at one time.

        page_key : typing.Optional[str]
            Used to retrieve the next page of API keys after the limit has been reached.

        corpus_key : typing.Optional[CorpusKey]
            Filters the API keys to only those with permissions on the specified corpus key.

        api_key_role : typing.Optional[ApiKeyRole]
            Filter API keys by their role.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        AsyncPager[ApiKey]
            An array of API keys.

        Examples
        --------
        from vectara import AsyncVectara
        import asyncio
        client = AsyncVectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        async def main() -> None:
            response = await client.api_keys.list(corpus_key='my-corpus', )
            async for item in response:
                yield item

            # alternatively, you can paginate page-by-page
            async for page in response.iter_pages():
                yield page
        asyncio.run(main())
        """
        response = await self._raw_client.list(
            limit=limit,
            page_key=page_key,
            corpus_key=corpus_key,
            api_key_role=api_key_role,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data

    async def create(
        self,
        *,
        name: str,
        api_key_role: ApiKeyRole,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        corpus_keys: typing.Optional[typing.Sequence[CorpusKey]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ApiKey:
        """
        An API key is to authenticate when calling Vectara APIs.

        Parameters
        ----------
        name : str
            The human-readable name of the API key.

        api_key_role : ApiKeyRole

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

        corpus_keys : typing.Optional[typing.Sequence[CorpusKey]]
            Corpora this API key has roles on if it is not a Personal API key.
            This property should be null or missing if this `api_key_role` is
            `personal`.

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

        Returns
        -------
        ApiKey
            An API key object, used to query the Vectara API with the assigned roles.

        Examples
        --------
        from vectara import AsyncVectara
        import asyncio
        client = AsyncVectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        async def main() -> None:
            await client.api_keys.create(name='name', api_key_role="serving", )
        asyncio.run(main())
        """
        response = await self._raw_client.create(
            name=name,
            api_key_role=api_key_role,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            corpus_keys=corpus_keys,
            request_options=request_options,
        )
        return response.data

    async def get(
        self,
        api_key_id: str,
        *,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ApiKey:
        """
        Retrieve details of a specific API key by its ID.

        Parameters
        ----------
        api_key_id : str
            The ID of the API key.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        ApiKey
            The API key.

        Examples
        --------
        from vectara import AsyncVectara
        import asyncio
        client = AsyncVectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        async def main() -> None:
            await client.api_keys.get(api_key_id='api_key_id', )
        asyncio.run(main())
        """
        response = await self._raw_client.get(
            api_key_id,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data

    async def delete(
        self,
        api_key_id: str,
        *,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Delete API keys to help you manage the security and lifecycle of API keys in your application.

        Parameters
        ----------
        api_key_id : str
            The ID of the API key.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

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

        Returns
        -------
        None

        Examples
        --------
        from vectara import AsyncVectara
        import asyncio
        client = AsyncVectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        async def main() -> None:
            await client.api_keys.delete(api_key_id='api_key_id', )
        asyncio.run(main())
        """
        response = await self._raw_client.delete(
            api_key_id,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )
        return response.data

    async def update(
        self,
        api_key_id: str,
        *,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        enabled: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ApiKey:
        """
        Update an API key such as the roles attached to the key.

        Parameters
        ----------
        api_key_id : str
            The ID of the API key.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

        enabled : typing.Optional[bool]
            Indicates whether to disable or enable an API key.

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

        Returns
        -------
        ApiKey
            The API key.

        Examples
        --------
        from vectara import AsyncVectara
        import asyncio
        client = AsyncVectara(api_key="YOUR_API_KEY", client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", )
        async def main() -> None:
            await client.api_keys.update(api_key_id='api_key_id', )
        asyncio.run(main())
        """
        response = await self._raw_client.update(
            api_key_id,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            enabled=enabled,
            request_options=request_options,
        )
        return response.data
