Skip to content

Commit 45f6919

Browse files
committed
feat(alerting): Add batch actions for wallet alerts (#372)
* Add batch actions for wallet alerts * Fix linters
1 parent 8d24296 commit 45f6919

4 files changed

Lines changed: 149 additions & 5 deletions

File tree

lago_python_client/customers/wallet_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
NestedUpdateCommandMixin,
1111
)
1212
from ..models.wallet import WalletResponse
13-
from .wallets.metadata_client import CustomerWalletMetadataClient
1413
from .wallets.alert_client import CustomerWalletAlertClient
14+
from .wallets.metadata_client import CustomerWalletMetadataClient
1515

1616

1717
class CustomerWalletClient(

lago_python_client/customers/wallets/alert_client.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
from typing import ClassVar, Type
22

33
from ...base_client import BaseClient
4-
from ...models.alert import AlertResponse
5-
64
from ...mixins import (
75
NestedCreateCommandMixin,
8-
NestedUpdateCommandMixin,
96
NestedDestroyCommandMixin,
10-
NestedFindCommandMixin,
117
NestedFindAllCommandMixin,
8+
NestedFindCommandMixin,
9+
NestedUpdateCommandMixin,
10+
)
11+
from ...models.alert import AlertResponse, AlertsList
12+
from ...services.json import to_json
13+
from ...services.request import (
14+
make_headers,
15+
make_url,
16+
send_delete_request,
17+
send_post_request,
18+
)
19+
from ...services.response import (
20+
Response,
21+
get_response_data,
22+
prepare_index_response,
23+
verify_response,
1224
)
1325

1426

@@ -26,3 +38,27 @@ class CustomerWalletAlertClient(
2638

2739
def api_resource(self, customer_id: str, wallet_code: str) -> tuple[str]:
2840
return ("customers", customer_id, "wallets", wallet_code, "alerts")
41+
42+
def create_batch(self, customer_id: str, wallet_code: str, input_object: AlertsList) -> None:
43+
api_response: Response = send_post_request(
44+
url=make_url(origin=self.base_url, path_parts=self.api_resource(customer_id, wallet_code)),
45+
content=to_json(input_object.dict()),
46+
headers=make_headers(api_key=self.api_key),
47+
)
48+
49+
return prepare_index_response(
50+
api_resource="alerts",
51+
response_model=AlertResponse,
52+
data=get_response_data(response=api_response),
53+
)
54+
55+
def destroy_all(self, customer_id: str, wallet_code: str) -> None:
56+
api_response: Response = send_delete_request(
57+
url=make_url(
58+
origin=self.base_url,
59+
path_parts=self.api_resource(customer_id, wallet_code),
60+
),
61+
headers=make_headers(api_key=self.api_key),
62+
)
63+
64+
verify_response(api_response)

tests/fixtures/wallet_alert_index.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@
2222
"billable_metric": null,
2323
"created_at": "2025-03-20T10:00:00Z",
2424
"last_processed_at": "2025-05-19T10:04:21Z"
25+
},
26+
{
27+
"lago_id": "1a921a92-1a92-1a92-1a92-1a921a921a92",
28+
"lago_organization_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
29+
"external_subscription_id": null,
30+
"external_customer_id": "cus_0987654321",
31+
"lago_wallet_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
32+
"wallet_code": "wallet_code",
33+
"code": "wallet_credits_alert",
34+
"name": "Credits Balance Alert",
35+
"alert_type": "wallet_credits_balance",
36+
"direction": "increasing",
37+
"previous_value": 1000,
38+
"thresholds": [
39+
{
40+
"code": "warn",
41+
"recurring": false,
42+
"value": "99.0"
43+
}
44+
],
45+
"billable_metric": null,
46+
"created_at": "2025-03-20T10:00:00Z",
47+
"last_processed_at": "2025-05-19T10:04:21Z"
2548
}
2649
],
2750
"meta": {

tests/test_customer_wallet_alert_client.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from lago_python_client.exceptions import LagoApiError
66
from lago_python_client.models import (
77
Alert,
8+
AlertsList,
89
AlertThreshold,
910
AlertThresholdList,
1011
)
@@ -176,6 +177,7 @@ def test_valid_find_all_customer_wallet_alerts_request_with_options(httpx_mock:
176177
"customer_id", "wallet_code", options={"per_page": 2, "page": 1}
177178
)
178179

180+
assert len(response) == 2
179181
assert response["alerts"][0].lago_id == "1a901a90-1a90-1a90-1a90-1a901a901a90"
180182
assert response["meta"]["current_page"] == 1
181183

@@ -192,3 +194,86 @@ def test_invalid_find_all_wallet_alerts_request(httpx_mock: HTTPXMock):
192194

193195
with pytest.raises(LagoApiError):
194196
client.customers.wallets.alerts.find_all("customer_id", "wallet_code")
197+
198+
199+
def test_valid_create_batch_request(httpx_mock: HTTPXMock):
200+
client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d")
201+
202+
httpx_mock.add_response(
203+
method="POST",
204+
url="https://api.getlago.com/api/v1/customers/customer_id/wallets/wallet_code/alerts",
205+
content=mock_response("wallet_alert_index"),
206+
)
207+
208+
input = AlertsList(
209+
alerts=[
210+
Alert(
211+
alert_type="wallet_balance_amount",
212+
code="wallet_balance_alert",
213+
name="Balance Amount Alert",
214+
thresholds=[AlertThreshold(code="warn", value="1000")],
215+
),
216+
Alert(
217+
alert_type="wallet_credits_balance",
218+
code="wallet_credits_alert",
219+
name="Credits Balance Alert",
220+
thresholds=[AlertThreshold(value="2000")],
221+
),
222+
]
223+
)
224+
225+
response = client.customers.wallets.alerts.create_batch("customer_id", "wallet_code", input)
226+
227+
assert len(response["alerts"]) == 2
228+
229+
assert response["alerts"][0].lago_id == "1a901a90-1a90-1a90-1a90-1a901a901a90"
230+
assert response["alerts"][0].wallet_code == "wallet_code"
231+
assert response["alerts"][0].code == "wallet_balance_alert"
232+
assert response["alerts"][0].alert_type == "wallet_balance_amount"
233+
234+
assert response["alerts"][1].lago_id == "1a921a92-1a92-1a92-1a92-1a921a921a92"
235+
assert response["alerts"][1].wallet_code == "wallet_code"
236+
assert response["alerts"][1].code == "wallet_credits_alert"
237+
assert response["alerts"][1].alert_type == "wallet_credits_balance"
238+
239+
240+
def test_invalid_create_batch_request(httpx_mock: HTTPXMock):
241+
client = Client(api_key="invalid")
242+
243+
httpx_mock.add_response(
244+
method="POST",
245+
url="https://api.getlago.com/api/v1/customers/customer_id/wallets/wallet_code/alerts",
246+
status_code=401,
247+
content=b"",
248+
)
249+
250+
with pytest.raises(LagoApiError):
251+
client.customers.wallets.alerts.create_batch("customer_id", "wallet_code", AlertsList(alerts=[]))
252+
253+
254+
def test_valid_destroy_all_request(httpx_mock: HTTPXMock):
255+
client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d")
256+
257+
httpx_mock.add_response(
258+
method="DELETE",
259+
url="https://api.getlago.com/api/v1/customers/customer_id/wallets/wallet_code/alerts",
260+
status_code=200,
261+
content=b"",
262+
)
263+
264+
response = client.customers.wallets.alerts.destroy_all("customer_id", "wallet_code")
265+
assert response is None
266+
267+
268+
def test_invalid_destroy_all_request(httpx_mock: HTTPXMock):
269+
client = Client(api_key="invalid")
270+
271+
httpx_mock.add_response(
272+
method="DELETE",
273+
url="https://api.getlago.com/api/v1/customers/customer_id/wallets/wallet_code/alerts",
274+
status_code=404,
275+
content=b"",
276+
)
277+
278+
with pytest.raises(LagoApiError):
279+
client.customers.wallets.alerts.destroy_all("customer_id", "wallet_code")

0 commit comments

Comments
 (0)