Skip to content

Commit 34681f1

Browse files
feat(event): Add support for ingest API
1 parent 80c7652 commit 34681f1

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

lago_python_client/base_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
class BaseClient(ABC):
88
"""The base class used for each collection client."""
99

10-
def __init__(self, base_url: str, api_key: str):
10+
def __init__(self, base_url: str, api_key: str, base_ingest_url: str = ""):
1111
self.base_url = base_url
1212
self.api_key = api_key
13+
self.base_ingest_url = base_ingest_url
1314

1415
@property
1516
@classmethod

lago_python_client/client.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,32 @@
3131

3232
class Client:
3333
BASE_URL: Final[str] = "https://api.getlago.com/"
34+
BASE_INGEST_URL: Final[str] = "https://ingest.getlago.com/"
3435
API_PATH: Final[str] = "api/v1/"
3536

36-
def __init__(self, api_key: str = "", api_url: str = "") -> None:
37+
def __init__(self, api_key: str = "", api_url: str = "", use_ingest_service = False, ingest_api_url: str = "") -> None:
3738
self.api_key: str = api_key
3839
self.api_url: str = api_url
40+
self.use_ingest_service: bool = use_ingest_service
41+
self.ingest_api_url: str = ingest_api_url
3942

4043
@property
4144
def base_api_url(self) -> str:
4245
return urljoin(self.api_url if self.api_url else Client.BASE_URL, Client.API_PATH)
4346

47+
@property
48+
def base_ingest_api_url(self) -> str:
49+
if not self.use_ingest_service:
50+
return self.base_api_url
51+
52+
if self.ingest_api_url == "":
53+
ingest_url = Client.BASE_INGEST_URL
54+
else:
55+
ingest_url = self.ingest_api_url
56+
57+
return urljoin(ingest_url, Client.API_PATH)
58+
59+
4460
@callable_cached_property
4561
def add_ons(self) -> AddOnClient:
4662
return AddOnClient(self.base_api_url, self.api_key)
@@ -67,7 +83,7 @@ def customers(self) -> CustomerClient:
6783

6884
@callable_cached_property
6985
def events(self) -> EventClient:
70-
return EventClient(self.base_api_url, self.api_key)
86+
return EventClient(self.base_api_url, self.api_key, self.base_ingest_api_url)
7187

7288
@callable_cached_property
7389
def fees(self) -> FeeClient:

lago_python_client/events/clients.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import sys
2-
from typing import Any, ClassVar, Type
2+
from typing import Any, ClassVar, Optional, Type
3+
4+
import httpx
35

46
from lago_python_client.base_model import BaseModel
57

68
from ..base_client import BaseClient
79
from ..fees.clients import FeeClient
8-
from ..mixins import CreateCommandMixin, FindCommandMixin
10+
from ..mixins import FindCommandMixin
911
from ..models.event import EventResponse
1012
from ..models.fee import FeeResponse
1113
from ..services.json import to_json
1214
from ..services.request import make_headers, make_url, send_post_request
1315
from ..services.response import (
1416
get_response_data,
17+
prepare_object_response,
1518
prepare_object_list_response,
1619
verify_response,
1720
Response,
@@ -23,11 +26,34 @@
2326
from typing import Mapping
2427

2528

26-
class EventClient(CreateCommandMixin[EventResponse], FindCommandMixin[EventResponse], BaseClient):
29+
class EventClient(FindCommandMixin[EventResponse], BaseClient):
2730
API_RESOURCE: ClassVar[str] = "events"
2831
RESPONSE_MODEL: ClassVar[Type[EventResponse]] = EventResponse
2932
ROOT_NAME: ClassVar[str] = "event"
3033

34+
def create(self, input_object: BaseModel, timeout: Optional[httpx.Timeout] = None) -> Optional[EventResponse]:
35+
api_response: Response = send_post_request(
36+
url=make_url(
37+
origin=self.base_ingest_url,
38+
path_parts=(self.API_RESOURCE,),
39+
),
40+
content=to_json({
41+
self.ROOT_NAME: input_object.dict(),
42+
}),
43+
headers=make_headers(api_key=self.api_key),
44+
timeout=timeout,
45+
)
46+
47+
# Process response data
48+
response_data = get_response_data(response=api_response, key=self.ROOT_NAME)
49+
if not response_data:
50+
return None
51+
52+
return prepare_object_response(
53+
response_model=self.RESPONSE_MODEL,
54+
data=response_data,
55+
)
56+
3157
def batch_create(self, input_object: BaseModel) -> None:
3258
api_response: Response = send_post_request(
3359
url=make_url(

tests/test_event_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def test_invalid_create_events_request(httpx_mock: HTTPXMock):
8080
with pytest.raises(LagoApiError):
8181
client.events.create(create_event())
8282

83+
def test_valid_create_event_request_with_custom_ingest_url(httpx_mock: HTTPXMock):
84+
client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d', use_ingest_service=True, ingest_api_url='https://custom-ingest.getlago.com')
85+
86+
httpx_mock.add_response(method='POST', url='https://custom-ingest.getlago.com/api/v1/events', content=b'')
87+
client.events.create(create_event()) # Any response means success, any exception - failure
88+
8389

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

0 commit comments

Comments
 (0)