Skip to content
Merged
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
22 changes: 19 additions & 3 deletions lago_python_client/customers/clients.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from collections.abc import Mapping
from typing import Any, ClassVar, Optional, Type
from typing import Any, ClassVar, Optional, Type, Union

from ..base_client import BaseClient
from ..mixins import (
Expand Down Expand Up @@ -32,11 +33,26 @@ class CustomerClient(
ROOT_NAME: ClassVar[str] = "customer"

def current_usage(
self, resource_id: str, external_subscription_id: str, apply_taxes: Optional[str] = None
self,
resource_id: str,
external_subscription_id: str,
apply_taxes: Optional[str] = None,
filter_by_charge_id: Optional[str] = None,
filter_by_charge_code: Optional[str] = None,
filter_by_group: Optional[dict] = None,
full_usage: Optional[bool] = None,
) -> CustomerUsageResponse:
query_params = {"external_subscription_id": external_subscription_id}
query_params: dict[str, Union[str, bool]] = {"external_subscription_id": external_subscription_id}
if apply_taxes is not None:
query_params["apply_taxes"] = apply_taxes
if filter_by_charge_id is not None:
query_params["filter_by_charge_id"] = filter_by_charge_id
if filter_by_charge_code is not None:
query_params["filter_by_charge_code"] = filter_by_charge_code
if filter_by_group is not None:
query_params["filter_by_group"] = json.dumps(filter_by_group)
if full_usage is not None:
query_params["full_usage"] = str(full_usage).lower()

api_response: Response = send_get_request(
url=make_url(
Expand Down
28 changes: 28 additions & 0 deletions tests/test_customer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,34 @@ def test_valid_current_usage_without_taxes(httpx_mock: HTTPXMock):
assert response.charges_usage[0].filters[0].values["country"] == ["france"]


def test_valid_current_usage_with_filters(httpx_mock: HTTPXMock):
client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d")
charge_id = "1a901a90-1a90-1a90-1a90-1a901a901a90"

httpx_mock.add_response(
method="GET",
url=(
"https://api.getlago.com/api/v1/customers/external_customer_id/current_usage"
f"?external_subscription_id=123&filter_by_charge_id={charge_id}"
"&filter_by_charge_code=storage"
"&filter_by_group=%7B%22cloud%22%3A+%22aws%22%7D"
"&full_usage=true"
),
content=mock_response("customer_usage"),
)
response = client.customers.current_usage(
"external_customer_id",
"123",
filter_by_charge_id=charge_id,
filter_by_charge_code="storage",
filter_by_group={"cloud": "aws"},
full_usage=True,
)

assert response.from_datetime == "2022-07-01T00:00:00Z"
assert len(response.charges_usage) == 1


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

Expand Down