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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.knowledge_base_response import KnowledgeBaseResponse
from ..core.pydantic_utilities import parse_obj_as
from ..errors.method_not_allowed_error import MethodNotAllowedError
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 KnowledgeBaseClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def create_knowledge_base(
        self,
        *,
        type: typing.Optional[str] = OMIT,
        credentials: typing.Optional[
            typing.Dict[str, typing.Optional[typing.Any]]
        ] = OMIT,
        seed_topics: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeBaseResponse:
        """
        Add a new knowledge base

        Parameters
        ----------
        type : typing.Optional[str]
            Type of the knowledge base

        credentials : typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]]
            Credentials of the knowledge base

        seed_topics : typing.Optional[typing.Sequence[str]]
            Seed topics of the knowledge base

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

        Returns
        -------
        KnowledgeBaseResponse
            Successful operation

        Examples
        --------
        from NeuralTrust import NeuralTrustApi

        client = NeuralTrustApi(
            api_key="YOUR_API_KEY",
        )
        client.knowledge_base.create_knowledge_base()
        """
        _response = self._client_wrapper.httpx_client.request(
            "knowledge-base",
            method="POST",
            json={
                "type": type,
                "credentials": credentials,
                "seed_topics": seed_topics,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 405:
                raise MethodNotAllowedError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            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 get_knowledge_base(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> KnowledgeBaseResponse:
        """
        Get a knowledge base

        Parameters
        ----------
        id : str
            ID of the knowledge base to retrieve

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

        Returns
        -------
        KnowledgeBaseResponse
            Successful operation

        Examples
        --------
        from NeuralTrust import NeuralTrustApi

        client = NeuralTrustApi(
            api_key="YOUR_API_KEY",
        )
        client.knowledge_base.get_knowledge_base(
            id="id",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"knowledge-base/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 405:
                raise MethodNotAllowedError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            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 delete_knowledge_base(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> KnowledgeBaseResponse:
        """
        Delete a knowledge base

        Parameters
        ----------
        id : str
            ID of the knowledge base to delete

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

        Returns
        -------
        KnowledgeBaseResponse
            Successful operation

        Examples
        --------
        from NeuralTrust import NeuralTrustApi

        client = NeuralTrustApi(
            api_key="YOUR_API_KEY",
        )
        client.knowledge_base.delete_knowledge_base(
            id="id",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"knowledge-base/{jsonable_encoder(id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # 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 AsyncKnowledgeBaseClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create_knowledge_base(
        self,
        *,
        type: typing.Optional[str] = OMIT,
        credentials: typing.Optional[
            typing.Dict[str, typing.Optional[typing.Any]]
        ] = OMIT,
        seed_topics: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> KnowledgeBaseResponse:
        """
        Add a new knowledge base

        Parameters
        ----------
        type : typing.Optional[str]
            Type of the knowledge base

        credentials : typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]]
            Credentials of the knowledge base

        seed_topics : typing.Optional[typing.Sequence[str]]
            Seed topics of the knowledge base

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

        Returns
        -------
        KnowledgeBaseResponse
            Successful operation

        Examples
        --------
        import asyncio

        from NeuralTrust import AsyncNeuralTrustApi

        client = AsyncNeuralTrustApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.knowledge_base.create_knowledge_base()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "knowledge-base",
            method="POST",
            json={
                "type": type,
                "credentials": credentials,
                "seed_topics": seed_topics,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 405:
                raise MethodNotAllowedError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            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 get_knowledge_base(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> KnowledgeBaseResponse:
        """
        Get a knowledge base

        Parameters
        ----------
        id : str
            ID of the knowledge base to retrieve

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

        Returns
        -------
        KnowledgeBaseResponse
            Successful operation

        Examples
        --------
        import asyncio

        from NeuralTrust import AsyncNeuralTrustApi

        client = AsyncNeuralTrustApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.knowledge_base.get_knowledge_base(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"knowledge-base/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 405:
                raise MethodNotAllowedError(
                    typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            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 delete_knowledge_base(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> KnowledgeBaseResponse:
        """
        Delete a knowledge base

        Parameters
        ----------
        id : str
            ID of the knowledge base to delete

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

        Returns
        -------
        KnowledgeBaseResponse
            Successful operation

        Examples
        --------
        import asyncio

        from NeuralTrust import AsyncNeuralTrustApi

        client = AsyncNeuralTrustApi(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.knowledge_base.delete_knowledge_base(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"knowledge-base/{jsonable_encoder(id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    KnowledgeBaseResponse,
                    parse_obj_as(
                        type_=KnowledgeBaseResponse,  # 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)
