# 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 ..core.serialization import convert_and_respect_annotation_metadata
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.bill_data import BillData
from ..types.entrypointfield import Entrypointfield
from ..types.force_customer_creation import ForceCustomerCreation
from ..types.idempotency_key import IdempotencyKey
from ..types.payabli_api_response import PayabliApiResponse
from ..types.payment_detail import PaymentDetail
from ..types.payor_data_request import PayorDataRequest
from ..types.schedule_detail import ScheduleDetail
from ..types.source import Source
from ..types.subdomain import Subdomain
from ..types.subscription_query_records import SubscriptionQueryRecords
from .types.add_subscription_response import AddSubscriptionResponse
from .types.remove_subscription_response import RemoveSubscriptionResponse
from .types.request_schedule_payment_method import RequestSchedulePaymentMethod
from .types.set_pause import SetPause
from .types.update_subscription_response import UpdateSubscriptionResponse

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


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

    def get_subscription(
        self, sub_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[SubscriptionQueryRecords]:
        """
        Retrieves a single subscription's details.

        Parameters
        ----------
        sub_id : int
            The subscription ID.

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

        Returns
        -------
        HttpResponse[SubscriptionQueryRecords]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Subscription/{jsonable_encoder(sub_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SubscriptionQueryRecords,
                    parse_obj_as(
                        type_=SubscriptionQueryRecords,  # 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 new_subscription(
        self,
        *,
        force_customer_creation: typing.Optional[ForceCustomerCreation] = None,
        idempotency_key: typing.Optional[IdempotencyKey] = None,
        customer_data: typing.Optional[PayorDataRequest] = OMIT,
        entry_point: typing.Optional[Entrypointfield] = OMIT,
        invoice_data: typing.Optional[BillData] = OMIT,
        payment_details: typing.Optional[PaymentDetail] = OMIT,
        payment_method: typing.Optional[RequestSchedulePaymentMethod] = OMIT,
        schedule_details: typing.Optional[ScheduleDetail] = OMIT,
        set_pause: typing.Optional[SetPause] = OMIT,
        source: typing.Optional[Source] = OMIT,
        subdomain: typing.Optional[Subdomain] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[AddSubscriptionResponse]:
        """
        Creates a subscription or scheduled payment to run at a specified time and frequency.

        Parameters
        ----------
        force_customer_creation : typing.Optional[ForceCustomerCreation]

        idempotency_key : typing.Optional[IdempotencyKey]

        customer_data : typing.Optional[PayorDataRequest]
            Object describing the customer/payor.

        entry_point : typing.Optional[Entrypointfield]

        invoice_data : typing.Optional[BillData]
            Object describing an Invoice linked to the subscription.

        payment_details : typing.Optional[PaymentDetail]
            Object describing details of the payment. To skip the payment, set the `totalAmount` to 0. Payments will be paused until the amount is updated to a non-zero value. When `totalAmount` is set to 0, the `serviceFee` must also be set to 0.

        payment_method : typing.Optional[RequestSchedulePaymentMethod]
            Information about the payment method for the transaction. Required and recommended fields for each payment method type are described in each schema below.

        schedule_details : typing.Optional[ScheduleDetail]
            Object describing the schedule for subscription.

        set_pause : typing.Optional[SetPause]

        source : typing.Optional[Source]

        subdomain : typing.Optional[Subdomain]

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

        Returns
        -------
        HttpResponse[AddSubscriptionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "Subscription/add",
            method="POST",
            params={
                "forceCustomerCreation": force_customer_creation,
            },
            json={
                "customerData": convert_and_respect_annotation_metadata(
                    object_=customer_data, annotation=PayorDataRequest, direction="write"
                ),
                "entryPoint": entry_point,
                "invoiceData": convert_and_respect_annotation_metadata(
                    object_=invoice_data, annotation=BillData, direction="write"
                ),
                "paymentDetails": convert_and_respect_annotation_metadata(
                    object_=payment_details, annotation=PaymentDetail, direction="write"
                ),
                "paymentMethod": convert_and_respect_annotation_metadata(
                    object_=payment_method, annotation=RequestSchedulePaymentMethod, direction="write"
                ),
                "scheduleDetails": convert_and_respect_annotation_metadata(
                    object_=schedule_details, annotation=ScheduleDetail, direction="write"
                ),
                "setPause": set_pause,
                "source": source,
                "subdomain": subdomain,
            },
            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(
                    AddSubscriptionResponse,
                    parse_obj_as(
                        type_=AddSubscriptionResponse,  # 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 remove_subscription(
        self, sub_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[RemoveSubscriptionResponse]:
        """
        Deletes a subscription, autopay, or recurring payment and prevents future charges.

        Parameters
        ----------
        sub_id : int
            The subscription ID.

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

        Returns
        -------
        HttpResponse[RemoveSubscriptionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Subscription/{jsonable_encoder(sub_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RemoveSubscriptionResponse,
                    parse_obj_as(
                        type_=RemoveSubscriptionResponse,  # 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_subscription(
        self,
        sub_id: int,
        *,
        payment_details: typing.Optional[PaymentDetail] = OMIT,
        schedule_details: typing.Optional[ScheduleDetail] = OMIT,
        set_pause: typing.Optional[SetPause] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UpdateSubscriptionResponse]:
        """
        Updates a subscription's details.

        Parameters
        ----------
        sub_id : int
            The subscription ID.

        payment_details : typing.Optional[PaymentDetail]
            Object describing details of the payment. To skip the payment, set the `totalAmount` to 0. Payments will be paused until the amount is updated to a non-zero value. When `totalAmount` is set to 0, the `serviceFee` must also be set to 0.

        schedule_details : typing.Optional[ScheduleDetail]
            Object describing the schedule for subscription

        set_pause : typing.Optional[SetPause]

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

        Returns
        -------
        HttpResponse[UpdateSubscriptionResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"Subscription/{jsonable_encoder(sub_id)}",
            method="PUT",
            json={
                "paymentDetails": convert_and_respect_annotation_metadata(
                    object_=payment_details, annotation=PaymentDetail, direction="write"
                ),
                "scheduleDetails": convert_and_respect_annotation_metadata(
                    object_=schedule_details, annotation=ScheduleDetail, direction="write"
                ),
                "setPause": set_pause,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateSubscriptionResponse,
                    parse_obj_as(
                        type_=UpdateSubscriptionResponse,  # 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 AsyncRawSubscriptionClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def get_subscription(
        self, sub_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[SubscriptionQueryRecords]:
        """
        Retrieves a single subscription's details.

        Parameters
        ----------
        sub_id : int
            The subscription ID.

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

        Returns
        -------
        AsyncHttpResponse[SubscriptionQueryRecords]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Subscription/{jsonable_encoder(sub_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    SubscriptionQueryRecords,
                    parse_obj_as(
                        type_=SubscriptionQueryRecords,  # 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 new_subscription(
        self,
        *,
        force_customer_creation: typing.Optional[ForceCustomerCreation] = None,
        idempotency_key: typing.Optional[IdempotencyKey] = None,
        customer_data: typing.Optional[PayorDataRequest] = OMIT,
        entry_point: typing.Optional[Entrypointfield] = OMIT,
        invoice_data: typing.Optional[BillData] = OMIT,
        payment_details: typing.Optional[PaymentDetail] = OMIT,
        payment_method: typing.Optional[RequestSchedulePaymentMethod] = OMIT,
        schedule_details: typing.Optional[ScheduleDetail] = OMIT,
        set_pause: typing.Optional[SetPause] = OMIT,
        source: typing.Optional[Source] = OMIT,
        subdomain: typing.Optional[Subdomain] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[AddSubscriptionResponse]:
        """
        Creates a subscription or scheduled payment to run at a specified time and frequency.

        Parameters
        ----------
        force_customer_creation : typing.Optional[ForceCustomerCreation]

        idempotency_key : typing.Optional[IdempotencyKey]

        customer_data : typing.Optional[PayorDataRequest]
            Object describing the customer/payor.

        entry_point : typing.Optional[Entrypointfield]

        invoice_data : typing.Optional[BillData]
            Object describing an Invoice linked to the subscription.

        payment_details : typing.Optional[PaymentDetail]
            Object describing details of the payment. To skip the payment, set the `totalAmount` to 0. Payments will be paused until the amount is updated to a non-zero value. When `totalAmount` is set to 0, the `serviceFee` must also be set to 0.

        payment_method : typing.Optional[RequestSchedulePaymentMethod]
            Information about the payment method for the transaction. Required and recommended fields for each payment method type are described in each schema below.

        schedule_details : typing.Optional[ScheduleDetail]
            Object describing the schedule for subscription.

        set_pause : typing.Optional[SetPause]

        source : typing.Optional[Source]

        subdomain : typing.Optional[Subdomain]

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

        Returns
        -------
        AsyncHttpResponse[AddSubscriptionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "Subscription/add",
            method="POST",
            params={
                "forceCustomerCreation": force_customer_creation,
            },
            json={
                "customerData": convert_and_respect_annotation_metadata(
                    object_=customer_data, annotation=PayorDataRequest, direction="write"
                ),
                "entryPoint": entry_point,
                "invoiceData": convert_and_respect_annotation_metadata(
                    object_=invoice_data, annotation=BillData, direction="write"
                ),
                "paymentDetails": convert_and_respect_annotation_metadata(
                    object_=payment_details, annotation=PaymentDetail, direction="write"
                ),
                "paymentMethod": convert_and_respect_annotation_metadata(
                    object_=payment_method, annotation=RequestSchedulePaymentMethod, direction="write"
                ),
                "scheduleDetails": convert_and_respect_annotation_metadata(
                    object_=schedule_details, annotation=ScheduleDetail, direction="write"
                ),
                "setPause": set_pause,
                "source": source,
                "subdomain": subdomain,
            },
            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(
                    AddSubscriptionResponse,
                    parse_obj_as(
                        type_=AddSubscriptionResponse,  # 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 remove_subscription(
        self, sub_id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[RemoveSubscriptionResponse]:
        """
        Deletes a subscription, autopay, or recurring payment and prevents future charges.

        Parameters
        ----------
        sub_id : int
            The subscription ID.

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

        Returns
        -------
        AsyncHttpResponse[RemoveSubscriptionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Subscription/{jsonable_encoder(sub_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RemoveSubscriptionResponse,
                    parse_obj_as(
                        type_=RemoveSubscriptionResponse,  # 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_subscription(
        self,
        sub_id: int,
        *,
        payment_details: typing.Optional[PaymentDetail] = OMIT,
        schedule_details: typing.Optional[ScheduleDetail] = OMIT,
        set_pause: typing.Optional[SetPause] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UpdateSubscriptionResponse]:
        """
        Updates a subscription's details.

        Parameters
        ----------
        sub_id : int
            The subscription ID.

        payment_details : typing.Optional[PaymentDetail]
            Object describing details of the payment. To skip the payment, set the `totalAmount` to 0. Payments will be paused until the amount is updated to a non-zero value. When `totalAmount` is set to 0, the `serviceFee` must also be set to 0.

        schedule_details : typing.Optional[ScheduleDetail]
            Object describing the schedule for subscription

        set_pause : typing.Optional[SetPause]

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

        Returns
        -------
        AsyncHttpResponse[UpdateSubscriptionResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"Subscription/{jsonable_encoder(sub_id)}",
            method="PUT",
            json={
                "paymentDetails": convert_and_respect_annotation_metadata(
                    object_=payment_details, annotation=PaymentDetail, direction="write"
                ),
                "scheduleDetails": convert_and_respect_annotation_metadata(
                    object_=schedule_details, annotation=ScheduleDetail, direction="write"
                ),
                "setPause": set_pause,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdateSubscriptionResponse,
                    parse_obj_as(
                        type_=UpdateSubscriptionResponse,  # 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)
