Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lago_python_client/customers/wallet_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
NestedUpdateCommandMixin,
)
from ..models.wallet import WalletResponse
from .wallets.alert_client import CustomerWalletAlertClient
from .wallets.metadata_client import CustomerWalletMetadataClient


Expand All @@ -35,3 +36,7 @@ def api_resource(self, customer_id: str) -> tuple[str]:
@callable_cached_property
def metadata(self) -> CustomerWalletMetadataClient:
return CustomerWalletMetadataClient(self.base_url, self.api_key)

@callable_cached_property
def alerts(self) -> CustomerWalletAlertClient:
return CustomerWalletAlertClient(self.base_url, self.api_key)
64 changes: 64 additions & 0 deletions lago_python_client/customers/wallets/alert_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from typing import ClassVar, Type

from ...base_client import BaseClient
from ...mixins import (
NestedCreateCommandMixin,
NestedDestroyCommandMixin,
NestedFindAllCommandMixin,
NestedFindCommandMixin,
NestedUpdateCommandMixin,
)
from ...models.alert import AlertResponse, AlertsList
from ...services.json import to_json
from ...services.request import (
make_headers,
make_url,
send_delete_request,
send_post_request,
)
from ...services.response import (
Response,
get_response_data,
prepare_index_response,
verify_response,
)


class CustomerWalletAlertClient(
NestedCreateCommandMixin[AlertResponse],
NestedUpdateCommandMixin[AlertResponse],
NestedDestroyCommandMixin[AlertResponse],
NestedFindCommandMixin[AlertResponse],
NestedFindAllCommandMixin[AlertResponse],
BaseClient,
):
API_RESOURCE: ClassVar[str] = "alerts"
RESPONSE_MODEL: ClassVar[Type[AlertResponse]] = AlertResponse
ROOT_NAME: ClassVar[str] = "alert"

def api_resource(self, customer_id: str, wallet_code: str) -> tuple[str]:
return ("customers", customer_id, "wallets", wallet_code, "alerts")

def create_batch(self, customer_id: str, wallet_code: str, input_object: AlertsList) -> None:
api_response: Response = send_post_request(
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),
)

return prepare_index_response(
api_resource="alerts",
response_model=AlertResponse,
data=get_response_data(response=api_response),
)

def destroy_all(self, customer_id: str, wallet_code: str) -> None:
api_response: Response = send_delete_request(
url=make_url(
origin=self.base_url,
path_parts=self.api_resource(customer_id, wallet_code),
),
headers=make_headers(api_key=self.api_key),
)

verify_response(api_response)
11 changes: 7 additions & 4 deletions lago_python_client/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class AlertThresholdList(BaseModel):


class Alert(BaseModel):
alert_type: str
code: str
alert_type: Optional[str]
code: Optional[str]
name: Optional[str]
thresholds: Optional[AlertThresholdList]
billable_metric_code: Optional[str]
thresholds: AlertThresholdList


class AlertsList(BaseModel):
Expand All @@ -39,10 +39,13 @@ class AlertThresholdResponseList(BaseResponseModel):
class AlertResponse(BaseResponseModel):
lago_id: str
lago_organization_id: str
external_subscription_id: str
external_subscription_id: Optional[str]
lago_wallet_id: Optional[str]
wallet_code: Optional[str]
alert_type: str
code: str
name: Optional[str]
direction: Optional[str]
previous_value: Optional[str]
last_processed_at: Optional[str]
thresholds: AlertThresholdResponseList
Expand Down
26 changes: 26 additions & 0 deletions tests/fixtures/wallet_alert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"alert": {
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"lago_organization_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_subscription_id": null,
"external_customer_id": "customer_id",
"lago_wallet_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"wallet_code": "wallet_code",
"code": "wallet_balance_alert",
"name": "Balance Amount Alert",
"alert_type": "wallet_balance_amount",
"direction": "increasing",
"previous_value": 1000,
"thresholds": [
{
"code": "warn",
"value": 10000,
"recurring": false
}
],
"billable_metric": null,
"created_at": "2025-03-20T10:00:00Z",
"last_processed_at": "2025-05-19T10:04:21Z"
}
}

57 changes: 57 additions & 0 deletions tests/fixtures/wallet_alert_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"alerts": [
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"lago_organization_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_subscription_id": null,
"external_customer_id": "cus_0987654321",
"lago_wallet_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"wallet_code": "wallet_code",
"code": "wallet_balance_alert",
"name": "Balance Amount Alert",
"alert_type": "wallet_balance_amount",
"direction": "increasing",
"previous_value": 1000,
"thresholds": [
{
"code": "warn",
"recurring": false,
"value": "99.0"
}
],
"billable_metric": null,
"created_at": "2025-03-20T10:00:00Z",
"last_processed_at": "2025-05-19T10:04:21Z"
},
{
"lago_id": "1a921a92-1a92-1a92-1a92-1a921a921a92",
"lago_organization_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_subscription_id": null,
"external_customer_id": "cus_0987654321",
"lago_wallet_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"wallet_code": "wallet_code",
"code": "wallet_credits_alert",
"name": "Credits Balance Alert",
"alert_type": "wallet_credits_balance",
"direction": "increasing",
"previous_value": 1000,
"thresholds": [
{
"code": "warn",
"recurring": false,
"value": "99.0"
}
],
"billable_metric": null,
"created_at": "2025-03-20T10:00:00Z",
"last_processed_at": "2025-05-19T10:04:21Z"
}
],
"meta": {
"current_page": 1,
"next_page": 2,
"prev_page": null,
"total_pages": 4,
"total_count": 70
}
}
Loading
Loading