Skip to content

feat(projected-usage): Add projected usage endpoint for customer API #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions lago_python_client/customers/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from ..models.customer import CustomerResponse
from ..models.customer_usage import CustomerUsageResponse
from ..models.customer_projected_usage import CustomerProjectedUsageResponse
from ..services.request import make_headers, make_url, send_get_request, send_post_request, QueryPairs
from ..services.response import (
get_response_data,
Expand Down Expand Up @@ -56,6 +57,27 @@ def current_usage(
data=get_response_data(response=api_response, key="customer_usage"),
)

def projected_usage(
self, resource_id: str, external_subscription_id: str, apply_taxes: Optional[str] = None
) -> CustomerProjectedUsageResponse:
query_params = {"external_subscription_id": external_subscription_id}
if apply_taxes is not None:
query_params["apply_taxes"] = apply_taxes

api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
path_parts=(self.API_RESOURCE, resource_id, "projected_usage"),
query_pairs=query_params,
),
headers=make_headers(api_key=self.api_key),
)

return prepare_object_response(
response_model=CustomerProjectedUsageResponse,
data=get_response_data(response=api_response, key="customer_projected_usage"),
)

def past_usage(
self,
resource_id: str,
Expand Down
73 changes: 73 additions & 0 deletions lago_python_client/models/customer_projected_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from typing import Dict, List, Optional

from lago_python_client.base_model import BaseModel

from ..base_model import BaseResponseModel


class Metric(BaseModel):
lago_id: str
name: str
code: str
aggregation_type: str


class PricingUnitDetails(BaseModel):
amount_cents: int
short_name: str
conversion_rate: float


class ProjectedChargeFilterUsage(BaseModel):
invoice_display_name: Optional[str]
values: Dict[str, List[str]]
units: str
projected_units: str
amount_cents: int
projected_amount_cents: int
events_count: int
pricing_unit_details: Optional[PricingUnitDetails]


class ChargeObject(BaseModel):
lago_id: str
charge_model: str
invoice_display_name: Optional[str]


class ProjectedGroupedUsage(BaseModel):
amount_cents: int
projected_amount_cents: int
events_count: int
units: str
projected_units: str
grouped_by: Dict[str, str]
filters: List[ProjectedChargeFilterUsage]
pricing_unit_details: Optional[PricingUnitDetails]


class ProjectedChargeUsage(BaseModel):
units: str
projected_units: str
events_count: int
amount_cents: int
projected_amount_cents: int
amount_currency: str
charge: ChargeObject
billable_metric: Metric
filters: List[ProjectedChargeFilterUsage]
grouped_usage: Optional[List[ProjectedGroupedUsage]]
pricing_unit_details: Optional[PricingUnitDetails]


class CustomerProjectedUsageResponse(BaseResponseModel):
from_datetime: str
to_datetime: str
issuing_date: str
invoice_id: Optional[str]
currency: str
amount_cents: int
projected_amount_cents: int
total_amount_cents: int
taxes_amount_cents: int
charges_usage: List[ProjectedChargeUsage]
7 changes: 0 additions & 7 deletions lago_python_client/models/customer_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class ChargeFilterUsage(BaseModel):
invoice_display_name: Optional[str]
values: Dict[str, List[str]]
units: str
projected_units: str
amount_cents: int
projected_amount_cents: int
events_count: int
pricing_unit_details: Optional[PricingUnitDetails]

Expand All @@ -37,21 +35,17 @@ class ChargeObject(BaseModel):

class GroupedUsage(BaseModel):
amount_cents: int
projected_amount_cents: int
events_count: int
units: str
projected_units: str
grouped_by: Dict[str, str]
filters: List[ChargeFilterUsage]
pricing_unit_details: Optional[PricingUnitDetails]


class ChargeUsage(BaseModel):
units: str
projected_units: str
events_count: int
amount_cents: int
projected_amount_cents: int
amount_currency: str
charge: ChargeObject
billable_metric: Metric
Expand All @@ -67,7 +61,6 @@ class CustomerUsageResponse(BaseResponseModel):
invoice_id: Optional[str]
currency: str
amount_cents: int
projected_amount_cents: int
total_amount_cents: int
taxes_amount_cents: int
charges_usage: List[ChargeUsage]
5 changes: 0 additions & 5 deletions tests/fixtures/customer_past_usage.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
"lago_invoice_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"currency": "EUR",
"amount_cents": 123,
"projected_amount_cents": 0,
"total_amount_cents": 123,
"taxes_amount_cents": 0,
"charges_usage": [
{
"units": "1.0",
"projected_units": "0.0",
"events_count": 1,
"amount_cents": 123,
"projected_amount_cents": 0,
"amount_currency": "EUR",
"charge": {
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
Expand All @@ -36,10 +33,8 @@
"filters": [
{
"units": "3.0",
"projected_units": "0.0",
"events_count": 1,
"amount_cents": 123,
"projected_amount_cents": 0,
"values": {
"country": [
"france"
Expand Down
90 changes: 90 additions & 0 deletions tests/fixtures/customer_projected_usage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"customer_projected_usage": {
"from_datetime": "2022-07-01T00:00:00Z",
"to_datetime": "2022-07-31T23:59:59Z",
"issuing_date": "2022-08-01",
"invoice_id": null,
"currency": "EUR",
"amount_cents": 123,
"projected_amount_cents": 256,
"total_amount_cents": 123,
"taxes_amount_cents": 0,
"charges_usage": [
{
"units": "1.0",
"projected_units": "2.0",
"events_count": 1,
"amount_cents": 123,
"projected_amount_cents": 256,
"amount_currency": "EUR",
"charge": {
"lago_id": "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba",
"charge_model": "graduated",
"invoice_display_name": "add_on_invoice_display_name"
},
"billable_metric": {
"lago_id": "99a6094e-199b-4101-896a-54e927ce7bd7",
"name": "Usage metric",
"code": "usage_metric",
"aggregation_type": "sum"
},
"pricing_unit_details": {
"amount_cents": 123,
"short_name": "CR",
"conversion_rate": 1.0
},
"filters": [
{
"units": "3.0",
"projected_units": "4.0",
"events_count": 1,
"amount_cents": 123,
"projected_amount_cents": 256,
"values": {
"country": [
"france"
]
},
"pricing_unit_details": {
"amount_cents": 123,
"short_name": "CR",
"conversion_rate": 1.0
}
}
],
"grouped_usage": [
{
"amount_cents": 123,
"projected_amount_cents": 256,
"events_count": 1,
"units": "3.0",
"projected_units": "4.0",
"grouped_by": {
"agent_name": "aragorn"
},
"pricing_unit_details": {
"amount_cents": 456,
"short_name": "CR",
"conversion_rate": 1.2
},
"filters": [
{
"units": "3.0",
"projected_units": "4.0",
"events_count": 1,
"amount_cents": 123,
"projected_amount_cents": 256,
"values": {
"country": [
"france"
]
},
"pricing_unit_details": null
}
]
}
]
}
]
}
}
9 changes: 0 additions & 9 deletions tests/fixtures/customer_usage.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
"invoice_id": null,
"currency": "EUR",
"amount_cents": 123,
"projected_amount_cents": 256,
"total_amount_cents": 123,
"taxes_amount_cents": 0,
"charges_usage": [
{
"units": "1.0",
"projected_units": "2.0",
"events_count": 1,
"amount_cents": 123,
"projected_amount_cents": 256,
"amount_currency": "EUR",
"charge": {
"lago_id": "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba",
Expand All @@ -36,10 +33,8 @@
"filters": [
{
"units": "3.0",
"projected_units": "4.0",
"events_count": 1,
"amount_cents": 123,
"projected_amount_cents": 256,
"values": {
"country": [
"france"
Expand All @@ -55,10 +50,8 @@
"grouped_usage": [
{
"amount_cents": 123,
"projected_amount_cents": 256,
"events_count": 1,
"units": "3.0",
"projected_units": "4.0",
"grouped_by": {
"agent_name": "aragorn"
},
Expand All @@ -70,10 +63,8 @@
"filters": [
{
"units": "3.0",
"projected_units": "4.0",
"events_count": 1,
"amount_cents": 123,
"projected_amount_cents": 256,
"values": {
"country": [
"france"
Expand Down
23 changes: 23 additions & 0 deletions tests/test_customer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ def test_valid_current_usage(httpx_mock: HTTPXMock):
assert response.charges_usage[0].filters[0].values["country"] == ["france"]


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

httpx_mock.add_response(
method="GET",
url="https://api.getlago.com/api/v1/customers/external_customer_id/projected_usage?external_subscription_id=123",
content=mock_response("customer_projected_usage"),
)
response = client.customers.projected_usage("external_customer_id", "123")

assert response.from_datetime == "2022-07-01T00:00:00Z"
assert len(response.charges_usage) == 1
assert response.charges_usage[0].units == "1.0"
assert response.charges_usage[0].projected_units == "2.0"
assert response.charges_usage[0].amount_cents == 123
assert response.charges_usage[0].projected_amount_cents == 256
assert response.charges_usage[0].amount_currency == "EUR"
assert response.charges_usage[0].charge.lago_id == "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba"
assert response.charges_usage[0].charge.charge_model == "graduated"
assert response.charges_usage[0].charge.invoice_display_name == "add_on_invoice_display_name"
assert response.charges_usage[0].billable_metric.lago_id == "99a6094e-199b-4101-896a-54e927ce7bd7"


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

Expand Down