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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..requests.search_events_query import SearchEventsQueryParams
from ..core.request_options import RequestOptions
from ..core.http_response import HttpResponse
from ..types.search_events_response import SearchEventsResponse
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.disable_events_response import DisableEventsResponse
from ..types.enable_events_response import EnableEventsResponse
from ..types.list_event_types_response import ListEventTypesResponse
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 RawEventsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def search_events(
        self,
        *,
        cursor: typing.Optional[str] = OMIT,
        limit: typing.Optional[int] = OMIT,
        query: typing.Optional[SearchEventsQueryParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[SearchEventsResponse]:
        """
        Search for Square API events that occur within a 28-day timeframe.

        Parameters
        ----------
        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint. Provide this cursor to retrieve the next set of events for your original query.

            For more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

        limit : typing.Optional[int]
            The maximum number of events to return in a single page. The response might contain fewer events. The default value is 100, which is also the maximum allowed value.

            For more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

            Default: 100

        query : typing.Optional[SearchEventsQueryParams]
            The filtering and sorting criteria for the search request. To retrieve additional pages using a cursor, you must use the original query.

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

        Returns
        -------
        HttpResponse[SearchEventsResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/events",
            method="POST",
            json={
                "cursor": cursor,
                "limit": limit,
                "query": convert_and_respect_annotation_metadata(
                    object_=query, annotation=SearchEventsQueryParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SearchEventsResponse,
                    construct_type(
                        type_=SearchEventsResponse,  # 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 disable_events(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DisableEventsResponse]:
        """
        Disables events to prevent them from being searchable.
        All events are disabled by default. You must enable events to make them searchable.
        Disabling events for a specific time period prevents them from being searchable, even if you re-enable them later.

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

        Returns
        -------
        HttpResponse[DisableEventsResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/events/disable",
            method="PUT",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DisableEventsResponse,
                    construct_type(
                        type_=DisableEventsResponse,  # 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 enable_events(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[EnableEventsResponse]:
        """
        Enables events to make them searchable. Only events that occur while in the enabled state are searchable.

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

        Returns
        -------
        HttpResponse[EnableEventsResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/events/enable",
            method="PUT",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    EnableEventsResponse,
                    construct_type(
                        type_=EnableEventsResponse,  # 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 list_event_types(
        self, *, api_version: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ListEventTypesResponse]:
        """
        Lists all event types that you can subscribe to as webhooks or query using the Events API.

        Parameters
        ----------
        api_version : typing.Optional[str]
            The API version for which to list event types. Setting this field overrides the default version used by the application.

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

        Returns
        -------
        HttpResponse[ListEventTypesResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/events/types",
            method="GET",
            params={
                "api_version": api_version,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListEventTypesResponse,
                    construct_type(
                        type_=ListEventTypesResponse,  # 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 AsyncRawEventsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def search_events(
        self,
        *,
        cursor: typing.Optional[str] = OMIT,
        limit: typing.Optional[int] = OMIT,
        query: typing.Optional[SearchEventsQueryParams] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[SearchEventsResponse]:
        """
        Search for Square API events that occur within a 28-day timeframe.

        Parameters
        ----------
        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint. Provide this cursor to retrieve the next set of events for your original query.

            For more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

        limit : typing.Optional[int]
            The maximum number of events to return in a single page. The response might contain fewer events. The default value is 100, which is also the maximum allowed value.

            For more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

            Default: 100

        query : typing.Optional[SearchEventsQueryParams]
            The filtering and sorting criteria for the search request. To retrieve additional pages using a cursor, you must use the original query.

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

        Returns
        -------
        AsyncHttpResponse[SearchEventsResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/events",
            method="POST",
            json={
                "cursor": cursor,
                "limit": limit,
                "query": convert_and_respect_annotation_metadata(
                    object_=query, annotation=SearchEventsQueryParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SearchEventsResponse,
                    construct_type(
                        type_=SearchEventsResponse,  # 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 disable_events(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DisableEventsResponse]:
        """
        Disables events to prevent them from being searchable.
        All events are disabled by default. You must enable events to make them searchable.
        Disabling events for a specific time period prevents them from being searchable, even if you re-enable them later.

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

        Returns
        -------
        AsyncHttpResponse[DisableEventsResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/events/disable",
            method="PUT",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DisableEventsResponse,
                    construct_type(
                        type_=DisableEventsResponse,  # 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 enable_events(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[EnableEventsResponse]:
        """
        Enables events to make them searchable. Only events that occur while in the enabled state are searchable.

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

        Returns
        -------
        AsyncHttpResponse[EnableEventsResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/events/enable",
            method="PUT",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    EnableEventsResponse,
                    construct_type(
                        type_=EnableEventsResponse,  # 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 list_event_types(
        self, *, api_version: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ListEventTypesResponse]:
        """
        Lists all event types that you can subscribe to as webhooks or query using the Events API.

        Parameters
        ----------
        api_version : typing.Optional[str]
            The API version for which to list event types. Setting this field overrides the default version used by the application.

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

        Returns
        -------
        AsyncHttpResponse[ListEventTypesResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/events/types",
            method="GET",
            params={
                "api_version": api_version,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListEventTypesResponse,
                    construct_type(
                        type_=ListEventTypesResponse,  # 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)
