Skip to content

Commit

Permalink
feat(analytics): Add analytics resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Nov 15, 2023
1 parent ec9de82 commit 2382c4f
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lago_python_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from .events.clients import EventClient
from .fees.clients import FeeClient
from .functools_ext import callable_cached_property
from .gross_revenues.clients import GrossRevenueClient
from .invoices.clients import InvoiceClient
from .mrrs.clients import MrrClient
from .organizations.clients import OrganizationClient
from .plans.clients import PlanClient
from .subscriptions.clients import SubscriptionClient
Expand Down Expand Up @@ -71,10 +73,18 @@ def events(self) -> EventClient:
def fees(self) -> FeeClient:
return FeeClient(self.base_api_url, self.api_key)

@callable_cached_property
def gross_revenues(self) -> GrossRevenueClient:
return GrossRevenueClient(self.base_api_url, self.api_key)

@callable_cached_property
def invoices(self) -> InvoiceClient:
return InvoiceClient(self.base_api_url, self.api_key)

@callable_cached_property
def mrrs(self) -> MrrClient:
return MrrClient(self.base_api_url, self.api_key)

@callable_cached_property
def organizations(self) -> OrganizationClient:
return OrganizationClient(self.base_api_url, self.api_key)
Expand Down
Empty file.
39 changes: 39 additions & 0 deletions lago_python_client/gross_revenues/clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
from typing import Any, ClassVar, Type, Union

from ..base_client import BaseClient
from ..mixins import FindAllCommandMixin
from ..models.gross_revenue import GrossRevenueResponse
from ..services.request import make_headers, make_url, send_get_request
from ..services.response import get_response_data, prepare_index_response, Response

if sys.version_info >= (3, 9):
from collections.abc import Mapping
else:
from typing import Mapping


class GrossRevenueClient(
FindAllCommandMixin[GrossRevenueResponse],
BaseClient,
):
API_RESOURCE: ClassVar[str] = 'gross_revenues'
RESPONSE_MODEL: ClassVar[Type[GrossRevenueResponse]] = GrossRevenueResponse
ROOT_NAME: ClassVar[str] = 'gross_revenue'

def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]:
api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
path_parts=('analytics', 'gross_revenue'),
query_pairs=options,
),
headers=make_headers(api_key=self.api_key),
)

# Process response data
return prepare_index_response(
api_resource=self.API_RESOURCE,
response_model=self.RESPONSE_MODEL,
data=get_response_data(response=api_response),
)
13 changes: 13 additions & 0 deletions lago_python_client/models/gross_revenue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import List, Optional

from ..base_model import BaseModel, BaseResponseModel


class GrossRevenueResponse(BaseResponseModel):
amount_cents: Optional[int]
currency: Optional[str]
month: str


class GrossRevenuesResponse(BaseResponseModel):
__root__: List[GrossRevenueResponse]
13 changes: 13 additions & 0 deletions lago_python_client/models/mrr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import List, Optional

from ..base_model import BaseModel, BaseResponseModel


class MrrResponse(BaseResponseModel):
amount_cents: Optional[int]
currency: Optional[str]
month: str


class MrrsResponse(BaseResponseModel):
__root__: List[MrrResponse]
Empty file.
39 changes: 39 additions & 0 deletions lago_python_client/mrrs/clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
from typing import Any, ClassVar, Type, Union

from ..base_client import BaseClient
from ..mixins import FindAllCommandMixin
from ..models.mrr import MrrResponse
from ..services.request import make_headers, make_url, send_get_request
from ..services.response import get_response_data, prepare_index_response, Response

if sys.version_info >= (3, 9):
from collections.abc import Mapping
else:
from typing import Mapping


class MrrClient(
FindAllCommandMixin[MrrResponse],
BaseClient,
):
API_RESOURCE: ClassVar[str] = 'mrrs'
RESPONSE_MODEL: ClassVar[Type[MrrResponse]] = MrrResponse
ROOT_NAME: ClassVar[str] = 'mrr'

def find_all(self, options: Mapping[str, Union[int, str]] = {}) -> Mapping[str, Any]:
api_response: Response = send_get_request(
url=make_url(
origin=self.base_url,
path_parts=('analytics', 'mrr'),
query_pairs=options,
),
headers=make_headers(api_key=self.api_key),
)

# Process response data
return prepare_index_response(
api_resource=self.API_RESOURCE,
response_model=self.RESPONSE_MODEL,
data=get_response_data(response=api_response),
)
15 changes: 15 additions & 0 deletions tests/fixtures/gross_revenue_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"gross_revenues": [
{
"month": "2023-11-01T00:00:00.000Z",
"amount_cents": 100,
"currency": "EUR"
},
{
"month": "2023-12-01T00:00:00.000Z",
"amount_cents": 200,
"currency": "USD"
}
],
"meta": {}
}
15 changes: 15 additions & 0 deletions tests/fixtures/mrr_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"mrrs": [
{
"month": "2023-11-01T00:00:00.000Z",
"amount_cents": 100,
"currency": "EUR"
},
{
"month": "2023-12-01T00:00:00.000Z",
"amount_cents": 200,
"currency": "USD"
}
],
"meta": {}
}
27 changes: 27 additions & 0 deletions tests/test_gross_revenue_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

import pytest
from pytest_httpx import HTTPXMock

from lago_python_client.client import Client
from lago_python_client.exceptions import LagoApiError
from lago_python_client.models.gross_revenue import GrossRevenueResponse


def mock_collection_response():
current_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(current_dir, 'fixtures/gross_revenue_index.json')

with open(data_path, 'rb') as gross_revenues_response:
return gross_revenues_response.read()


def test_valid_find_all_gross_revenues_request(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/analytics/gross_revenue', content=mock_collection_response())
response = client.gross_revenues.find_all()

assert response['gross_revenues'][0].currency == 'EUR'
assert response['gross_revenues'][0].amount_cents == 100
assert response['gross_revenues'][0].month == '2023-11-01T00:00:00.000Z'
27 changes: 27 additions & 0 deletions tests/test_mrr_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

import pytest
from pytest_httpx import HTTPXMock

from lago_python_client.client import Client
from lago_python_client.exceptions import LagoApiError
from lago_python_client.models.mrr import MrrResponse


def mock_collection_response():
current_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(current_dir, 'fixtures/mrr_index.json')

with open(data_path, 'rb') as mrrs_response:
return mrrs_response.read()


def test_valid_find_all_mrrs_request(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/analytics/mrr', content=mock_collection_response())
response = client.mrrs.find_all()

assert response['mrrs'][0].currency == 'EUR'
assert response['mrrs'][0].amount_cents == 100
assert response['mrrs'][0].month == '2023-11-01T00:00:00.000Z'

0 comments on commit 2382c4f

Please sign in to comment.