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

import typing
from ...core.client_wrapper import SyncClientWrapper
from ...requests.device_code import DeviceCodeParams
from ...core.request_options import RequestOptions
from ...core.http_response import HttpResponse
from ...types.create_device_code_response import CreateDeviceCodeResponse
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_device_code_response import GetDeviceCodeResponse
from ...core.jsonable_encoder import jsonable_encoder
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 RawCodesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def create(
        self,
        *,
        idempotency_key: str,
        device_code: DeviceCodeParams,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateDeviceCodeResponse]:
        """
        Creates a DeviceCode that can be used to login to a Square Terminal device to enter the connected
        terminal mode.

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

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

        device_code : DeviceCodeParams
            The device code to create.

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

        Returns
        -------
        HttpResponse[CreateDeviceCodeResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/devices/codes",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "device_code": convert_and_respect_annotation_metadata(
                    object_=device_code, annotation=DeviceCodeParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateDeviceCodeResponse,
                    construct_type(
                        type_=CreateDeviceCodeResponse,  # 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, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetDeviceCodeResponse]:
        """
        Retrieves DeviceCode with the associated ID.

        Parameters
        ----------
        id : str
            The unique identifier for the device code.

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

        Returns
        -------
        HttpResponse[GetDeviceCodeResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/devices/codes/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetDeviceCodeResponse,
                    construct_type(
                        type_=GetDeviceCodeResponse,  # 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 AsyncRawCodesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create(
        self,
        *,
        idempotency_key: str,
        device_code: DeviceCodeParams,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateDeviceCodeResponse]:
        """
        Creates a DeviceCode that can be used to login to a Square Terminal device to enter the connected
        terminal mode.

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

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

        device_code : DeviceCodeParams
            The device code to create.

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

        Returns
        -------
        AsyncHttpResponse[CreateDeviceCodeResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/devices/codes",
            method="POST",
            json={
                "idempotency_key": idempotency_key,
                "device_code": convert_and_respect_annotation_metadata(
                    object_=device_code, annotation=DeviceCodeParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateDeviceCodeResponse,
                    construct_type(
                        type_=CreateDeviceCodeResponse,  # 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, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetDeviceCodeResponse]:
        """
        Retrieves DeviceCode with the associated ID.

        Parameters
        ----------
        id : str
            The unique identifier for the device code.

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

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