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

import typing
from ...core.client_wrapper import SyncClientWrapper
from ...requests.webhook_subscription import WebhookSubscriptionParams
from ...core.request_options import RequestOptions
from ...core.http_response import HttpResponse
from ...types.create_webhook_subscription_response import CreateWebhookSubscriptionResponse
from ...core.serialization import convert_and_respect_annotation_metadata
from ...core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ...core.api_error import ApiError
from ...types.get_webhook_subscription_response import GetWebhookSubscriptionResponse
from ...core.jsonable_encoder import jsonable_encoder
from ...types.update_webhook_subscription_response import UpdateWebhookSubscriptionResponse
from ...types.delete_webhook_subscription_response import DeleteWebhookSubscriptionResponse
from ...types.update_webhook_subscription_signature_key_response import UpdateWebhookSubscriptionSignatureKeyResponse
from ...types.test_webhook_subscription_response import TestWebhookSubscriptionResponse
from ...core.client_wrapper import AsyncClientWrapper
from ...core.http_response import AsyncHttpResponse

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


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

    def create(
        self,
        *,
        subscription: WebhookSubscriptionParams,
        idempotency_key: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateWebhookSubscriptionResponse]:
        """
        Creates a webhook subscription.

        Parameters
        ----------
        subscription : WebhookSubscriptionParams
            The [Subscription](entity:WebhookSubscription) to create.

        idempotency_key : typing.Optional[str]
            A unique string that identifies the [CreateWebhookSubscription](api-endpoint:WebhookSubscriptions-CreateWebhookSubscription) request.

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

        Returns
        -------
        HttpResponse[CreateWebhookSubscriptionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/webhooks/subscriptions",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "subscription": convert_and_respect_annotation_metadata(
                    object_=subscription, annotation=WebhookSubscriptionParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateWebhookSubscriptionResponse,
                    construct_type(
                        type_=CreateWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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, subscription_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetWebhookSubscriptionResponse]:
        """
        Retrieves a webhook subscription identified by its ID.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to retrieve.

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

        Returns
        -------
        HttpResponse[GetWebhookSubscriptionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetWebhookSubscriptionResponse,
                    construct_type(
                        type_=GetWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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,
        subscription_id: str,
        *,
        subscription: typing.Optional[WebhookSubscriptionParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UpdateWebhookSubscriptionResponse]:
        """
        Updates a webhook subscription.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to update.

        subscription : typing.Optional[WebhookSubscriptionParams]
            The [Subscription](entity:WebhookSubscription) to update.

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

        Returns
        -------
        HttpResponse[UpdateWebhookSubscriptionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}",
            method="PUT",
            json={
                "subscription": convert_and_respect_annotation_metadata(
                    object_=subscription, annotation=WebhookSubscriptionParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateWebhookSubscriptionResponse,
                    construct_type(
                        type_=UpdateWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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, subscription_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DeleteWebhookSubscriptionResponse]:
        """
        Deletes a webhook subscription.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to delete.

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

        Returns
        -------
        HttpResponse[DeleteWebhookSubscriptionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteWebhookSubscriptionResponse,
                    construct_type(
                        type_=DeleteWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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_signature_key(
        self,
        subscription_id: str,
        *,
        idempotency_key: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UpdateWebhookSubscriptionSignatureKeyResponse]:
        """
        Updates a webhook subscription by replacing the existing signature key with a new one.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to update.

        idempotency_key : typing.Optional[str]
            A unique string that identifies the [UpdateWebhookSubscriptionSignatureKey](api-endpoint:WebhookSubscriptions-UpdateWebhookSubscriptionSignatureKey) request.

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

        Returns
        -------
        HttpResponse[UpdateWebhookSubscriptionSignatureKeyResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}/signature-key",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateWebhookSubscriptionSignatureKeyResponse,
                    construct_type(
                        type_=UpdateWebhookSubscriptionSignatureKeyResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 test(
        self,
        subscription_id: str,
        *,
        event_type: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[TestWebhookSubscriptionResponse]:
        """
        Tests a webhook subscription by sending a test event to the notification URL.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to test.

        event_type : typing.Optional[str]
            The event type that will be used to test the [Subscription](entity:WebhookSubscription). The event type must be
            contained in the list of event types in the [Subscription](entity:WebhookSubscription).

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

        Returns
        -------
        HttpResponse[TestWebhookSubscriptionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}/test",
            method="POST",
            json={
                "event_type": event_type,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    TestWebhookSubscriptionResponse,
                    construct_type(
                        type_=TestWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 AsyncRawSubscriptionsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create(
        self,
        *,
        subscription: WebhookSubscriptionParams,
        idempotency_key: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateWebhookSubscriptionResponse]:
        """
        Creates a webhook subscription.

        Parameters
        ----------
        subscription : WebhookSubscriptionParams
            The [Subscription](entity:WebhookSubscription) to create.

        idempotency_key : typing.Optional[str]
            A unique string that identifies the [CreateWebhookSubscription](api-endpoint:WebhookSubscriptions-CreateWebhookSubscription) request.

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

        Returns
        -------
        AsyncHttpResponse[CreateWebhookSubscriptionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/webhooks/subscriptions",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "subscription": convert_and_respect_annotation_metadata(
                    object_=subscription, annotation=WebhookSubscriptionParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateWebhookSubscriptionResponse,
                    construct_type(
                        type_=CreateWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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, subscription_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetWebhookSubscriptionResponse]:
        """
        Retrieves a webhook subscription identified by its ID.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to retrieve.

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

        Returns
        -------
        AsyncHttpResponse[GetWebhookSubscriptionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetWebhookSubscriptionResponse,
                    construct_type(
                        type_=GetWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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,
        subscription_id: str,
        *,
        subscription: typing.Optional[WebhookSubscriptionParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UpdateWebhookSubscriptionResponse]:
        """
        Updates a webhook subscription.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to update.

        subscription : typing.Optional[WebhookSubscriptionParams]
            The [Subscription](entity:WebhookSubscription) to update.

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

        Returns
        -------
        AsyncHttpResponse[UpdateWebhookSubscriptionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}",
            method="PUT",
            json={
                "subscription": convert_and_respect_annotation_metadata(
                    object_=subscription, annotation=WebhookSubscriptionParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateWebhookSubscriptionResponse,
                    construct_type(
                        type_=UpdateWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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, subscription_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DeleteWebhookSubscriptionResponse]:
        """
        Deletes a webhook subscription.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to delete.

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

        Returns
        -------
        AsyncHttpResponse[DeleteWebhookSubscriptionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteWebhookSubscriptionResponse,
                    construct_type(
                        type_=DeleteWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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_signature_key(
        self,
        subscription_id: str,
        *,
        idempotency_key: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UpdateWebhookSubscriptionSignatureKeyResponse]:
        """
        Updates a webhook subscription by replacing the existing signature key with a new one.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to update.

        idempotency_key : typing.Optional[str]
            A unique string that identifies the [UpdateWebhookSubscriptionSignatureKey](api-endpoint:WebhookSubscriptions-UpdateWebhookSubscriptionSignatureKey) request.

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

        Returns
        -------
        AsyncHttpResponse[UpdateWebhookSubscriptionSignatureKeyResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}/signature-key",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateWebhookSubscriptionSignatureKeyResponse,
                    construct_type(
                        type_=UpdateWebhookSubscriptionSignatureKeyResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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 test(
        self,
        subscription_id: str,
        *,
        event_type: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[TestWebhookSubscriptionResponse]:
        """
        Tests a webhook subscription by sending a test event to the notification URL.

        Parameters
        ----------
        subscription_id : str
            [REQUIRED] The ID of the [Subscription](entity:WebhookSubscription) to test.

        event_type : typing.Optional[str]
            The event type that will be used to test the [Subscription](entity:WebhookSubscription). The event type must be
            contained in the list of event types in the [Subscription](entity:WebhookSubscription).

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

        Returns
        -------
        AsyncHttpResponse[TestWebhookSubscriptionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/webhooks/subscriptions/{jsonable_encoder(subscription_id)}/test",
            method="POST",
            json={
                "event_type": event_type,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    TestWebhookSubscriptionResponse,
                    construct_type(
                        type_=TestWebhookSubscriptionResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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)
