diff --git a/lago_python_client/billable_metrics/clients.py b/lago_python_client/billable_metrics/clients.py index b3ebdbc0..f863d240 100644 --- a/lago_python_client/billable_metrics/clients.py +++ b/lago_python_client/billable_metrics/clients.py @@ -38,6 +38,7 @@ def evaluate_expression( path_parts=(self.API_RESOURCE, "evaluate_expression"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( diff --git a/lago_python_client/coupons/clients.py b/lago_python_client/coupons/clients.py index b29e9f11..aa198097 100644 --- a/lago_python_client/coupons/clients.py +++ b/lago_python_client/coupons/clients.py @@ -48,6 +48,7 @@ def destroy(self, external_customer_id: str, applied_coupon_id: str) -> AppliedC ), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( diff --git a/lago_python_client/credit_notes/clients.py b/lago_python_client/credit_notes/clients.py index ae91cacf..c68ac10c 100644 --- a/lago_python_client/credit_notes/clients.py +++ b/lago_python_client/credit_notes/clients.py @@ -43,6 +43,7 @@ def download(self, resource_id: str) -> Optional[CreditNoteResponse]: path_parts=(self.API_RESOURCE, resource_id, "download"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) response_data = get_response_data(response=api_response, key=self.ROOT_NAME) @@ -61,6 +62,7 @@ def void(self, resource_id: str) -> CreditNoteResponse: path_parts=(self.API_RESOURCE, resource_id, "void"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -80,6 +82,7 @@ def estimate(self, input_object: CreditNoteEstimate) -> CreditNoteEstimatedRespo } ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -97,6 +100,7 @@ def replace_metadata( ), content=to_json({"metadata": metadata}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -111,6 +115,7 @@ def merge_metadata( ), content=to_json({"metadata": metadata}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -122,6 +127,7 @@ def delete_all_metadata(self, resource_id: str) -> Optional[Dict[str, Optional[s path_parts=(self.API_RESOURCE, resource_id, "metadata"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -133,6 +139,7 @@ def delete_metadata_key(self, resource_id: str, key: str) -> Optional[Dict[str, path_parts=(self.API_RESOURCE, resource_id, "metadata", key), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") diff --git a/lago_python_client/customers/clients.py b/lago_python_client/customers/clients.py index 41573f03..28bcd652 100644 --- a/lago_python_client/customers/clients.py +++ b/lago_python_client/customers/clients.py @@ -93,6 +93,7 @@ def current_usage( query_pairs=query_params, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -114,6 +115,7 @@ def projected_usage( query_pairs=query_params, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -139,6 +141,7 @@ def past_usage( }, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_index_response( @@ -154,6 +157,7 @@ def portal_url(self, resource_id: str) -> str: path_parts=(self.API_RESOURCE, resource_id, "portal_url"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) response_data = get_response_data(response=api_response, key=self.ROOT_NAME) @@ -166,6 +170,7 @@ def checkout_url(self, resource_id: str) -> str: path_parts=(self.API_RESOURCE, resource_id, "checkout_url"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) response_data = get_response_data(response=api_response, key=self.ROOT_NAME) diff --git a/lago_python_client/customers/payment_methods_client.py b/lago_python_client/customers/payment_methods_client.py index c568b041..8353529d 100644 --- a/lago_python_client/customers/payment_methods_client.py +++ b/lago_python_client/customers/payment_methods_client.py @@ -21,6 +21,7 @@ def destroy(self, customer_external_id: str, payment_method_id: str) -> PaymentM path_parts=(self.PARENT_API_RESOURCE, customer_external_id, self.API_RESOURCE, payment_method_id), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -41,6 +42,7 @@ def set_as_default(self, customer_external_id: str, payment_method_id: str) -> P ), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( diff --git a/lago_python_client/customers/wallets/alert_client.py b/lago_python_client/customers/wallets/alert_client.py index 19558596..e3ef42df 100644 --- a/lago_python_client/customers/wallets/alert_client.py +++ b/lago_python_client/customers/wallets/alert_client.py @@ -44,6 +44,7 @@ def create_batch(self, customer_id: str, wallet_code: str, input_object: AlertsL url=make_url(origin=self.base_url, path_parts=self.api_resource(customer_id, wallet_code)), content=to_json(input_object.dict()), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_index_response( @@ -59,6 +60,7 @@ def destroy_all(self, customer_id: str, wallet_code: str) -> None: path_parts=self.api_resource(customer_id, wallet_code), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) verify_response(api_response) diff --git a/lago_python_client/customers/wallets/metadata_client.py b/lago_python_client/customers/wallets/metadata_client.py index e7aea314..54246832 100644 --- a/lago_python_client/customers/wallets/metadata_client.py +++ b/lago_python_client/customers/wallets/metadata_client.py @@ -33,6 +33,7 @@ def replace( ), content=to_json({"metadata": metadata}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -47,6 +48,7 @@ def merge( ), content=to_json({"metadata": metadata}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -58,6 +60,7 @@ def delete_all(self, customer_id: str, wallet_code: str) -> Optional[Dict[str, O path_parts=self.api_resource(customer_id, wallet_code), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -69,6 +72,7 @@ def delete_key(self, customer_id: str, wallet_code: str, key: str) -> Optional[D path_parts=self.api_resource(customer_id, wallet_code) + (key,), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") diff --git a/lago_python_client/events/clients.py b/lago_python_client/events/clients.py index 624b375a..1067c48d 100644 --- a/lago_python_client/events/clients.py +++ b/lago_python_client/events/clients.py @@ -39,6 +39,7 @@ def create(self, input_object: BaseModel, timeout: Optional[httpx.Timeout] = Non ), headers=make_headers(api_key=self.api_key), timeout=timeout, + rate_limit_retry_config=self.rate_limit_retry_config, ) # Process response data @@ -59,6 +60,7 @@ def batch_create(self, input_object: BaseModel) -> None: ), content=to_json(input_object.dict()), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) verify_response(api_response) @@ -74,6 +76,7 @@ def estimate_fees(self, input_object: BaseModel) -> Mapping[str, Any]: } ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_list_response( diff --git a/lago_python_client/gross_revenues/clients.py b/lago_python_client/gross_revenues/clients.py index 06381645..438ee1a7 100644 --- a/lago_python_client/gross_revenues/clients.py +++ b/lago_python_client/gross_revenues/clients.py @@ -26,6 +26,7 @@ def find_all(self, options: QueryPairs = None) -> Mapping[str, Any]: query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) # Process response data diff --git a/lago_python_client/invoice_collections/clients.py b/lago_python_client/invoice_collections/clients.py index fdae5309..0332bbd5 100644 --- a/lago_python_client/invoice_collections/clients.py +++ b/lago_python_client/invoice_collections/clients.py @@ -26,6 +26,7 @@ def find_all(self, options: QueryPairs = None) -> Mapping[str, Any]: query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) # Process response data diff --git a/lago_python_client/invoiced_usages/clients.py b/lago_python_client/invoiced_usages/clients.py index 7c4a30f1..f2fbc4cd 100644 --- a/lago_python_client/invoiced_usages/clients.py +++ b/lago_python_client/invoiced_usages/clients.py @@ -26,6 +26,7 @@ def find_all(self, options: QueryPairs = None) -> Mapping[str, Any]: query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) # Process response data diff --git a/lago_python_client/invoices/clients.py b/lago_python_client/invoices/clients.py index 30732fbf..c46e9b68 100644 --- a/lago_python_client/invoices/clients.py +++ b/lago_python_client/invoices/clients.py @@ -39,6 +39,7 @@ def download(self, resource_id: str) -> Optional[InvoiceResponse]: path_parts=(self.API_RESOURCE, resource_id, "download"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) response_data = get_response_data(response=api_response, key=self.ROOT_NAME) @@ -64,6 +65,7 @@ def retry_payment( ), content=to_json(payload) if payload else None, headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) response_data = get_response_data(response=api_response, key=self.ROOT_NAME) @@ -82,6 +84,7 @@ def refresh(self, resource_id: str) -> InvoiceResponse: path_parts=(self.API_RESOURCE, resource_id, "refresh"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -96,6 +99,7 @@ def retry(self, resource_id: str) -> InvoiceResponse: path_parts=(self.API_RESOURCE, resource_id, "retry"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -110,6 +114,7 @@ def finalize(self, resource_id: str) -> InvoiceResponse: path_parts=(self.API_RESOURCE, resource_id, "finalize"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -124,6 +129,7 @@ def void(self, resource_id: str) -> InvoiceResponse: path_parts=(self.API_RESOURCE, resource_id, "void"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -138,6 +144,7 @@ def lose_dispute(self, resource_id: str) -> InvoiceResponse: path_parts=(self.API_RESOURCE, resource_id, "lose_dispute"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -152,6 +159,7 @@ def payment_url(self, resource_id: str) -> str: path_parts=(self.API_RESOURCE, resource_id, "payment_url"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) response_data = get_response_data(response=api_response, key="invoice_payment_details") @@ -165,6 +173,7 @@ def preview(self, input_object: InvoicePreview) -> InvoiceResponse: ), content=to_json(input_object.dict()), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -180,6 +189,7 @@ def void_invoice(self, resource_id: str, options: Mapping[str, Any] = {}) -> Inv ), content=to_json(options) if options else None, headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( diff --git a/lago_python_client/mrrs/clients.py b/lago_python_client/mrrs/clients.py index c960b04e..edf1aef8 100644 --- a/lago_python_client/mrrs/clients.py +++ b/lago_python_client/mrrs/clients.py @@ -26,6 +26,7 @@ def find_all(self, options: QueryPairs = None) -> Mapping[str, Any]: query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) # Process response data diff --git a/lago_python_client/overdue_balances/clients.py b/lago_python_client/overdue_balances/clients.py index 852dfb67..44c1428d 100644 --- a/lago_python_client/overdue_balances/clients.py +++ b/lago_python_client/overdue_balances/clients.py @@ -32,6 +32,7 @@ def find_all( query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) # Process response data diff --git a/lago_python_client/plans/clients.py b/lago_python_client/plans/clients.py index c619ae68..f7083239 100644 --- a/lago_python_client/plans/clients.py +++ b/lago_python_client/plans/clients.py @@ -51,6 +51,7 @@ def replace_metadata( ), content=to_json({"metadata": metadata}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -65,6 +66,7 @@ def merge_metadata( ), content=to_json({"metadata": metadata}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -76,6 +78,7 @@ def delete_all_metadata(self, resource_id: str) -> Optional[Dict[str, Optional[s path_parts=(self.API_RESOURCE, resource_id, "metadata"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -87,6 +90,7 @@ def delete_metadata_key(self, resource_id: str, key: str) -> Optional[Dict[str, path_parts=(self.API_RESOURCE, resource_id, "metadata", key), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -105,6 +109,7 @@ def find_all_charge_filters( query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_index_response( @@ -120,6 +125,7 @@ def find_charge_filter(self, plan_code: str, charge_code: str, filter_id: str) - path_parts=(self.API_RESOURCE, plan_code, "charges", charge_code, "filters", filter_id), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -135,6 +141,7 @@ def create_charge_filter(self, plan_code: str, charge_code: str, input_object: B ), content=to_json({"filter": input_object.dict(exclude_none=True)}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -152,6 +159,7 @@ def update_charge_filter( ), content=to_json({"filter": input_object.dict(exclude_none=True)}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -166,6 +174,7 @@ def destroy_charge_filter(self, plan_code: str, charge_code: str, filter_id: str path_parts=(self.API_RESOURCE, plan_code, "charges", charge_code, "filters", filter_id), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( diff --git a/lago_python_client/subscriptions/clients.py b/lago_python_client/subscriptions/clients.py index 810dfeaa..e00f3b8e 100644 --- a/lago_python_client/subscriptions/clients.py +++ b/lago_python_client/subscriptions/clients.py @@ -58,6 +58,7 @@ def lifetime_usage(self, resource_id: str) -> LifetimeUsageResponse: path_parts=(self.API_RESOURCE, resource_id, "lifetime_usage"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -77,6 +78,7 @@ def update_lifetime_usage( {"lifetime_usage": {"external_historical_usage_amount_cents": external_historical_usage_amount_cents}} ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -98,6 +100,7 @@ def create_alerts( ), content=to_json(input_object.dict()), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_index_response( @@ -117,6 +120,7 @@ def delete_alerts(self, external_id: str, subscription_status: Optional[str] = N query_pairs=query_params, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) verify_response(api_response) @@ -134,6 +138,7 @@ def find_all_charge_filters( query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_index_response( @@ -155,6 +160,7 @@ def find_charge_filter( query_pairs=query_params, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -176,6 +182,7 @@ def create_charge_filter( ), content=to_json({"filter": input_object.dict(exclude_none=True)}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -202,6 +209,7 @@ def update_charge_filter( ), content=to_json({"filter": input_object.dict(exclude_none=True)}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( @@ -222,6 +230,7 @@ def destroy_charge_filter( query_pairs=query_params, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_response( diff --git a/lago_python_client/usages/clients.py b/lago_python_client/usages/clients.py index 60b14d47..df16fafd 100644 --- a/lago_python_client/usages/clients.py +++ b/lago_python_client/usages/clients.py @@ -26,6 +26,7 @@ def find_all(self, options: QueryPairs = None) -> Mapping[str, Any]: query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) # Process response data diff --git a/lago_python_client/wallets/clients.py b/lago_python_client/wallets/clients.py index 3a02515f..cdd7c125 100644 --- a/lago_python_client/wallets/clients.py +++ b/lago_python_client/wallets/clients.py @@ -57,6 +57,7 @@ def replace_metadata( ), content=to_json({"metadata": metadata}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -71,6 +72,7 @@ def merge_metadata( ), content=to_json({"metadata": metadata}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -82,6 +84,7 @@ def delete_all_metadata(self, resource_id: str) -> Optional[Dict[str, Optional[s path_parts=(self.API_RESOURCE, resource_id, "metadata"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -93,6 +96,7 @@ def delete_metadata_key(self, resource_id: str, key: str) -> Optional[Dict[str, path_parts=(self.API_RESOURCE, resource_id, "metadata", key), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return get_response_data(response=api_response, key="metadata") @@ -111,6 +115,7 @@ def create(self, input_object: BaseModel) -> Mapping[str, Any]: ), content=to_json({"wallet_transaction": input_object.dict(exclude_none=True)}), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_object_list_response( @@ -129,6 +134,7 @@ def find_all(self, wallet_id: str, options: QueryPairs = None) -> Mapping[str, A query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_index_response( @@ -144,6 +150,7 @@ def payment_url(self, resource_id: str) -> str: path_parts=(self.API_RESOURCE, resource_id, "payment_url"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) response_data = get_response_data(response=api_response, key="wallet_transaction_payment_details") @@ -159,6 +166,7 @@ def consumptions(self, resource_id: str, options: QueryPairs = None) -> Mapping[ query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_index_response( @@ -177,6 +185,7 @@ def fundings(self, resource_id: str, options: QueryPairs = None) -> Mapping[str, query_pairs=options, ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) return prepare_index_response( diff --git a/lago_python_client/webhooks/clients.py b/lago_python_client/webhooks/clients.py index 66b8e8f1..af5f7215 100644 --- a/lago_python_client/webhooks/clients.py +++ b/lago_python_client/webhooks/clients.py @@ -35,6 +35,7 @@ def public_key(self) -> bytes: path_parts=(self.API_RESOURCE, "json_public_key"), ), headers=make_headers(api_key=self.api_key), + rate_limit_retry_config=self.rate_limit_retry_config, ) response_data: Optional[Union[Mapping[str, Any], Sequence[Any]]] = get_response_data( response=api_response, key=self.ROOT_NAME diff --git a/tests/test_custom_action_rate_limit.py b/tests/test_custom_action_rate_limit.py new file mode 100644 index 00000000..b2ce2571 --- /dev/null +++ b/tests/test_custom_action_rate_limit.py @@ -0,0 +1,93 @@ +from importlib import import_module +from types import SimpleNamespace + +import pytest +from pytest_httpx import HTTPXMock + +from lago_python_client.exceptions import LagoRateLimitError +from lago_python_client.services.rate_limit import RateLimitRetryConfig + +INPUT = SimpleNamespace(dict=lambda **kwargs: {}) + + +@pytest.mark.parametrize( + "module, client_class, action, args", + [ + ('lago_python_client.billable_metrics.clients', 'BillableMetricClient', 'evaluate_expression', (INPUT,)), + ('lago_python_client.coupons.clients', 'AppliedCouponClient', 'destroy', ('test-id', 'test-id')), + ('lago_python_client.credit_notes.clients', 'CreditNoteClient', 'download', ('test-id',)), + ('lago_python_client.credit_notes.clients', 'CreditNoteClient', 'void', ('test-id',)), + ('lago_python_client.credit_notes.clients', 'CreditNoteClient', 'estimate', (INPUT,)), + ('lago_python_client.credit_notes.clients', 'CreditNoteClient', 'replace_metadata', ('test-id', {})), + ('lago_python_client.credit_notes.clients', 'CreditNoteClient', 'merge_metadata', ('test-id', {})), + ('lago_python_client.credit_notes.clients', 'CreditNoteClient', 'delete_all_metadata', ('test-id',)), + ('lago_python_client.credit_notes.clients', 'CreditNoteClient', 'delete_metadata_key', ('test-id', 'test-id')), + ('lago_python_client.customers.clients', 'CustomerClient', 'current_usage', ('test-id', 'test-id')), + ('lago_python_client.customers.clients', 'CustomerClient', 'projected_usage', ('test-id', 'test-id')), + ('lago_python_client.customers.clients', 'CustomerClient', 'past_usage', ('test-id', 'test-id')), + ('lago_python_client.customers.clients', 'CustomerClient', 'portal_url', ('test-id',)), + ('lago_python_client.customers.clients', 'CustomerClient', 'checkout_url', ('test-id',)), + ('lago_python_client.customers.payment_methods_client', 'CustomerPaymentMethodsClient', 'destroy', ('test-id', 'test-id')), + ('lago_python_client.customers.payment_methods_client', 'CustomerPaymentMethodsClient', 'set_as_default', ('test-id', 'test-id')), + ('lago_python_client.customers.wallets.alert_client', 'CustomerWalletAlertClient', 'create_batch', ('test-id', 'test-id', INPUT)), + ('lago_python_client.customers.wallets.alert_client', 'CustomerWalletAlertClient', 'destroy_all', ('test-id', 'test-id')), + ('lago_python_client.customers.wallets.metadata_client', 'CustomerWalletMetadataClient', 'replace', ('test-id', 'test-id', {})), + ('lago_python_client.customers.wallets.metadata_client', 'CustomerWalletMetadataClient', 'merge', ('test-id', 'test-id', {})), + ('lago_python_client.customers.wallets.metadata_client', 'CustomerWalletMetadataClient', 'delete_all', ('test-id', 'test-id')), + ('lago_python_client.customers.wallets.metadata_client', 'CustomerWalletMetadataClient', 'delete_key', ('test-id', 'test-id', 'test-id')), + ('lago_python_client.events.clients', 'EventClient', 'create', (INPUT,)), + ('lago_python_client.events.clients', 'EventClient', 'batch_create', (INPUT,)), + ('lago_python_client.events.clients', 'EventClient', 'estimate_fees', (INPUT,)), + ('lago_python_client.gross_revenues.clients', 'GrossRevenueClient', 'find_all', ()), + ('lago_python_client.invoice_collections.clients', 'InvoiceCollectionClient', 'find_all', ()), + ('lago_python_client.invoiced_usages.clients', 'InvoicedUsageClient', 'find_all', ()), + ('lago_python_client.mrrs.clients', 'MrrClient', 'find_all', ()), + ('lago_python_client.overdue_balances.clients', 'OverdueBalanceClient', 'find_all', ()), + ('lago_python_client.plans.clients', 'PlanClient', 'replace_metadata', ('test-id', {})), + ('lago_python_client.plans.clients', 'PlanClient', 'merge_metadata', ('test-id', {})), + ('lago_python_client.plans.clients', 'PlanClient', 'delete_all_metadata', ('test-id',)), + ('lago_python_client.plans.clients', 'PlanClient', 'delete_metadata_key', ('test-id', 'test-id')), + ('lago_python_client.plans.clients', 'PlanClient', 'find_all_charge_filters', ('test-id', 'test-id')), + ('lago_python_client.plans.clients', 'PlanClient', 'find_charge_filter', ('test-id', 'test-id', 'test-id')), + ('lago_python_client.plans.clients', 'PlanClient', 'create_charge_filter', ('test-id', 'test-id', INPUT)), + ('lago_python_client.plans.clients', 'PlanClient', 'update_charge_filter', ('test-id', 'test-id', 'test-id', INPUT)), + ('lago_python_client.plans.clients', 'PlanClient', 'destroy_charge_filter', ('test-id', 'test-id', 'test-id')), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'lifetime_usage', ('test-id',)), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'update_lifetime_usage', ('test-id', 1)), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'create_alerts', ('test-id', INPUT)), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'delete_alerts', ('test-id',)), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'find_all_charge_filters', ('test-id', 'test-id')), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'find_charge_filter', ('test-id', 'test-id', 'test-id')), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'create_charge_filter', ('test-id', 'test-id', INPUT)), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'update_charge_filter', ('test-id', 'test-id', 'test-id', INPUT)), + ('lago_python_client.subscriptions.clients', 'SubscriptionClient', 'destroy_charge_filter', ('test-id', 'test-id', 'test-id')), + ('lago_python_client.usages.clients', 'UsageClient', 'find_all', ()), + ('lago_python_client.wallets.clients', 'WalletClient', 'replace_metadata', ('test-id', {})), + ('lago_python_client.wallets.clients', 'WalletClient', 'merge_metadata', ('test-id', {})), + ('lago_python_client.wallets.clients', 'WalletClient', 'delete_all_metadata', ('test-id',)), + ('lago_python_client.wallets.clients', 'WalletClient', 'delete_metadata_key', ('test-id', 'test-id')), + ('lago_python_client.wallets.clients', 'WalletTransactionClient', 'create', (INPUT,)), + ('lago_python_client.wallets.clients', 'WalletTransactionClient', 'find_all', ('test-id',)), + ('lago_python_client.wallets.clients', 'WalletTransactionClient', 'payment_url', ('test-id',)), + ('lago_python_client.wallets.clients', 'WalletTransactionClient', 'consumptions', ('test-id',)), + ('lago_python_client.wallets.clients', 'WalletTransactionClient', 'fundings', ('test-id',)), + ('lago_python_client.webhooks.clients', 'WebhookClient', 'public_key', ()), + ], +) +@pytest.mark.parametrize("policy", [{"retry_on_rate_limit": False}, {"max_retries": 0}, {"max_retries": 1}]) +def test_custom_action_rate_limit_policy( + module, client_class, action, args, policy, httpx_mock: HTTPXMock, monkeypatch +): + monkeypatch.setattr("lago_python_client.services.rate_limit.time.sleep", lambda _: None) + client = getattr(import_module(module), client_class)( + base_url="https://api.getlago.com/api/v1", + base_ingest_url="https://api.getlago.com/api/v1", + api_key="test-key", + rate_limit_retry_config=RateLimitRetryConfig(**policy), + ) + httpx_mock.add_response(status_code=429, headers={"x-ratelimit-reset": "0"}, is_reusable=True) + + with pytest.raises(LagoRateLimitError): + getattr(client, action)(*args) + + assert len(httpx_mock.get_requests()) == (2 if policy.get("max_retries") == 1 else 1) diff --git a/tests/test_invoice_client.py b/tests/test_invoice_client.py index c1f43f61..737d0a98 100644 --- a/tests/test_invoice_client.py +++ b/tests/test_invoice_client.py @@ -4,7 +4,7 @@ from pytest_httpx import HTTPXMock from lago_python_client.client import Client -from lago_python_client.exceptions import LagoApiError +from lago_python_client.exceptions import LagoApiError, LagoRateLimitError from lago_python_client.models import ( Customer, Invoice, @@ -445,3 +445,60 @@ def test_valid_create_one_off_invoice_with_invoice_custom_section(httpx_mock: HT assert response.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba" assert response.invoice_type == "one_off" + + +@pytest.mark.parametrize( + "action", + [ + "download", + "retry_payment", + "refresh", + "retry", + "finalize", + "void", + "lose_dispute", + "payment_url", + "preview", + "void_invoice", + ], +) +@pytest.mark.parametrize("policy", [{"retry_on_rate_limit": False}, {"max_retries": 0}, {"max_retries": 1}]) +def test_invoice_action_rate_limit_policy(action, policy, httpx_mock: HTTPXMock, monkeypatch): + monkeypatch.setattr("lago_python_client.services.rate_limit.time.sleep", lambda _: None) + client = Client(api_key="test-key", **policy) + httpx_mock.add_response(status_code=429, headers={"x-ratelimit-reset": "0"}, is_reusable=True) + + with pytest.raises(LagoRateLimitError): + getattr(client.invoices, action)(invoice_preview() if action == "preview" else "invoice-id") + + expected_requests = 2 if policy.get("max_retries") == 1 else 1 + assert len(httpx_mock.get_requests()) == expected_requests + + +@pytest.mark.parametrize( + "action", + [ + "download", + "retry_payment", + "refresh", + "retry", + "finalize", + "void", + "lose_dispute", + "payment_url", + "preview", + "void_invoice", + ], +) +def test_invoice_action_rate_limit_callback(action, httpx_mock: HTTPXMock): + captured = [] + client = Client(api_key="test-key", on_rate_limit_info=captured.append) + httpx_mock.add_response( + content=mock_payment_url_response() if action == "payment_url" else mock_response(), + headers={"x-ratelimit-limit": "100", "x-ratelimit-remaining": "42"}, + ) + + getattr(client.invoices, action)(invoice_preview() if action == "preview" else "invoice-id") + + assert len(captured) == 1 + assert captured[0].remaining == 42