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

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..requests.webhook_advanced_options import WebhookAdvancedOptionsParams
from ..types.api_version_enum import ApiVersionEnum
from ..types.max_page_size import MaxPageSize
from ..types.next_page_token import NextPageToken
from ..types.sort_dir import SortDir
from ..types.webhook_endpoint import WebhookEndpoint
from ..types.webhook_endpoint_create import WebhookEndpointCreate
from ..types.webhook_endpoint_event_type import WebhookEndpointEventType
from ..types.webhook_endpoint_status import WebhookEndpointStatus
from .raw_client import AsyncRawWebhookEndpointsClient, RawWebhookEndpointsClient
from .types.webhook_endpoints_delete_response import WebhookEndpointsDeleteResponse
from .types.webhook_endpoints_list_response import WebhookEndpointsListResponse

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


class WebhookEndpointsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawWebhookEndpointsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawWebhookEndpointsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawWebhookEndpointsClient
        """
        return self._raw_client

    def list(
        self,
        *,
        status: typing.Optional[WebhookEndpointStatus] = None,
        sort_dir: typing.Optional[SortDir] = None,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookEndpointsListResponse:
        """
        List all webhook endpoints.

        Parameters
        ----------
        status : typing.Optional[WebhookEndpointStatus]
            Filter by endpoint status.

        sort_dir : typing.Optional[SortDir]

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

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

        Returns
        -------
        WebhookEndpointsListResponse
            List of webhook endpoints

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_endpoints.list(
            next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
        )
        """
        _response = self._raw_client.list(
            status=status,
            sort_dir=sort_dir,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            request_options=request_options,
        )
        return _response.data

    def create(
        self,
        *,
        url: str,
        name: str,
        enabled_events: typing.Sequence[WebhookEndpointEventType],
        api_version: ApiVersionEnum,
        status: typing.Optional[WebhookEndpointStatus] = OMIT,
        advanced_options: typing.Optional[WebhookAdvancedOptionsParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookEndpointCreate:
        """
        Create a new webhook endpoint. The response includes a `signingSecret` that is only returned once — store it securely for verifying webhook signatures.

        The `enabledEvents` array specifies which global event types this endpoint should receive. Use the [Webhook Events](https://docs.extend.ai/2026-02-09/developers/api-reference/webhook-events) reference to see available event types.

        To subscribe to events scoped to a specific resource (e.g., a single extractor or workflow), use [Create Webhook Subscription](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/webhook/create-webhook-subscription) after creating the endpoint.

        Parameters
        ----------
        url : str
            The URL that webhook events will be sent to.

        name : str
            A human-readable name for the webhook endpoint.

        enabled_events : typing.Sequence[WebhookEndpointEventType]
            The list of global event types to subscribe to. Pass an empty array to create an endpoint with no global events (useful if you only plan to use resource-scoped subscriptions).

        api_version : ApiVersionEnum

        status : typing.Optional[WebhookEndpointStatus]

        advanced_options : typing.Optional[WebhookAdvancedOptionsParams]

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

        Returns
        -------
        WebhookEndpointCreate
            Successfully created webhook endpoint

        Examples
        --------
        from extend_ai import ApiVersionEnum, Extend, WebhookEndpointEventType

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_endpoints.create(
            url="https://example.com/webhooks",
            name="Production webhook",
            enabled_events=[
                WebhookEndpointEventType.EXTRACT_RUN_PROCESSED,
                WebhookEndpointEventType.WORKFLOW_CREATED,
            ],
            api_version=ApiVersionEnum.TWO_THOUSAND_TWENTY_SIX0209,
        )
        """
        _response = self._raw_client.create(
            url=url,
            name=name,
            enabled_events=enabled_events,
            api_version=api_version,
            status=status,
            advanced_options=advanced_options,
            request_options=request_options,
        )
        return _response.data

    def retrieve(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> WebhookEndpoint:
        """
        Retrieve a webhook endpoint by ID.

        Parameters
        ----------
        id : str
            The ID of the webhook endpoint.

            Example: `"wh_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        WebhookEndpoint
            Retrieved webhook endpoint

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_endpoints.retrieve(
            id="webhook_endpoint_id_here",
        )
        """
        _response = self._raw_client.retrieve(id, request_options=request_options)
        return _response.data

    def update(
        self,
        id: str,
        *,
        url: typing.Optional[str] = OMIT,
        name: typing.Optional[str] = OMIT,
        status: typing.Optional[WebhookEndpointStatus] = OMIT,
        enabled_events: typing.Optional[typing.Sequence[WebhookEndpointEventType]] = OMIT,
        advanced_options: typing.Optional[WebhookAdvancedOptionsParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookEndpoint:
        """
        Update a webhook endpoint. Only the fields you include in the request body will be updated; omitted fields remain unchanged.

        The `apiVersion` of a webhook endpoint cannot be changed after creation.

        Parameters
        ----------
        id : str
            The ID of the webhook endpoint to update.

            Example: `"wh_Xj8mK2pL9nR4vT7qY5wZ"`

        url : typing.Optional[str]
            The URL that webhook events will be sent to.

        name : typing.Optional[str]
            A human-readable name for the webhook endpoint.

        status : typing.Optional[WebhookEndpointStatus]

        enabled_events : typing.Optional[typing.Sequence[WebhookEndpointEventType]]
            The list of global event types to subscribe to.

        advanced_options : typing.Optional[WebhookAdvancedOptionsParams]

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

        Returns
        -------
        WebhookEndpoint
            Successfully updated webhook endpoint

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_endpoints.update(
            id="webhook_endpoint_id_here",
        )
        """
        _response = self._raw_client.update(
            id,
            url=url,
            name=name,
            status=status,
            enabled_events=enabled_events,
            advanced_options=advanced_options,
            request_options=request_options,
        )
        return _response.data

    def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WebhookEndpointsDeleteResponse:
        """
        Delete a webhook endpoint and all of its subscriptions. This operation is permanent and cannot be undone.

        Parameters
        ----------
        id : str
            The ID of the webhook endpoint to delete.

            Example: `"wh_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        WebhookEndpointsDeleteResponse
            Successfully deleted webhook endpoint

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.webhook_endpoints.delete(
            id="webhook_endpoint_id_here",
        )
        """
        _response = self._raw_client.delete(id, request_options=request_options)
        return _response.data


class AsyncWebhookEndpointsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawWebhookEndpointsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawWebhookEndpointsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawWebhookEndpointsClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        status: typing.Optional[WebhookEndpointStatus] = None,
        sort_dir: typing.Optional[SortDir] = None,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookEndpointsListResponse:
        """
        List all webhook endpoints.

        Parameters
        ----------
        status : typing.Optional[WebhookEndpointStatus]
            Filter by endpoint status.

        sort_dir : typing.Optional[SortDir]

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

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

        Returns
        -------
        WebhookEndpointsListResponse
            List of webhook endpoints

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_endpoints.list(
                next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            status=status,
            sort_dir=sort_dir,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            request_options=request_options,
        )
        return _response.data

    async def create(
        self,
        *,
        url: str,
        name: str,
        enabled_events: typing.Sequence[WebhookEndpointEventType],
        api_version: ApiVersionEnum,
        status: typing.Optional[WebhookEndpointStatus] = OMIT,
        advanced_options: typing.Optional[WebhookAdvancedOptionsParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookEndpointCreate:
        """
        Create a new webhook endpoint. The response includes a `signingSecret` that is only returned once — store it securely for verifying webhook signatures.

        The `enabledEvents` array specifies which global event types this endpoint should receive. Use the [Webhook Events](https://docs.extend.ai/2026-02-09/developers/api-reference/webhook-events) reference to see available event types.

        To subscribe to events scoped to a specific resource (e.g., a single extractor or workflow), use [Create Webhook Subscription](https://docs.extend.ai/2026-02-09/developers/api-reference/endpoints/webhook/create-webhook-subscription) after creating the endpoint.

        Parameters
        ----------
        url : str
            The URL that webhook events will be sent to.

        name : str
            A human-readable name for the webhook endpoint.

        enabled_events : typing.Sequence[WebhookEndpointEventType]
            The list of global event types to subscribe to. Pass an empty array to create an endpoint with no global events (useful if you only plan to use resource-scoped subscriptions).

        api_version : ApiVersionEnum

        status : typing.Optional[WebhookEndpointStatus]

        advanced_options : typing.Optional[WebhookAdvancedOptionsParams]

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

        Returns
        -------
        WebhookEndpointCreate
            Successfully created webhook endpoint

        Examples
        --------
        import asyncio

        from extend_ai import ApiVersionEnum, AsyncExtend, WebhookEndpointEventType

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_endpoints.create(
                url="https://example.com/webhooks",
                name="Production webhook",
                enabled_events=[
                    WebhookEndpointEventType.EXTRACT_RUN_PROCESSED,
                    WebhookEndpointEventType.WORKFLOW_CREATED,
                ],
                api_version=ApiVersionEnum.TWO_THOUSAND_TWENTY_SIX0209,
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create(
            url=url,
            name=name,
            enabled_events=enabled_events,
            api_version=api_version,
            status=status,
            advanced_options=advanced_options,
            request_options=request_options,
        )
        return _response.data

    async def retrieve(self, id: str, *, request_options: typing.Optional[RequestOptions] = None) -> WebhookEndpoint:
        """
        Retrieve a webhook endpoint by ID.

        Parameters
        ----------
        id : str
            The ID of the webhook endpoint.

            Example: `"wh_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        WebhookEndpoint
            Retrieved webhook endpoint

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_endpoints.retrieve(
                id="webhook_endpoint_id_here",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.retrieve(id, request_options=request_options)
        return _response.data

    async def update(
        self,
        id: str,
        *,
        url: typing.Optional[str] = OMIT,
        name: typing.Optional[str] = OMIT,
        status: typing.Optional[WebhookEndpointStatus] = OMIT,
        enabled_events: typing.Optional[typing.Sequence[WebhookEndpointEventType]] = OMIT,
        advanced_options: typing.Optional[WebhookAdvancedOptionsParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WebhookEndpoint:
        """
        Update a webhook endpoint. Only the fields you include in the request body will be updated; omitted fields remain unchanged.

        The `apiVersion` of a webhook endpoint cannot be changed after creation.

        Parameters
        ----------
        id : str
            The ID of the webhook endpoint to update.

            Example: `"wh_Xj8mK2pL9nR4vT7qY5wZ"`

        url : typing.Optional[str]
            The URL that webhook events will be sent to.

        name : typing.Optional[str]
            A human-readable name for the webhook endpoint.

        status : typing.Optional[WebhookEndpointStatus]

        enabled_events : typing.Optional[typing.Sequence[WebhookEndpointEventType]]
            The list of global event types to subscribe to.

        advanced_options : typing.Optional[WebhookAdvancedOptionsParams]

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

        Returns
        -------
        WebhookEndpoint
            Successfully updated webhook endpoint

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_endpoints.update(
                id="webhook_endpoint_id_here",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update(
            id,
            url=url,
            name=name,
            status=status,
            enabled_events=enabled_events,
            advanced_options=advanced_options,
            request_options=request_options,
        )
        return _response.data

    async def delete(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WebhookEndpointsDeleteResponse:
        """
        Delete a webhook endpoint and all of its subscriptions. This operation is permanent and cannot be undone.

        Parameters
        ----------
        id : str
            The ID of the webhook endpoint to delete.

            Example: `"wh_Xj8mK2pL9nR4vT7qY5wZ"`

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

        Returns
        -------
        WebhookEndpointsDeleteResponse
            Successfully deleted webhook endpoint

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.webhook_endpoints.delete(
                id="webhook_endpoint_id_here",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.delete(id, request_options=request_options)
        return _response.data
