# 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.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.agent_api_key import AgentApiKey
from ..types.agent_api_key_type import AgentApiKeyType
from ..types.create_api_key_response import CreateApiKeyResponse
from ..types.http_validation_error import HttpValidationError

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


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

    def agent_api_keys_list(
        self,
        *,
        agent_id: typing.Optional[str] = None,
        agent_name: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page_number: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.List[AgentApiKey]]:
        """
        List API keys for an agent ID.

        Parameters
        ----------
        agent_id : typing.Optional[str]

        agent_name : typing.Optional[str]

        limit : typing.Optional[int]

        page_number : typing.Optional[int]

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

        Returns
        -------
        HttpResponse[typing.List[AgentApiKey]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "agent_api_keys",
            method="GET",
            params={
                "agent_id": agent_id,
                "agent_name": agent_name,
                "limit": limit,
                "page_number": page_number,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[AgentApiKey],
                    parse_obj_as(
                        type_=typing.List[AgentApiKey],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def agent_api_keys_create(
        self,
        *,
        name: str,
        agent_id: typing.Optional[str] = OMIT,
        agent_name: typing.Optional[str] = OMIT,
        api_key: typing.Optional[str] = OMIT,
        api_key_type: typing.Optional[AgentApiKeyType] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateApiKeyResponse]:
        """
        Parameters
        ----------
        name : str
            The name of the agent's API key.

        agent_id : typing.Optional[str]
            The UUID of the agent

        agent_name : typing.Optional[str]
            The name of the agent - if not provided, the agent_id must be set.

        api_key : typing.Optional[str]
            Optionally provide the API key value - if not set, one will be generated.

        api_key_type : typing.Optional[AgentApiKeyType]
            The type of the agent API key (external by default).

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

        Returns
        -------
        HttpResponse[CreateApiKeyResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "agent_api_keys",
            method="POST",
            json={
                "agent_id": agent_id,
                "agent_name": agent_name,
                "api_key": api_key,
                "api_key_type": api_key_type,
                "name": name,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateApiKeyResponse,
                    parse_obj_as(
                        type_=CreateApiKeyResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def agent_api_keys_name_delete(
        self,
        api_key_name: str,
        *,
        agent_id: typing.Optional[str] = None,
        agent_name: typing.Optional[str] = None,
        api_key_type: typing.Optional[AgentApiKeyType] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[str]:
        """
        Delete API key by name.

        Parameters
        ----------
        api_key_name : str

        agent_id : typing.Optional[str]

        agent_name : typing.Optional[str]

        api_key_type : typing.Optional[AgentApiKeyType]

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

        Returns
        -------
        HttpResponse[str]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"agent_api_keys/name/{jsonable_encoder(api_key_name)}",
            method="DELETE",
            params={
                "agent_id": agent_id,
                "agent_name": agent_name,
                "api_key_type": api_key_type,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    str,
                    parse_obj_as(
                        type_=str,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def agent_api_keys_name_retrieve(
        self,
        name: str,
        *,
        agent_id: typing.Optional[str] = None,
        agent_name: typing.Optional[str] = None,
        api_key_type: typing.Optional[AgentApiKeyType] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[AgentApiKey]:
        """
        Return named API key for the agent ID.

        Parameters
        ----------
        name : str

        agent_id : typing.Optional[str]

        agent_name : typing.Optional[str]

        api_key_type : typing.Optional[AgentApiKeyType]

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

        Returns
        -------
        HttpResponse[AgentApiKey]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"agent_api_keys/name/{jsonable_encoder(name)}",
            method="GET",
            params={
                "agent_id": agent_id,
                "agent_name": agent_name,
                "api_key_type": api_key_type,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AgentApiKey,
                    parse_obj_as(
                        type_=AgentApiKey,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def agent_api_keys_retrieve(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[AgentApiKey]:
        """
        Return API key by ID.

        Parameters
        ----------
        id : str

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

        Returns
        -------
        HttpResponse[AgentApiKey]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"agent_api_keys/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AgentApiKey,
                    parse_obj_as(
                        type_=AgentApiKey,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def agent_api_keys_delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[str]:
        """
        Delete API key by ID.

        Parameters
        ----------
        id : str

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

        Returns
        -------
        HttpResponse[str]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"agent_api_keys/{jsonable_encoder(id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    str,
                    parse_obj_as(
                        type_=str,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


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

    async def agent_api_keys_list(
        self,
        *,
        agent_id: typing.Optional[str] = None,
        agent_name: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page_number: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.List[AgentApiKey]]:
        """
        List API keys for an agent ID.

        Parameters
        ----------
        agent_id : typing.Optional[str]

        agent_name : typing.Optional[str]

        limit : typing.Optional[int]

        page_number : typing.Optional[int]

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

        Returns
        -------
        AsyncHttpResponse[typing.List[AgentApiKey]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "agent_api_keys",
            method="GET",
            params={
                "agent_id": agent_id,
                "agent_name": agent_name,
                "limit": limit,
                "page_number": page_number,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.List[AgentApiKey],
                    parse_obj_as(
                        type_=typing.List[AgentApiKey],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def agent_api_keys_create(
        self,
        *,
        name: str,
        agent_id: typing.Optional[str] = OMIT,
        agent_name: typing.Optional[str] = OMIT,
        api_key: typing.Optional[str] = OMIT,
        api_key_type: typing.Optional[AgentApiKeyType] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateApiKeyResponse]:
        """
        Parameters
        ----------
        name : str
            The name of the agent's API key.

        agent_id : typing.Optional[str]
            The UUID of the agent

        agent_name : typing.Optional[str]
            The name of the agent - if not provided, the agent_id must be set.

        api_key : typing.Optional[str]
            Optionally provide the API key value - if not set, one will be generated.

        api_key_type : typing.Optional[AgentApiKeyType]
            The type of the agent API key (external by default).

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

        Returns
        -------
        AsyncHttpResponse[CreateApiKeyResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "agent_api_keys",
            method="POST",
            json={
                "agent_id": agent_id,
                "agent_name": agent_name,
                "api_key": api_key,
                "api_key_type": api_key_type,
                "name": name,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateApiKeyResponse,
                    parse_obj_as(
                        type_=CreateApiKeyResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def agent_api_keys_name_delete(
        self,
        api_key_name: str,
        *,
        agent_id: typing.Optional[str] = None,
        agent_name: typing.Optional[str] = None,
        api_key_type: typing.Optional[AgentApiKeyType] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[str]:
        """
        Delete API key by name.

        Parameters
        ----------
        api_key_name : str

        agent_id : typing.Optional[str]

        agent_name : typing.Optional[str]

        api_key_type : typing.Optional[AgentApiKeyType]

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

        Returns
        -------
        AsyncHttpResponse[str]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"agent_api_keys/name/{jsonable_encoder(api_key_name)}",
            method="DELETE",
            params={
                "agent_id": agent_id,
                "agent_name": agent_name,
                "api_key_type": api_key_type,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    str,
                    parse_obj_as(
                        type_=str,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def agent_api_keys_name_retrieve(
        self,
        name: str,
        *,
        agent_id: typing.Optional[str] = None,
        agent_name: typing.Optional[str] = None,
        api_key_type: typing.Optional[AgentApiKeyType] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[AgentApiKey]:
        """
        Return named API key for the agent ID.

        Parameters
        ----------
        name : str

        agent_id : typing.Optional[str]

        agent_name : typing.Optional[str]

        api_key_type : typing.Optional[AgentApiKeyType]

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

        Returns
        -------
        AsyncHttpResponse[AgentApiKey]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"agent_api_keys/name/{jsonable_encoder(name)}",
            method="GET",
            params={
                "agent_id": agent_id,
                "agent_name": agent_name,
                "api_key_type": api_key_type,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AgentApiKey,
                    parse_obj_as(
                        type_=AgentApiKey,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def agent_api_keys_retrieve(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[AgentApiKey]:
        """
        Return API key by ID.

        Parameters
        ----------
        id : str

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

        Returns
        -------
        AsyncHttpResponse[AgentApiKey]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"agent_api_keys/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AgentApiKey,
                    parse_obj_as(
                        type_=AgentApiKey,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def agent_api_keys_delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[str]:
        """
        Delete API key by ID.

        Parameters
        ----------
        id : str

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

        Returns
        -------
        AsyncHttpResponse[str]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"agent_api_keys/{jsonable_encoder(id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    str,
                    parse_obj_as(
                        type_=str,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
