# 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.webhook import Webhook
from ..core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..types.actions_enum import ActionsEnum
from .types.info_webhooks_response import InfoWebhooksResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..types.webhook_serializer_for_update import WebhookSerializerForUpdate
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def list(
        self, *, project: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.List[Webhook]:
        """
        List all webhooks set up for your organization.

        Parameters
        ----------
        project : typing.Optional[str]
            Project ID

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

        Returns
        -------
        typing.List[Webhook]


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/webhooks/",
            method="GET",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Webhook],
                    construct_type(
                        type_=typing.List[Webhook],  # 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(
        self,
        *,
        url: str,
        actions: typing.Optional[typing.Sequence[ActionsEnum]] = OMIT,
        headers: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        project: typing.Optional[int] = OMIT,
        send_for_all_actions: typing.Optional[bool] = OMIT,
        send_payload: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Webhook:
        """
        Create a webhook for your organization.

        Parameters
        ----------
        url : str
            URL of webhook

        actions : typing.Optional[typing.Sequence[ActionsEnum]]

        headers : typing.Optional[typing.Optional[typing.Any]]
            Key Value Json of headers

        is_active : typing.Optional[bool]
            If value is False the webhook is disabled

        project : typing.Optional[int]

        send_for_all_actions : typing.Optional[bool]
            If value is False - used only for actions from WebhookAction

        send_payload : typing.Optional[bool]
            If value is False send only action

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

        Returns
        -------
        Webhook


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.create(
            url="url",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/webhooks/",
            method="POST",
            json={
                "actions": actions,
                "headers": headers,
                "is_active": is_active,
                "project": project,
                "send_for_all_actions": send_for_all_actions,
                "send_payload": send_payload,
                "url": url,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Webhook,
                    construct_type(
                        type_=Webhook,  # 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 info(
        self,
        *,
        organization_only: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> InfoWebhooksResponse:
        """
        Get descriptions of all available webhook actions to set up webhooks.

        Parameters
        ----------
        organization_only : typing.Optional[bool]
            organization-only or not

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

        Returns
        -------
        InfoWebhooksResponse
            Object with webhook action descriptions.

        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.info()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/webhooks/info/",
            method="GET",
            params={
                "organization-only": organization_only,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    InfoWebhooksResponse,
                    construct_type(
                        type_=InfoWebhooksResponse,  # 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(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> Webhook:
        """
        Parameters
        ----------
        id : int

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

        Returns
        -------
        Webhook


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.get(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/webhooks/{jsonable_encoder(id)}/",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Webhook,
                    construct_type(
                        type_=Webhook,  # 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(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Parameters
        ----------
        id : int

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.delete(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/webhooks/{jsonable_encoder(id)}/",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            _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(
        self,
        id: int,
        *,
        actions: typing.Optional[typing.Sequence[ActionsEnum]] = OMIT,
        headers: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        send_for_all_actions: typing.Optional[bool] = OMIT,
        send_payload: typing.Optional[bool] = OMIT,
        url: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookSerializerForUpdate:
        """
        Parameters
        ----------
        id : int

        actions : typing.Optional[typing.Sequence[ActionsEnum]]

        headers : typing.Optional[typing.Optional[typing.Any]]
            Key Value Json of headers

        is_active : typing.Optional[bool]
            If value is False the webhook is disabled

        send_for_all_actions : typing.Optional[bool]
            If value is False - used only for actions from WebhookAction

        send_payload : typing.Optional[bool]
            If value is False send only action

        url : typing.Optional[str]
            URL of webhook

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

        Returns
        -------
        WebhookSerializerForUpdate


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.webhooks.update(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/webhooks/{jsonable_encoder(id)}/",
            method="PATCH",
            json={
                "actions": actions,
                "headers": headers,
                "is_active": is_active,
                "send_for_all_actions": send_for_all_actions,
                "send_payload": send_payload,
                "url": url,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    WebhookSerializerForUpdate,
                    construct_type(
                        type_=WebhookSerializerForUpdate,  # 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 AsyncWebhooksClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self, *, project: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.List[Webhook]:
        """
        List all webhooks set up for your organization.

        Parameters
        ----------
        project : typing.Optional[str]
            Project ID

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

        Returns
        -------
        typing.List[Webhook]


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.list()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/webhooks/",
            method="GET",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Webhook],
                    construct_type(
                        type_=typing.List[Webhook],  # 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(
        self,
        *,
        url: str,
        actions: typing.Optional[typing.Sequence[ActionsEnum]] = OMIT,
        headers: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        project: typing.Optional[int] = OMIT,
        send_for_all_actions: typing.Optional[bool] = OMIT,
        send_payload: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Webhook:
        """
        Create a webhook for your organization.

        Parameters
        ----------
        url : str
            URL of webhook

        actions : typing.Optional[typing.Sequence[ActionsEnum]]

        headers : typing.Optional[typing.Optional[typing.Any]]
            Key Value Json of headers

        is_active : typing.Optional[bool]
            If value is False the webhook is disabled

        project : typing.Optional[int]

        send_for_all_actions : typing.Optional[bool]
            If value is False - used only for actions from WebhookAction

        send_payload : typing.Optional[bool]
            If value is False send only action

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

        Returns
        -------
        Webhook


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.create(
                url="url",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/webhooks/",
            method="POST",
            json={
                "actions": actions,
                "headers": headers,
                "is_active": is_active,
                "project": project,
                "send_for_all_actions": send_for_all_actions,
                "send_payload": send_payload,
                "url": url,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Webhook,
                    construct_type(
                        type_=Webhook,  # 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 info(
        self,
        *,
        organization_only: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> InfoWebhooksResponse:
        """
        Get descriptions of all available webhook actions to set up webhooks.

        Parameters
        ----------
        organization_only : typing.Optional[bool]
            organization-only or not

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

        Returns
        -------
        InfoWebhooksResponse
            Object with webhook action descriptions.

        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.info()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/webhooks/info/",
            method="GET",
            params={
                "organization-only": organization_only,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    InfoWebhooksResponse,
                    construct_type(
                        type_=InfoWebhooksResponse,  # 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(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> Webhook:
        """
        Parameters
        ----------
        id : int

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

        Returns
        -------
        Webhook


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.get(
                id=1,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/webhooks/{jsonable_encoder(id)}/",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Webhook,
                    construct_type(
                        type_=Webhook,  # 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(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Parameters
        ----------
        id : int

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.delete(
                id=1,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/webhooks/{jsonable_encoder(id)}/",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            _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(
        self,
        id: int,
        *,
        actions: typing.Optional[typing.Sequence[ActionsEnum]] = OMIT,
        headers: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        send_for_all_actions: typing.Optional[bool] = OMIT,
        send_payload: typing.Optional[bool] = OMIT,
        url: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookSerializerForUpdate:
        """
        Parameters
        ----------
        id : int

        actions : typing.Optional[typing.Sequence[ActionsEnum]]

        headers : typing.Optional[typing.Optional[typing.Any]]
            Key Value Json of headers

        is_active : typing.Optional[bool]
            If value is False the webhook is disabled

        send_for_all_actions : typing.Optional[bool]
            If value is False - used only for actions from WebhookAction

        send_payload : typing.Optional[bool]
            If value is False send only action

        url : typing.Optional[str]
            URL of webhook

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

        Returns
        -------
        WebhookSerializerForUpdate


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.webhooks.update(
                id=1,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/webhooks/{jsonable_encoder(id)}/",
            method="PATCH",
            json={
                "actions": actions,
                "headers": headers,
                "is_active": is_active,
                "send_for_all_actions": send_for_all_actions,
                "send_payload": send_payload,
                "url": url,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    WebhookSerializerForUpdate,
                    construct_type(
                        type_=WebhookSerializerForUpdate,  # 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)
