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

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.bad_request_error import BadRequestError
from ..errors.internal_server_error import InternalServerError
from ..errors.service_unavailable_error import ServiceUnavailableError
from ..errors.unauthorized_error import UnauthorizedError
from ..types.created_at import CreatedAt
from ..types.customer_number_nullable import CustomerNumberNullable
from ..types.customer_query_records import CustomerQueryRecords
from ..types.customer_status import CustomerStatus
from ..types.email import Email
from ..types.entrypointfield import Entrypointfield
from ..types.idempotency_key import IdempotencyKey
from ..types.identifierfields import Identifierfields
from ..types.payabli_api_response import PayabliApiResponse
from ..types.payabli_api_response_00_responsedatanonobject import PayabliApiResponse00Responsedatanonobject
from ..types.payabli_api_response_customer_query import PayabliApiResponseCustomerQuery
from ..types.shippingaddress import Shippingaddress
from ..types.shippingaddressadditional import Shippingaddressadditional
from ..types.shippingcity import Shippingcity
from ..types.shippingcountry import Shippingcountry
from ..types.shippingstate import Shippingstate
from ..types.shippingzip import Shippingzip
from ..types.timezone import Timezone

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


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

    def add_customer(
        self,
        entry: Entrypointfield,
        *,
        force_customer_creation: typing.Optional[bool] = None,
        replace_existing: typing.Optional[int] = None,
        idempotency_key: typing.Optional[IdempotencyKey] = None,
        customer_number: typing.Optional[CustomerNumberNullable] = OMIT,
        customer_username: typing.Optional[str] = OMIT,
        customer_psw: typing.Optional[str] = OMIT,
        customer_status: typing.Optional[CustomerStatus] = OMIT,
        company: typing.Optional[str] = OMIT,
        firstname: typing.Optional[str] = OMIT,
        lastname: typing.Optional[str] = OMIT,
        phone: typing.Optional[str] = OMIT,
        email: typing.Optional[Email] = OMIT,
        address: typing.Optional[str] = OMIT,
        address_1: typing.Optional[str] = OMIT,
        city: typing.Optional[str] = OMIT,
        state: typing.Optional[str] = OMIT,
        zip: typing.Optional[str] = OMIT,
        country: typing.Optional[str] = OMIT,
        shipping_address: typing.Optional[Shippingaddress] = OMIT,
        shipping_address_1: typing.Optional[Shippingaddressadditional] = OMIT,
        shipping_city: typing.Optional[Shippingcity] = OMIT,
        shipping_state: typing.Optional[Shippingstate] = OMIT,
        shipping_zip: typing.Optional[Shippingzip] = OMIT,
        shipping_country: typing.Optional[Shippingcountry] = OMIT,
        balance: typing.Optional[float] = OMIT,
        time_zone: typing.Optional[Timezone] = OMIT,
        additional_fields: typing.Optional[typing.Dict[str, typing.Optional[str]]] = OMIT,
        identifier_fields: typing.Optional[Identifierfields] = OMIT,
        created_at: typing.Optional[CreatedAt] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[PayabliApiResponseCustomerQuery]:
        """
        Creates a customer in an entrypoint. An identifier is required to create customer records. Change your identifier settings in Settings > Custom Fields in PartnerHub.
        If you don't include an identifier, the record is rejected.

        Parameters
        ----------
        entry : Entrypointfield

        force_customer_creation : typing.Optional[bool]
            When `true`, the request creates a new customer record, regardless of whether customer identifiers match an existing customer.

        replace_existing : typing.Optional[int]
            Flag indicating to replace existing customer with a new record. Possible values: 0 (don't replace), 1 (replace). Default is `0`.

        idempotency_key : typing.Optional[IdempotencyKey]

        customer_number : typing.Optional[CustomerNumberNullable]

        customer_username : typing.Optional[str]
            Customer username for customer portal

        customer_psw : typing.Optional[str]
            Customer password for customer portal

        customer_status : typing.Optional[CustomerStatus]

        company : typing.Optional[str]
            Company name

        firstname : typing.Optional[str]
            Customer first name

        lastname : typing.Optional[str]
            Customer last name

        phone : typing.Optional[str]
            Customer phone number

        email : typing.Optional[Email]
            Customer email address.

        address : typing.Optional[str]
            Customer address

        address_1 : typing.Optional[str]
            Additional customer address

        city : typing.Optional[str]
            Customer city

        state : typing.Optional[str]
            Customer State

        zip : typing.Optional[str]
            Customer postal code

        country : typing.Optional[str]
            Customer country in ISO-3166-1 alpha 2 format. See https://en.wikipedia.org/wiki/ISO_3166-1 for reference.

        shipping_address : typing.Optional[Shippingaddress]

        shipping_address_1 : typing.Optional[Shippingaddressadditional]

        shipping_city : typing.Optional[Shippingcity]

        shipping_state : typing.Optional[Shippingstate]

        shipping_zip : typing.Optional[Shippingzip]

        shipping_country : typing.Optional[Shippingcountry]

        balance : typing.Optional[float]
            Customer balance.

        time_zone : typing.Optional[Timezone]

        additional_fields : typing.Optional[typing.Dict[str, typing.Optional[str]]]
            Additional Custom fields in format "key":"value".

        identifier_fields : typing.Optional[Identifierfields]

        created_at : typing.Optional[CreatedAt]

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

        Returns
        -------
        HttpResponse[PayabliApiResponseCustomerQuery]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Customer/single/{jsonable_encoder(entry)}",
            method="POST",
            params={
                "forceCustomerCreation": force_customer_creation,
                "replaceExisting": replace_existing,
            },
            json={
                "customerNumber": customer_number,
                "customerUsername": customer_username,
                "customerPsw": customer_psw,
                "customerStatus": customer_status,
                "company": company,
                "firstname": firstname,
                "lastname": lastname,
                "phone": phone,
                "email": email,
                "address": address,
                "address1": address_1,
                "city": city,
                "state": state,
                "zip": zip,
                "country": country,
                "shippingAddress": shipping_address,
                "shippingAddress1": shipping_address_1,
                "shippingCity": shipping_city,
                "shippingState": shipping_state,
                "shippingZip": shipping_zip,
                "shippingCountry": shipping_country,
                "balance": balance,
                "timeZone": time_zone,
                "additionalFields": additional_fields,
                "identifierFields": identifier_fields,
                "createdAt": created_at,
            },
            headers={
                "content-type": "application/json",
                "idempotencyKey": str(idempotency_key) if idempotency_key is not None else None,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponseCustomerQuery,
                    parse_obj_as(
                        type_=PayabliApiResponseCustomerQuery,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def delete_customer(
        self, customer_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[PayabliApiResponse00Responsedatanonobject]:
        """
        Delete a customer record.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

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

        Returns
        -------
        HttpResponse[PayabliApiResponse00Responsedatanonobject]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Customer/{jsonable_encoder(customer_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponse00Responsedatanonobject,
                    parse_obj_as(
                        type_=PayabliApiResponse00Responsedatanonobject,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get_customer(
        self, customer_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[CustomerQueryRecords]:
        """
        Retrieves a customer's record and details.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

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

        Returns
        -------
        HttpResponse[CustomerQueryRecords]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Customer/{jsonable_encoder(customer_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CustomerQueryRecords,
                    parse_obj_as(
                        type_=CustomerQueryRecords,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def link_customer_transaction(
        self, customer_id: int, trans_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[PayabliApiResponse00Responsedatanonobject]:
        """
        Links a customer to a transaction by ID.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

        trans_id : str
            ReferenceId for the transaction (PaymentId).

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

        Returns
        -------
        HttpResponse[PayabliApiResponse00Responsedatanonobject]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Customer/link/{jsonable_encoder(customer_id)}/{jsonable_encoder(trans_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponse00Responsedatanonobject,
                    parse_obj_as(
                        type_=PayabliApiResponse00Responsedatanonobject,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def request_consent(
        self, customer_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[PayabliApiResponse00Responsedatanonobject]:
        """
        Sends the consent opt-in email to the customer email address in the customer record.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

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

        Returns
        -------
        HttpResponse[PayabliApiResponse00Responsedatanonobject]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Customer/{jsonable_encoder(customer_id)}/consent",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponse00Responsedatanonobject,
                    parse_obj_as(
                        type_=PayabliApiResponse00Responsedatanonobject,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def update_customer(
        self,
        customer_id: int,
        *,
        customer_number: typing.Optional[CustomerNumberNullable] = OMIT,
        customer_username: typing.Optional[str] = OMIT,
        customer_psw: typing.Optional[str] = OMIT,
        customer_status: typing.Optional[CustomerStatus] = OMIT,
        company: typing.Optional[str] = OMIT,
        firstname: typing.Optional[str] = OMIT,
        lastname: typing.Optional[str] = OMIT,
        phone: typing.Optional[str] = OMIT,
        email: typing.Optional[Email] = OMIT,
        address: typing.Optional[str] = OMIT,
        address_1: typing.Optional[str] = OMIT,
        city: typing.Optional[str] = OMIT,
        state: typing.Optional[str] = OMIT,
        zip: typing.Optional[str] = OMIT,
        country: typing.Optional[str] = OMIT,
        shipping_address: typing.Optional[Shippingaddress] = OMIT,
        shipping_address_1: typing.Optional[Shippingaddressadditional] = OMIT,
        shipping_city: typing.Optional[Shippingcity] = OMIT,
        shipping_state: typing.Optional[Shippingstate] = OMIT,
        shipping_zip: typing.Optional[Shippingzip] = OMIT,
        shipping_country: typing.Optional[Shippingcountry] = OMIT,
        balance: typing.Optional[float] = OMIT,
        time_zone: typing.Optional[Timezone] = OMIT,
        additional_fields: typing.Optional[typing.Dict[str, typing.Optional[str]]] = OMIT,
        identifier_fields: typing.Optional[Identifierfields] = OMIT,
        created_at: typing.Optional[CreatedAt] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[PayabliApiResponse00Responsedatanonobject]:
        """
        Update a customer record. Include only the fields you want to change.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

        customer_number : typing.Optional[CustomerNumberNullable]

        customer_username : typing.Optional[str]
            Customer username for customer portal

        customer_psw : typing.Optional[str]
            Customer password for customer portal

        customer_status : typing.Optional[CustomerStatus]

        company : typing.Optional[str]
            Company name

        firstname : typing.Optional[str]
            Customer first name

        lastname : typing.Optional[str]
            Customer last name

        phone : typing.Optional[str]
            Customer phone number

        email : typing.Optional[Email]
            Customer email address.

        address : typing.Optional[str]
            Customer address

        address_1 : typing.Optional[str]
            Additional customer address

        city : typing.Optional[str]
            Customer city

        state : typing.Optional[str]
            Customer State

        zip : typing.Optional[str]
            Customer postal code

        country : typing.Optional[str]
            Customer country in ISO-3166-1 alpha 2 format. See https://en.wikipedia.org/wiki/ISO_3166-1 for reference.

        shipping_address : typing.Optional[Shippingaddress]

        shipping_address_1 : typing.Optional[Shippingaddressadditional]

        shipping_city : typing.Optional[Shippingcity]

        shipping_state : typing.Optional[Shippingstate]

        shipping_zip : typing.Optional[Shippingzip]

        shipping_country : typing.Optional[Shippingcountry]

        balance : typing.Optional[float]
            Customer balance.

        time_zone : typing.Optional[Timezone]

        additional_fields : typing.Optional[typing.Dict[str, typing.Optional[str]]]
            Additional Custom fields in format "key":"value".

        identifier_fields : typing.Optional[Identifierfields]

        created_at : typing.Optional[CreatedAt]

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

        Returns
        -------
        HttpResponse[PayabliApiResponse00Responsedatanonobject]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Customer/{jsonable_encoder(customer_id)}",
            method="PUT",
            json={
                "customerNumber": customer_number,
                "customerUsername": customer_username,
                "customerPsw": customer_psw,
                "customerStatus": customer_status,
                "company": company,
                "firstname": firstname,
                "lastname": lastname,
                "phone": phone,
                "email": email,
                "address": address,
                "address1": address_1,
                "city": city,
                "state": state,
                "zip": zip,
                "country": country,
                "shippingAddress": shipping_address,
                "shippingAddress1": shipping_address_1,
                "shippingCity": shipping_city,
                "shippingState": shipping_state,
                "shippingZip": shipping_zip,
                "shippingCountry": shipping_country,
                "balance": balance,
                "timeZone": time_zone,
                "additionalFields": additional_fields,
                "identifierFields": identifier_fields,
                "createdAt": created_at,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponse00Responsedatanonobject,
                    parse_obj_as(
                        type_=PayabliApiResponse00Responsedatanonobject,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawCustomerClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def add_customer(
        self,
        entry: Entrypointfield,
        *,
        force_customer_creation: typing.Optional[bool] = None,
        replace_existing: typing.Optional[int] = None,
        idempotency_key: typing.Optional[IdempotencyKey] = None,
        customer_number: typing.Optional[CustomerNumberNullable] = OMIT,
        customer_username: typing.Optional[str] = OMIT,
        customer_psw: typing.Optional[str] = OMIT,
        customer_status: typing.Optional[CustomerStatus] = OMIT,
        company: typing.Optional[str] = OMIT,
        firstname: typing.Optional[str] = OMIT,
        lastname: typing.Optional[str] = OMIT,
        phone: typing.Optional[str] = OMIT,
        email: typing.Optional[Email] = OMIT,
        address: typing.Optional[str] = OMIT,
        address_1: typing.Optional[str] = OMIT,
        city: typing.Optional[str] = OMIT,
        state: typing.Optional[str] = OMIT,
        zip: typing.Optional[str] = OMIT,
        country: typing.Optional[str] = OMIT,
        shipping_address: typing.Optional[Shippingaddress] = OMIT,
        shipping_address_1: typing.Optional[Shippingaddressadditional] = OMIT,
        shipping_city: typing.Optional[Shippingcity] = OMIT,
        shipping_state: typing.Optional[Shippingstate] = OMIT,
        shipping_zip: typing.Optional[Shippingzip] = OMIT,
        shipping_country: typing.Optional[Shippingcountry] = OMIT,
        balance: typing.Optional[float] = OMIT,
        time_zone: typing.Optional[Timezone] = OMIT,
        additional_fields: typing.Optional[typing.Dict[str, typing.Optional[str]]] = OMIT,
        identifier_fields: typing.Optional[Identifierfields] = OMIT,
        created_at: typing.Optional[CreatedAt] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[PayabliApiResponseCustomerQuery]:
        """
        Creates a customer in an entrypoint. An identifier is required to create customer records. Change your identifier settings in Settings > Custom Fields in PartnerHub.
        If you don't include an identifier, the record is rejected.

        Parameters
        ----------
        entry : Entrypointfield

        force_customer_creation : typing.Optional[bool]
            When `true`, the request creates a new customer record, regardless of whether customer identifiers match an existing customer.

        replace_existing : typing.Optional[int]
            Flag indicating to replace existing customer with a new record. Possible values: 0 (don't replace), 1 (replace). Default is `0`.

        idempotency_key : typing.Optional[IdempotencyKey]

        customer_number : typing.Optional[CustomerNumberNullable]

        customer_username : typing.Optional[str]
            Customer username for customer portal

        customer_psw : typing.Optional[str]
            Customer password for customer portal

        customer_status : typing.Optional[CustomerStatus]

        company : typing.Optional[str]
            Company name

        firstname : typing.Optional[str]
            Customer first name

        lastname : typing.Optional[str]
            Customer last name

        phone : typing.Optional[str]
            Customer phone number

        email : typing.Optional[Email]
            Customer email address.

        address : typing.Optional[str]
            Customer address

        address_1 : typing.Optional[str]
            Additional customer address

        city : typing.Optional[str]
            Customer city

        state : typing.Optional[str]
            Customer State

        zip : typing.Optional[str]
            Customer postal code

        country : typing.Optional[str]
            Customer country in ISO-3166-1 alpha 2 format. See https://en.wikipedia.org/wiki/ISO_3166-1 for reference.

        shipping_address : typing.Optional[Shippingaddress]

        shipping_address_1 : typing.Optional[Shippingaddressadditional]

        shipping_city : typing.Optional[Shippingcity]

        shipping_state : typing.Optional[Shippingstate]

        shipping_zip : typing.Optional[Shippingzip]

        shipping_country : typing.Optional[Shippingcountry]

        balance : typing.Optional[float]
            Customer balance.

        time_zone : typing.Optional[Timezone]

        additional_fields : typing.Optional[typing.Dict[str, typing.Optional[str]]]
            Additional Custom fields in format "key":"value".

        identifier_fields : typing.Optional[Identifierfields]

        created_at : typing.Optional[CreatedAt]

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

        Returns
        -------
        AsyncHttpResponse[PayabliApiResponseCustomerQuery]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Customer/single/{jsonable_encoder(entry)}",
            method="POST",
            params={
                "forceCustomerCreation": force_customer_creation,
                "replaceExisting": replace_existing,
            },
            json={
                "customerNumber": customer_number,
                "customerUsername": customer_username,
                "customerPsw": customer_psw,
                "customerStatus": customer_status,
                "company": company,
                "firstname": firstname,
                "lastname": lastname,
                "phone": phone,
                "email": email,
                "address": address,
                "address1": address_1,
                "city": city,
                "state": state,
                "zip": zip,
                "country": country,
                "shippingAddress": shipping_address,
                "shippingAddress1": shipping_address_1,
                "shippingCity": shipping_city,
                "shippingState": shipping_state,
                "shippingZip": shipping_zip,
                "shippingCountry": shipping_country,
                "balance": balance,
                "timeZone": time_zone,
                "additionalFields": additional_fields,
                "identifierFields": identifier_fields,
                "createdAt": created_at,
            },
            headers={
                "content-type": "application/json",
                "idempotencyKey": str(idempotency_key) if idempotency_key is not None else None,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponseCustomerQuery,
                    parse_obj_as(
                        type_=PayabliApiResponseCustomerQuery,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def delete_customer(
        self, customer_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[PayabliApiResponse00Responsedatanonobject]:
        """
        Delete a customer record.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

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

        Returns
        -------
        AsyncHttpResponse[PayabliApiResponse00Responsedatanonobject]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Customer/{jsonable_encoder(customer_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponse00Responsedatanonobject,
                    parse_obj_as(
                        type_=PayabliApiResponse00Responsedatanonobject,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get_customer(
        self, customer_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[CustomerQueryRecords]:
        """
        Retrieves a customer's record and details.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

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

        Returns
        -------
        AsyncHttpResponse[CustomerQueryRecords]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Customer/{jsonable_encoder(customer_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CustomerQueryRecords,
                    parse_obj_as(
                        type_=CustomerQueryRecords,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def link_customer_transaction(
        self, customer_id: int, trans_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[PayabliApiResponse00Responsedatanonobject]:
        """
        Links a customer to a transaction by ID.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

        trans_id : str
            ReferenceId for the transaction (PaymentId).

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

        Returns
        -------
        AsyncHttpResponse[PayabliApiResponse00Responsedatanonobject]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Customer/link/{jsonable_encoder(customer_id)}/{jsonable_encoder(trans_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponse00Responsedatanonobject,
                    parse_obj_as(
                        type_=PayabliApiResponse00Responsedatanonobject,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def request_consent(
        self, customer_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[PayabliApiResponse00Responsedatanonobject]:
        """
        Sends the consent opt-in email to the customer email address in the customer record.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

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

        Returns
        -------
        AsyncHttpResponse[PayabliApiResponse00Responsedatanonobject]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Customer/{jsonable_encoder(customer_id)}/consent",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponse00Responsedatanonobject,
                    parse_obj_as(
                        type_=PayabliApiResponse00Responsedatanonobject,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def update_customer(
        self,
        customer_id: int,
        *,
        customer_number: typing.Optional[CustomerNumberNullable] = OMIT,
        customer_username: typing.Optional[str] = OMIT,
        customer_psw: typing.Optional[str] = OMIT,
        customer_status: typing.Optional[CustomerStatus] = OMIT,
        company: typing.Optional[str] = OMIT,
        firstname: typing.Optional[str] = OMIT,
        lastname: typing.Optional[str] = OMIT,
        phone: typing.Optional[str] = OMIT,
        email: typing.Optional[Email] = OMIT,
        address: typing.Optional[str] = OMIT,
        address_1: typing.Optional[str] = OMIT,
        city: typing.Optional[str] = OMIT,
        state: typing.Optional[str] = OMIT,
        zip: typing.Optional[str] = OMIT,
        country: typing.Optional[str] = OMIT,
        shipping_address: typing.Optional[Shippingaddress] = OMIT,
        shipping_address_1: typing.Optional[Shippingaddressadditional] = OMIT,
        shipping_city: typing.Optional[Shippingcity] = OMIT,
        shipping_state: typing.Optional[Shippingstate] = OMIT,
        shipping_zip: typing.Optional[Shippingzip] = OMIT,
        shipping_country: typing.Optional[Shippingcountry] = OMIT,
        balance: typing.Optional[float] = OMIT,
        time_zone: typing.Optional[Timezone] = OMIT,
        additional_fields: typing.Optional[typing.Dict[str, typing.Optional[str]]] = OMIT,
        identifier_fields: typing.Optional[Identifierfields] = OMIT,
        created_at: typing.Optional[CreatedAt] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[PayabliApiResponse00Responsedatanonobject]:
        """
        Update a customer record. Include only the fields you want to change.

        Parameters
        ----------
        customer_id : int
            Payabli-generated customer ID. Maps to "Customer ID" column in PartnerHub.

        customer_number : typing.Optional[CustomerNumberNullable]

        customer_username : typing.Optional[str]
            Customer username for customer portal

        customer_psw : typing.Optional[str]
            Customer password for customer portal

        customer_status : typing.Optional[CustomerStatus]

        company : typing.Optional[str]
            Company name

        firstname : typing.Optional[str]
            Customer first name

        lastname : typing.Optional[str]
            Customer last name

        phone : typing.Optional[str]
            Customer phone number

        email : typing.Optional[Email]
            Customer email address.

        address : typing.Optional[str]
            Customer address

        address_1 : typing.Optional[str]
            Additional customer address

        city : typing.Optional[str]
            Customer city

        state : typing.Optional[str]
            Customer State

        zip : typing.Optional[str]
            Customer postal code

        country : typing.Optional[str]
            Customer country in ISO-3166-1 alpha 2 format. See https://en.wikipedia.org/wiki/ISO_3166-1 for reference.

        shipping_address : typing.Optional[Shippingaddress]

        shipping_address_1 : typing.Optional[Shippingaddressadditional]

        shipping_city : typing.Optional[Shippingcity]

        shipping_state : typing.Optional[Shippingstate]

        shipping_zip : typing.Optional[Shippingzip]

        shipping_country : typing.Optional[Shippingcountry]

        balance : typing.Optional[float]
            Customer balance.

        time_zone : typing.Optional[Timezone]

        additional_fields : typing.Optional[typing.Dict[str, typing.Optional[str]]]
            Additional Custom fields in format "key":"value".

        identifier_fields : typing.Optional[Identifierfields]

        created_at : typing.Optional[CreatedAt]

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

        Returns
        -------
        AsyncHttpResponse[PayabliApiResponse00Responsedatanonobject]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Customer/{jsonable_encoder(customer_id)}",
            method="PUT",
            json={
                "customerNumber": customer_number,
                "customerUsername": customer_username,
                "customerPsw": customer_psw,
                "customerStatus": customer_status,
                "company": company,
                "firstname": firstname,
                "lastname": lastname,
                "phone": phone,
                "email": email,
                "address": address,
                "address1": address_1,
                "city": city,
                "state": state,
                "zip": zip,
                "country": country,
                "shippingAddress": shipping_address,
                "shippingAddress1": shipping_address_1,
                "shippingCity": shipping_city,
                "shippingState": shipping_state,
                "shippingZip": shipping_zip,
                "shippingCountry": shipping_country,
                "balance": balance,
                "timeZone": time_zone,
                "additionalFields": additional_fields,
                "identifierFields": identifier_fields,
                "createdAt": created_at,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PayabliApiResponse00Responsedatanonobject,
                    parse_obj_as(
                        type_=PayabliApiResponse00Responsedatanonobject,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 503:
                raise ServiceUnavailableError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        PayabliApiResponse,
                        parse_obj_as(
                            type_=PayabliApiResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
