Skip to content

Commit a45c8d2

Browse files
committed
Add subscription alerts
1 parent cfc72ba commit a45c8d2

7 files changed

Lines changed: 383 additions & 0 deletions

File tree

lago_python_client/models/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
from .activity_log import ActivityLog as ActivityLog, ActivityLogResponse as ActivityLogResponse
2+
from .alert import (
3+
Alert as Alert,
4+
AlertThreshold as AlertThreshold,
5+
AlertThresholdList as AlertThresholdList,
6+
AlertResponse as AlertResponse,
7+
AlertThresholdResponse as AlertThresholdResponse,
8+
AlertThresholdResponseList as AlertThresholdResponseList,
9+
)
210
from .api_log import ApiLog as ApiLog, ApiLogResponse as ApiLogResponse
311
from .applied_coupon import AppliedCoupon as AppliedCoupon
412
from .billable_metric import (

lago_python_client/models/alert.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import List, Optional
2+
3+
from ..base_model import BaseModel, BaseResponseModel
4+
from .billable_metric import BillableMetricResponse
5+
6+
7+
class AlertThreshold(BaseModel):
8+
code: Optional[str]
9+
value: Optional[str]
10+
recurring: Optional[bool]
11+
12+
13+
class AlertThresholdList(BaseModel):
14+
__root__: List[AlertThreshold]
15+
16+
17+
class Alert(BaseModel):
18+
alert_type: Optional[str]
19+
code: Optional[str]
20+
name: Optional[str]
21+
thresholds: Optional[AlertThresholdList]
22+
billable_metric_code: Optional[str]
23+
24+
25+
class AlertThresholdResponse(BaseResponseModel):
26+
code: str
27+
value: str
28+
recurring: bool
29+
30+
31+
class AlertThresholdResponseList(BaseResponseModel):
32+
__root__: List[AlertThresholdResponse]
33+
34+
35+
class AlertResponse(BaseResponseModel):
36+
lago_id: str
37+
lago_organization_id: str
38+
external_subscription_id: Optional[str]
39+
lago_wallet_id: Optional[str]
40+
wallet_code: Optional[str]
41+
alert_type: str
42+
code: str
43+
name: str
44+
direction: str
45+
previous_value: int
46+
last_processed_at: str
47+
thresholds: AlertThresholdResponseList
48+
created_at: str
49+
billable_metric: Optional[BillableMetricResponse]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import ClassVar, Type
2+
3+
from ..base_client import BaseClient
4+
from ..models.alert import AlertResponse
5+
6+
from ..mixins import (
7+
NestedCreateCommandMixin,
8+
NestedUpdateCommandMixin,
9+
NestedDestroyCommandMixin,
10+
NestedFindCommandMixin,
11+
NestedFindAllCommandMixin,
12+
)
13+
14+
15+
class SubscriptionAlertClient(
16+
NestedCreateCommandMixin[AlertResponse],
17+
NestedUpdateCommandMixin[AlertResponse],
18+
NestedDestroyCommandMixin[AlertResponse],
19+
NestedFindCommandMixin[AlertResponse],
20+
NestedFindAllCommandMixin[AlertResponse],
21+
BaseClient,
22+
):
23+
API_RESOURCE: ClassVar[str] = "alerts"
24+
RESPONSE_MODEL: ClassVar[Type[AlertResponse]] = AlertResponse
25+
ROOT_NAME: ClassVar[str] = "alert"
26+
27+
def api_resource(self, subscription_id: str) -> tuple[str]:
28+
return ("subscriptions", subscription_id, "alerts")

lago_python_client/subscriptions/clients.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any, ClassVar, Mapping, Type
22

3+
from ..functools_ext import callable_cached_property
34
from ..base_client import BaseClient
45
from ..mixins import (
56
CreateCommandMixin,
@@ -25,6 +26,8 @@
2526
)
2627
from ..services.json import to_json
2728

29+
from .alert_client import SubscriptionAlertClient
30+
2831

2932
class SubscriptionClient(
3033
CreateCommandMixin[SubscriptionResponse],
@@ -38,6 +41,10 @@ class SubscriptionClient(
3841
RESPONSE_MODEL: ClassVar[Type[SubscriptionResponse]] = SubscriptionResponse
3942
ROOT_NAME: ClassVar[str] = "subscription"
4043

44+
@callable_cached_property
45+
def alerts(self) -> SubscriptionAlertClient:
46+
return SubscriptionAlertClient(self.base_url, self.api_key)
47+
4148
def lifetime_usage(self, resource_id: str) -> LifetimeUsageResponse:
4249
api_response: Response = send_get_request(
4350
url=make_url(
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"alert": {
3+
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
4+
"lago_organization_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
5+
"external_subscription_id": "subscription_id",
6+
"lago_wallet_id": null,
7+
"wallet_code": null,
8+
"alert_type": "billable_metric_current_usage_amount",
9+
"code": "storage_threshold_alert",
10+
"name": "Storage Usage Alert",
11+
"billable_metric": {
12+
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
13+
"name": "Storage",
14+
"code": "storage",
15+
"description": "GB of storage used in my application",
16+
"recurring": false,
17+
"rounding_function": "round",
18+
"rounding_precision": 2,
19+
"created_at": "2022-09-14T16:35:31Z",
20+
"expression": "round((ended_at - started_at) * units)",
21+
"field_name": "gb",
22+
"aggregation_type": "sum_agg",
23+
"weighted_interval": "seconds",
24+
"filters": [
25+
{
26+
"key": "region",
27+
"values": [
28+
"us-east-1"
29+
]
30+
}
31+
]
32+
},
33+
"previous_value": 1000,
34+
"direction": "increasing",
35+
"thresholds": [
36+
{
37+
"code": "warn",
38+
"recurring": false,
39+
"value": "99.0"
40+
}
41+
],
42+
"created_at": "2025-03-20T10:00:00Z",
43+
"last_processed_at": "2025-05-19T10:04:21Z"
44+
}
45+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"alerts": [
3+
{
4+
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
5+
"lago_organization_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
6+
"external_subscription_id": "subscription_id",
7+
"lago_wallet_id": null,
8+
"wallet_code": null,
9+
"alert_type": "billable_metric_current_usage_amount",
10+
"code": "storage_threshold_alert",
11+
"name": "Storage Usage Alert",
12+
"billable_metric": {
13+
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
14+
"name": "Storage",
15+
"code": "storage",
16+
"description": "GB of storage used in my application",
17+
"recurring": false,
18+
"rounding_function": "round",
19+
"rounding_precision": 2,
20+
"created_at": "2022-09-14T16:35:31Z",
21+
"expression": "round((ended_at - started_at) * units)",
22+
"field_name": "gb",
23+
"aggregation_type": "sum_agg",
24+
"weighted_interval": "seconds",
25+
"filters": [
26+
{
27+
"key": "region",
28+
"values": [
29+
"us-east-1"
30+
]
31+
}
32+
]
33+
},
34+
"previous_value": 1000,
35+
"direction": "increasing",
36+
"thresholds": [
37+
{
38+
"code": "warn",
39+
"recurring": false,
40+
"value": "99.0"
41+
}
42+
],
43+
"created_at": "2025-03-20T10:00:00Z",
44+
"last_processed_at": "2025-05-19T10:04:21Z"
45+
}
46+
],
47+
"meta": {
48+
"current_page": 1,
49+
"next_page": 2,
50+
"prev_page": null,
51+
"total_pages": 4,
52+
"total_count": 70
53+
}
54+
}

0 commit comments

Comments
 (0)