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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..core.request_options import RequestOptions
from ..core.http_response import HttpResponse
from ..types.get_snippet_response import GetSnippetResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..requests.snippet import SnippetParams
from ..types.upsert_snippet_response import UpsertSnippetResponse
from ..core.serialization import convert_and_respect_annotation_metadata
from ..types.delete_snippet_response import DeleteSnippetResponse
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 RawSnippetsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def get(
        self, site_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetSnippetResponse]:
        """
        Retrieves your snippet from a Square Online site. A site can contain snippets from multiple snippet applications, but you can retrieve only the snippet that was added by your application.

        You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.


        __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).

        Parameters
        ----------
        site_id : str
            The ID of the site that contains the snippet.

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

        Returns
        -------
        HttpResponse[GetSnippetResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/sites/{jsonable_encoder(site_id)}/snippet",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetSnippetResponse,
                    construct_type(
                        type_=GetSnippetResponse,  # 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 upsert(
        self, site_id: str, *, snippet: SnippetParams, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[UpsertSnippetResponse]:
        """
        Adds a snippet to a Square Online site or updates the existing snippet on the site.
        The snippet code is appended to the end of the `head` element on every page of the site, except checkout pages. A snippet application can add one snippet to a given site.

        You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.


        __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).

        Parameters
        ----------
        site_id : str
            The ID of the site where you want to add or update the snippet.

        snippet : SnippetParams
            The snippet for the site.

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

        Returns
        -------
        HttpResponse[UpsertSnippetResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/sites/{jsonable_encoder(site_id)}/snippet",
            method="POST",
            json={
                "snippet": convert_and_respect_annotation_metadata(
                    object_=snippet, annotation=SnippetParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpsertSnippetResponse,
                    construct_type(
                        type_=UpsertSnippetResponse,  # 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, site_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DeleteSnippetResponse]:
        """
        Removes your snippet from a Square Online site.

        You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.


        __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).

        Parameters
        ----------
        site_id : str
            The ID of the site that contains the snippet.

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

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

    async def get(
        self, site_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetSnippetResponse]:
        """
        Retrieves your snippet from a Square Online site. A site can contain snippets from multiple snippet applications, but you can retrieve only the snippet that was added by your application.

        You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.


        __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).

        Parameters
        ----------
        site_id : str
            The ID of the site that contains the snippet.

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

        Returns
        -------
        AsyncHttpResponse[GetSnippetResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/sites/{jsonable_encoder(site_id)}/snippet",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetSnippetResponse,
                    construct_type(
                        type_=GetSnippetResponse,  # 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 upsert(
        self, site_id: str, *, snippet: SnippetParams, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[UpsertSnippetResponse]:
        """
        Adds a snippet to a Square Online site or updates the existing snippet on the site.
        The snippet code is appended to the end of the `head` element on every page of the site, except checkout pages. A snippet application can add one snippet to a given site.

        You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.


        __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).

        Parameters
        ----------
        site_id : str
            The ID of the site where you want to add or update the snippet.

        snippet : SnippetParams
            The snippet for the site.

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

        Returns
        -------
        AsyncHttpResponse[UpsertSnippetResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/sites/{jsonable_encoder(site_id)}/snippet",
            method="POST",
            json={
                "snippet": convert_and_respect_annotation_metadata(
                    object_=snippet, annotation=SnippetParams, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpsertSnippetResponse,
                    construct_type(
                        type_=UpsertSnippetResponse,  # 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, site_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DeleteSnippetResponse]:
        """
        Removes your snippet from a Square Online site.

        You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.


        __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).

        Parameters
        ----------
        site_id : str
            The ID of the site that contains the snippet.

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

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