# 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.data_sink import DataSink
from ..core.pydantic_utilities import parse_obj_as
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.http_validation_error import HttpValidationError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..types.configurable_data_sink_names import ConfigurableDataSinkNames
from ..types.data_sink_create_component import DataSinkCreateComponent
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.jsonable_encoder import jsonable_encoder
from .types.data_sink_update_component import DataSinkUpdateComponent
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def list_data_sinks(
        self,
        *,
        project_id: typing.Optional[str] = None,
        organization_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[DataSink]:
        """
        List data sinks for a given project.

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

        organization_id : typing.Optional[str]

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

        Returns
        -------
        typing.List[DataSink]
            Successful Response

        Examples
        --------
        from llama_index import LlamaCloud

        client = LlamaCloud(
            token="YOUR_TOKEN",
        )
        client.data_sinks.list_data_sinks()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/v1/data-sinks",
            method="GET",
            params={
                "project_id": project_id,
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[DataSink],
                    parse_obj_as(
                        type_=typing.List[DataSink],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def create_data_sink(
        self,
        *,
        name: str,
        sink_type: ConfigurableDataSinkNames,
        component: DataSinkCreateComponent,
        project_id: typing.Optional[str] = None,
        organization_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> DataSink:
        """
        Create a new data sink.

        Parameters
        ----------
        name : str
            The name of the data sink.

        sink_type : ConfigurableDataSinkNames

        component : DataSinkCreateComponent
            Component that implements the data sink

        project_id : typing.Optional[str]

        organization_id : typing.Optional[str]

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

        Returns
        -------
        DataSink
            Successful Response

        Examples
        --------
        from llama_index import LlamaCloud

        client = LlamaCloud(
            token="YOUR_TOKEN",
        )
        client.data_sinks.create_data_sink(
            name="name",
            sink_type="PINECONE",
            component={"key": "value"},
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/v1/data-sinks",
            method="POST",
            params={
                "project_id": project_id,
                "organization_id": organization_id,
            },
            json={
                "name": name,
                "sink_type": sink_type,
                "component": convert_and_respect_annotation_metadata(
                    object_=component, annotation=DataSinkCreateComponent, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DataSink,
                    parse_obj_as(
                        type_=DataSink,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def upsert_data_sink(
        self,
        *,
        name: str,
        sink_type: ConfigurableDataSinkNames,
        component: DataSinkCreateComponent,
        project_id: typing.Optional[str] = None,
        organization_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> DataSink:
        """
        Upserts a data sink.
        Updates if a data sink with the same name and project_id already exists. Otherwise, creates a new data sink.

        Parameters
        ----------
        name : str
            The name of the data sink.

        sink_type : ConfigurableDataSinkNames

        component : DataSinkCreateComponent
            Component that implements the data sink

        project_id : typing.Optional[str]

        organization_id : typing.Optional[str]

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

        Returns
        -------
        DataSink
            Successful Response

        Examples
        --------
        from llama_index import LlamaCloud

        client = LlamaCloud(
            token="YOUR_TOKEN",
        )
        client.data_sinks.upsert_data_sink(
            name="name",
            sink_type="PINECONE",
            component={"key": "value"},
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/v1/data-sinks",
            method="PUT",
            params={
                "project_id": project_id,
                "organization_id": organization_id,
            },
            json={
                "name": name,
                "sink_type": sink_type,
                "component": convert_and_respect_annotation_metadata(
                    object_=component, annotation=DataSinkCreateComponent, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DataSink,
                    parse_obj_as(
                        type_=DataSink,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def get_data_sink(self, data_sink_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> DataSink:
        """
        Get a data sink by ID.

        Parameters
        ----------
        data_sink_id : str

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

        Returns
        -------
        DataSink
            Successful Response

        Examples
        --------
        from llama_index import LlamaCloud

        client = LlamaCloud(
            token="YOUR_TOKEN",
        )
        client.data_sinks.get_data_sink(
            data_sink_id="data_sink_id",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/v1/data-sinks/{jsonable_encoder(data_sink_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DataSink,
                    parse_obj_as(
                        type_=DataSink,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def update_data_sink(
        self,
        data_sink_id: str,
        *,
        sink_type: ConfigurableDataSinkNames,
        name: typing.Optional[str] = OMIT,
        component: typing.Optional[DataSinkUpdateComponent] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> DataSink:
        """
        Update a data sink by ID.

        Parameters
        ----------
        data_sink_id : str

        sink_type : ConfigurableDataSinkNames

        name : typing.Optional[str]
            The name of the data sink.

        component : typing.Optional[DataSinkUpdateComponent]
            Component that implements the data sink

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

        Returns
        -------
        DataSink
            Successful Response

        Examples
        --------
        from llama_index import LlamaCloud

        client = LlamaCloud(
            token="YOUR_TOKEN",
        )
        client.data_sinks.update_data_sink(
            data_sink_id="data_sink_id",
            sink_type="PINECONE",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/v1/data-sinks/{jsonable_encoder(data_sink_id)}",
            method="PUT",
            json={
                "name": name,
                "sink_type": sink_type,
                "component": convert_and_respect_annotation_metadata(
                    object_=component, annotation=DataSinkUpdateComponent, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DataSink,
                    parse_obj_as(
                        type_=DataSink,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete_data_sink(self, data_sink_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Delete a data sink by ID.

        Parameters
        ----------
        data_sink_id : str

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

        Returns
        -------
        None

        Examples
        --------
        from llama_index import LlamaCloud

        client = LlamaCloud(
            token="YOUR_TOKEN",
        )
        client.data_sinks.delete_data_sink(
            data_sink_id="data_sink_id",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/v1/data-sinks/{jsonable_encoder(data_sink_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def list_data_sinks(
        self,
        *,
        project_id: typing.Optional[str] = None,
        organization_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[DataSink]:
        """
        List data sinks for a given project.

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

        organization_id : typing.Optional[str]

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

        Returns
        -------
        typing.List[DataSink]
            Successful Response

        Examples
        --------
        import asyncio

        from llama_index import AsyncLlamaCloud

        client = AsyncLlamaCloud(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.data_sinks.list_data_sinks()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/v1/data-sinks",
            method="GET",
            params={
                "project_id": project_id,
                "organization_id": organization_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[DataSink],
                    parse_obj_as(
                        type_=typing.List[DataSink],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def create_data_sink(
        self,
        *,
        name: str,
        sink_type: ConfigurableDataSinkNames,
        component: DataSinkCreateComponent,
        project_id: typing.Optional[str] = None,
        organization_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> DataSink:
        """
        Create a new data sink.

        Parameters
        ----------
        name : str
            The name of the data sink.

        sink_type : ConfigurableDataSinkNames

        component : DataSinkCreateComponent
            Component that implements the data sink

        project_id : typing.Optional[str]

        organization_id : typing.Optional[str]

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

        Returns
        -------
        DataSink
            Successful Response

        Examples
        --------
        import asyncio

        from llama_index import AsyncLlamaCloud

        client = AsyncLlamaCloud(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.data_sinks.create_data_sink(
                name="name",
                sink_type="PINECONE",
                component={"key": "value"},
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/v1/data-sinks",
            method="POST",
            params={
                "project_id": project_id,
                "organization_id": organization_id,
            },
            json={
                "name": name,
                "sink_type": sink_type,
                "component": convert_and_respect_annotation_metadata(
                    object_=component, annotation=DataSinkCreateComponent, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DataSink,
                    parse_obj_as(
                        type_=DataSink,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def upsert_data_sink(
        self,
        *,
        name: str,
        sink_type: ConfigurableDataSinkNames,
        component: DataSinkCreateComponent,
        project_id: typing.Optional[str] = None,
        organization_id: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> DataSink:
        """
        Upserts a data sink.
        Updates if a data sink with the same name and project_id already exists. Otherwise, creates a new data sink.

        Parameters
        ----------
        name : str
            The name of the data sink.

        sink_type : ConfigurableDataSinkNames

        component : DataSinkCreateComponent
            Component that implements the data sink

        project_id : typing.Optional[str]

        organization_id : typing.Optional[str]

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

        Returns
        -------
        DataSink
            Successful Response

        Examples
        --------
        import asyncio

        from llama_index import AsyncLlamaCloud

        client = AsyncLlamaCloud(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.data_sinks.upsert_data_sink(
                name="name",
                sink_type="PINECONE",
                component={"key": "value"},
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/v1/data-sinks",
            method="PUT",
            params={
                "project_id": project_id,
                "organization_id": organization_id,
            },
            json={
                "name": name,
                "sink_type": sink_type,
                "component": convert_and_respect_annotation_metadata(
                    object_=component, annotation=DataSinkCreateComponent, direction="write"
                ),
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DataSink,
                    parse_obj_as(
                        type_=DataSink,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def get_data_sink(
        self, data_sink_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> DataSink:
        """
        Get a data sink by ID.

        Parameters
        ----------
        data_sink_id : str

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

        Returns
        -------
        DataSink
            Successful Response

        Examples
        --------
        import asyncio

        from llama_index import AsyncLlamaCloud

        client = AsyncLlamaCloud(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.data_sinks.get_data_sink(
                data_sink_id="data_sink_id",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/v1/data-sinks/{jsonable_encoder(data_sink_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DataSink,
                    parse_obj_as(
                        type_=DataSink,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def update_data_sink(
        self,
        data_sink_id: str,
        *,
        sink_type: ConfigurableDataSinkNames,
        name: typing.Optional[str] = OMIT,
        component: typing.Optional[DataSinkUpdateComponent] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> DataSink:
        """
        Update a data sink by ID.

        Parameters
        ----------
        data_sink_id : str

        sink_type : ConfigurableDataSinkNames

        name : typing.Optional[str]
            The name of the data sink.

        component : typing.Optional[DataSinkUpdateComponent]
            Component that implements the data sink

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

        Returns
        -------
        DataSink
            Successful Response

        Examples
        --------
        import asyncio

        from llama_index import AsyncLlamaCloud

        client = AsyncLlamaCloud(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.data_sinks.update_data_sink(
                data_sink_id="data_sink_id",
                sink_type="PINECONE",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/v1/data-sinks/{jsonable_encoder(data_sink_id)}",
            method="PUT",
            json={
                "name": name,
                "sink_type": sink_type,
                "component": convert_and_respect_annotation_metadata(
                    object_=component, annotation=DataSinkUpdateComponent, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DataSink,
                    parse_obj_as(
                        type_=DataSink,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete_data_sink(
        self, data_sink_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Delete a data sink by ID.

        Parameters
        ----------
        data_sink_id : str

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from llama_index import AsyncLlamaCloud

        client = AsyncLlamaCloud(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.data_sinks.delete_data_sink(
                data_sink_id="data_sink_id",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/v1/data-sinks/{jsonable_encoder(data_sink_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    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, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
