# 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.request_options import RequestOptions
from ..core.unchecked_base_model import construct_type
from ..types.actions_enum import ActionsEnum
from ..types.webhook import Webhook
from ..types.webhook_serializer_for_update import WebhookSerializerForUpdate
from .types.info_webhooks_response import InfoWebhooksResponse

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


class RawWebhooksClient:
    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
    ) -> HttpResponse[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
        -------
        HttpResponse[typing.List[Webhook]]

        """
        _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:
                _data = typing.cast(
                    typing.List[Webhook],
                    construct_type(
                        type_=typing.List[Webhook],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 create(
        self,
        *,
        url: str,
        actions: typing.Optional[typing.Sequence[ActionsEnum]] = OMIT,
        headers: 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,
    ) -> HttpResponse[Webhook]:
        """
        Create a webhook for your organization.

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

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

        headers : 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
        -------
        HttpResponse[Webhook]

        """
        _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:
                _data = typing.cast(
                    Webhook,
                    construct_type(
                        type_=Webhook,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 info(
        self,
        *,
        organization_only: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[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
        -------
        HttpResponse[InfoWebhooksResponse]
            Object with webhook action descriptions.
        """
        _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:
                _data = typing.cast(
                    InfoWebhooksResponse,
                    construct_type(
                        type_=InfoWebhooksResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 get(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> HttpResponse[Webhook]:
        """
        Parameters
        ----------
        id : int

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

        Returns
        -------
        HttpResponse[Webhook]

        """
        _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:
                _data = typing.cast(
                    Webhook,
                    construct_type(
                        type_=Webhook,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 delete(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> HttpResponse[None]:
        """
        Parameters
        ----------
        id : int

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

        Returns
        -------
        HttpResponse[None]
        """
        _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 HttpResponse(response=_response, data=None)
            _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 update(
        self,
        id: int,
        *,
        actions: typing.Optional[typing.Sequence[ActionsEnum]] = OMIT,
        headers: 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,
    ) -> HttpResponse[WebhookSerializerForUpdate]:
        """
        Parameters
        ----------
        id : int

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

        headers : 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
        -------
        HttpResponse[WebhookSerializerForUpdate]

        """
        _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:
                _data = typing.cast(
                    WebhookSerializerForUpdate,
                    construct_type(
                        type_=WebhookSerializerForUpdate,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 AsyncRawWebhooksClient:
    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
    ) -> AsyncHttpResponse[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
        -------
        AsyncHttpResponse[typing.List[Webhook]]

        """
        _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:
                _data = typing.cast(
                    typing.List[Webhook],
                    construct_type(
                        type_=typing.List[Webhook],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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 create(
        self,
        *,
        url: str,
        actions: typing.Optional[typing.Sequence[ActionsEnum]] = OMIT,
        headers: 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,
    ) -> AsyncHttpResponse[Webhook]:
        """
        Create a webhook for your organization.

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

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

        headers : 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
        -------
        AsyncHttpResponse[Webhook]

        """
        _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:
                _data = typing.cast(
                    Webhook,
                    construct_type(
                        type_=Webhook,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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 info(
        self,
        *,
        organization_only: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[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
        -------
        AsyncHttpResponse[InfoWebhooksResponse]
            Object with webhook action descriptions.
        """
        _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:
                _data = typing.cast(
                    InfoWebhooksResponse,
                    construct_type(
                        type_=InfoWebhooksResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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 get(
        self, id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[Webhook]:
        """
        Parameters
        ----------
        id : int

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

        Returns
        -------
        AsyncHttpResponse[Webhook]

        """
        _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:
                _data = typing.cast(
                    Webhook,
                    construct_type(
                        type_=Webhook,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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 delete(
        self, id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[None]:
        """
        Parameters
        ----------
        id : int

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _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 AsyncHttpResponse(response=_response, data=None)
            _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 update(
        self,
        id: int,
        *,
        actions: typing.Optional[typing.Sequence[ActionsEnum]] = OMIT,
        headers: 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,
    ) -> AsyncHttpResponse[WebhookSerializerForUpdate]:
        """
        Parameters
        ----------
        id : int

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

        headers : 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
        -------
        AsyncHttpResponse[WebhookSerializerForUpdate]

        """
        _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:
                _data = typing.cast(
                    WebhookSerializerForUpdate,
                    construct_type(
                        type_=WebhookSerializerForUpdate,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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)
