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

import typing
from ...core.client_wrapper import SyncClientWrapper
from ...requests.terminal_action import TerminalActionParams
from ...core.request_options import RequestOptions
from ...core.http_response import HttpResponse
from ...types.create_terminal_action_response import CreateTerminalActionResponse
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 ...requests.terminal_action_query import TerminalActionQueryParams
from ...types.search_terminal_actions_response import SearchTerminalActionsResponse
from ...types.get_terminal_action_response import GetTerminalActionResponse
from ...core.jsonable_encoder import jsonable_encoder
from ...types.cancel_terminal_action_response import CancelTerminalActionResponse
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 RawActionsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def create(
        self,
        *,
        idempotency_key: str,
        action: TerminalActionParams,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateTerminalActionResponse]:
        """
        Creates a Terminal action request and sends it to the specified device.

        Parameters
        ----------
        idempotency_key : str
            A unique string that identifies this `CreateAction` request. Keys can be any valid string
            but must be unique for every `CreateAction` request.

            See [Idempotency keys](https://developer.squareup.com/docs/build-basics/common-api-patterns/idempotency) for more
            information.

        action : TerminalActionParams
            The Action to create.

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

        Returns
        -------
        HttpResponse[CreateTerminalActionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/terminals/actions",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "action": convert_and_respect_annotation_metadata(
                    object_=action, annotation=TerminalActionParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateTerminalActionResponse,
                    construct_type(
                        type_=CreateTerminalActionResponse,  # 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 search(
        self,
        *,
        query: typing.Optional[TerminalActionQueryParams] = OMIT,
        cursor: typing.Optional[str] = OMIT,
        limit: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[SearchTerminalActionsResponse]:
        """
        Retrieves a filtered list of Terminal action requests created by the account making the request. Terminal action requests are available for 30 days.

        Parameters
        ----------
        query : typing.Optional[TerminalActionQueryParams]
            Queries terminal actions based on given conditions and sort order.
            Leaving this unset will return all actions with the default sort order.

        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint.
            Provide this to retrieve the next set of results for the original query.
            See [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination) for more
            information.

        limit : typing.Optional[int]
            Limit the number of results returned for a single request.

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

        Returns
        -------
        HttpResponse[SearchTerminalActionsResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/terminals/actions/search",
            method="POST",
            json={
                "query": convert_and_respect_annotation_metadata(
                    object_=query, annotation=TerminalActionQueryParams, direction="write"
                ),
                "cursor": cursor,
                "limit": limit,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SearchTerminalActionsResponse,
                    construct_type(
                        type_=SearchTerminalActionsResponse,  # 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, action_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetTerminalActionResponse]:
        """
        Retrieves a Terminal action request by `action_id`. Terminal action requests are available for 30 days.

        Parameters
        ----------
        action_id : str
            Unique ID for the desired `TerminalAction`.

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

        Returns
        -------
        HttpResponse[GetTerminalActionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/terminals/actions/{jsonable_encoder(action_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetTerminalActionResponse,
                    construct_type(
                        type_=GetTerminalActionResponse,  # 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 cancel(
        self, action_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[CancelTerminalActionResponse]:
        """
        Cancels a Terminal action request if the status of the request permits it.

        Parameters
        ----------
        action_id : str
            Unique ID for the desired `TerminalAction`.

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

        Returns
        -------
        HttpResponse[CancelTerminalActionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/terminals/actions/{jsonable_encoder(action_id)}/cancel",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CancelTerminalActionResponse,
                    construct_type(
                        type_=CancelTerminalActionResponse,  # 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 AsyncRawActionsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create(
        self,
        *,
        idempotency_key: str,
        action: TerminalActionParams,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateTerminalActionResponse]:
        """
        Creates a Terminal action request and sends it to the specified device.

        Parameters
        ----------
        idempotency_key : str
            A unique string that identifies this `CreateAction` request. Keys can be any valid string
            but must be unique for every `CreateAction` request.

            See [Idempotency keys](https://developer.squareup.com/docs/build-basics/common-api-patterns/idempotency) for more
            information.

        action : TerminalActionParams
            The Action to create.

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

        Returns
        -------
        AsyncHttpResponse[CreateTerminalActionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/terminals/actions",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "action": convert_and_respect_annotation_metadata(
                    object_=action, annotation=TerminalActionParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateTerminalActionResponse,
                    construct_type(
                        type_=CreateTerminalActionResponse,  # 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 search(
        self,
        *,
        query: typing.Optional[TerminalActionQueryParams] = OMIT,
        cursor: typing.Optional[str] = OMIT,
        limit: typing.Optional[int] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[SearchTerminalActionsResponse]:
        """
        Retrieves a filtered list of Terminal action requests created by the account making the request. Terminal action requests are available for 30 days.

        Parameters
        ----------
        query : typing.Optional[TerminalActionQueryParams]
            Queries terminal actions based on given conditions and sort order.
            Leaving this unset will return all actions with the default sort order.

        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint.
            Provide this to retrieve the next set of results for the original query.
            See [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination) for more
            information.

        limit : typing.Optional[int]
            Limit the number of results returned for a single request.

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

        Returns
        -------
        AsyncHttpResponse[SearchTerminalActionsResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/terminals/actions/search",
            method="POST",
            json={
                "query": convert_and_respect_annotation_metadata(
                    object_=query, annotation=TerminalActionQueryParams, direction="write"
                ),
                "cursor": cursor,
                "limit": limit,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SearchTerminalActionsResponse,
                    construct_type(
                        type_=SearchTerminalActionsResponse,  # 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, action_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetTerminalActionResponse]:
        """
        Retrieves a Terminal action request by `action_id`. Terminal action requests are available for 30 days.

        Parameters
        ----------
        action_id : str
            Unique ID for the desired `TerminalAction`.

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

        Returns
        -------
        AsyncHttpResponse[GetTerminalActionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/terminals/actions/{jsonable_encoder(action_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetTerminalActionResponse,
                    construct_type(
                        type_=GetTerminalActionResponse,  # 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 cancel(
        self, action_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[CancelTerminalActionResponse]:
        """
        Cancels a Terminal action request if the status of the request permits it.

        Parameters
        ----------
        action_id : str
            Unique ID for the desired `TerminalAction`.

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

        Returns
        -------
        AsyncHttpResponse[CancelTerminalActionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/terminals/actions/{jsonable_encoder(action_id)}/cancel",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CancelTerminalActionResponse,
                    construct_type(
                        type_=CancelTerminalActionResponse,  # 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)
