Skip to content

Commit

Permalink
feat(felixible aggregation): Add documentation for expression evaluat…
Browse files Browse the repository at this point in the history
…ion endpoint
  • Loading branch information
vincent-pochet committed Oct 24, 2024
1 parent c5bf056 commit 6d831b8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
20 changes: 18 additions & 2 deletions lago_python_client/billable_metrics/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
FindCommandMixin,
UpdateCommandMixin,
)
from ..models.billable_metric import BillableMetricResponse

from ..models.billable_metric import BillableMetricEvaluateExpression, BillableMetricEvaluateExpressionResponse, BillableMetricResponse
from ..services.request import make_headers, make_url, send_post_request
from ..services.response import Response, prepare_object_response, get_response_data

class BillableMetricClient(
CreateCommandMixin[BillableMetricResponse],
Expand All @@ -22,3 +23,18 @@ class BillableMetricClient(
API_RESOURCE: ClassVar[str] = "billable_metrics"
RESPONSE_MODEL: ClassVar[Type[BillableMetricResponse]] = BillableMetricResponse
ROOT_NAME: ClassVar[str] = "billable_metric"


def evaluate_expression(self, input_object: BillableMetricEvaluateExpression) -> BillableMetricEvaluateExpressionResponse:
api_response: Response = send_post_request(
url=make_url(
origin=self.base_url,
path_parts=(self.API_RESOURCE, "evaluate_expression"),
),
headers=make_headers(api_key=self.api_key),
)

return prepare_object_response(
response_model=BillableMetricEvaluateExpressionResponse,
data=get_response_data(response=api_response, key='expression_result'),
)
3 changes: 3 additions & 0 deletions lago_python_client/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
BillableMetric as BillableMetric,
BillableMetricFilter as BillableMetricFilter,
BillableMetricFilters as BillableMetricFilters,
BillableMetricEvaluateExpressionEvent as BillableMetricEvaluateExpressionEvent,
BillableMetricEvaluateExpression as BillableMetricEvaluateExpression,
BillableMetricEvaluateExpressionResponse as BillableMetricEvaluateExpressionResponse,
)
from .charge import (
Charge as Charge,
Expand Down
14 changes: 13 additions & 1 deletion lago_python_client/models/billable_metric.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional
from typing import Any, Dict, List, Optional, Union

from lago_python_client.base_model import BaseModel

Expand Down Expand Up @@ -41,3 +41,15 @@ class BillableMetricResponse(BaseResponseModel):
active_subscriptions_count: int
draft_invoices_count: int
plans_count: int

class BillableMetricEvaluateExpressionEvent(BaseModel):
code: Optional[str]
timestamp: Optional[Union[str, int]]
properties: Optional[Dict[str, Any]]

class BillableMetricEvaluateExpression(BaseModel):
expression: str
event: BillableMetricEvaluateExpressionEvent

class BillableMetricEvaluateExpressionResponse(BaseResponseModel):
value: Union[str, int, float]
5 changes: 5 additions & 0 deletions tests/fixtures/billable_metric_evaluate_expression.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"expression_result": {
"value": "2.0"
}
}
34 changes: 34 additions & 0 deletions tests/test_billable_metric_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
BillableMetric,
BillableMetricFilter,
BillableMetricFilters,
BillableMetricEvaluateExpression,
BillableMetricEvaluateExpressionEvent,
)


Expand All @@ -25,6 +27,17 @@ def billable_metric_object():
)


def billable_metric_evaluate_expression_object():
return BillableMetricEvaluateExpression(
expression="round(event.properties.value * event.properties.units)",
event=BillableMetricEvaluateExpressionEvent(
code="event_code",
timestamp=1651240791,
properties={"value": 10, "units": 2},
)
)


def filters():
return BillableMetricFilters(__root__=[BillableMetricFilter(key="country", values=["france", "italy", "spain"])])

Expand All @@ -45,6 +58,14 @@ def mock_collection_response():
return billable_metric_response.read()


def mock_evaluate_expression_response():
this_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(this_dir, "fixtures/billable_metric_evaluate_expression.json")

with open(data_path, "rb") as evaluate_expression_response:
return evaluate_expression_response.read()


def test_valid_create_billable_metric_request(httpx_mock: HTTPXMock):
client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d")

Expand Down Expand Up @@ -204,3 +225,16 @@ def test_invalid_find_all_billable_metric_request(httpx_mock: HTTPXMock):

with pytest.raises(LagoApiError):
client.billable_metrics.find_all()


def test_valid_evaluate_billable_metric_expression(httpx_mock: HTTPXMock):
client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d")

httpx_mock.add_response(
method="POST",
url="https://api.getlago.com/api/v1/billable_metrics/evaluate_expression",
content=mock_evaluate_expression_response(),
)
response = client.billable_metrics.evaluate_expression(billable_metric_evaluate_expression_object())

assert response.value == "2.0"

0 comments on commit 6d831b8

Please sign in to comment.