Skip to content
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

feat: Add route to find an event by it's transaction id #13

Merged
merged 2 commits into from
Jun 28, 2022
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ event = Event(
client.events().create(event)
```

``` python
transaction_id = "6afadc2a-f28c-40a4-a868-35636f229765"
event = client.events().find(transaction_id)
```

### Customers
[Api reference](https://doc.getlago.com/docs/api/customers/customer-object)

Expand Down
13 changes: 13 additions & 0 deletions lago_python_client/clients/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ def __init__(self, base_url: str, api_key: str):
self.base_url = base_url
self.api_key = api_key

def find(self, resource_id: str, params: Dict=None):
api_resource = self.api_resource() + '/' + resource_id
query_url = urljoin(self.base_url, api_resource)

data=None
if params is not None:
data = json.dumps(params)

api_response = requests.get(query_url, data=data, headers=self.headers())
data = self.handle_response(api_response).json().get(self.root_name())

return self.prepare_response(data)

def create(self, input_object: BaseModel):
query_url = urljoin(self.base_url, self.api_resource())
query_parameters = {
Expand Down
5 changes: 5 additions & 0 deletions lago_python_client/clients/event_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from .base_client import BaseClient
from lago_python_client.models.event import EventResponse
from typing import Dict


class EventClient(BaseClient):
Expand All @@ -7,3 +9,6 @@ def api_resource(self):

def root_name(self):
return 'event'

def prepare_response(self, data: Dict):
return EventResponse.parse_obj(data)
9 changes: 9 additions & 0 deletions lago_python_client/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ class Event(BaseModel):
code: str
timestamp: Optional[int]
properties: Optional[dict]

class EventResponse(BaseModel):
lago_id: str
transaction_id: str
customer_id: str
code: str
timestamp: str
properties: Optional[dict]
created_at: str
13 changes: 13 additions & 0 deletions tests/fixtures/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"event": {
"lago_id": "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba",
"transaction_id": "1a2d9c8d-5875-4688-9854-5ccfd414bc5e",
"customer_id": "24691b9a-7330-409d-9dfd-d80582593297",
"code": "test",
"timestamp": "2022-04-29T08:59:51Z",
"properties": {
"custom_field": "custom"
},
"created_at": "2022-04-29T08:59:51Z"
}
}
27 changes: 27 additions & 0 deletions tests/test_event_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
import requests_mock
import os

from lago_python_client.client import Client
from lago_python_client.models import Event
Expand All @@ -9,6 +10,12 @@
def create_event():
return Event(customer_id='5eb02857-a71e-4ea2-bcf9-57d8885990ba', code='123', transaction_id='123')

def mock_response():
this_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(this_dir, 'fixtures/event.json')

with open(data_path, 'r') as event_response:
return event_response.read()

class TestEventClient(unittest.TestCase):
def test_valid_create_events_request(self):
Expand All @@ -29,6 +36,26 @@ def test_invalid_create_events_request(self):
with self.assertRaises(LagoApiError):
client.events().create(create_event())

def test_valid_find_event_request(self):
client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d')
event_id = '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba'

with requests_mock.Mocker() as m:
m.register_uri('GET', 'https://api.getlago.com/api/v1/events/' + event_id, text=mock_response())
response = client.events().find(event_id)

self.assertEqual(response.lago_id, event_id)

def test_invalid_find_events_request(self):
client = Client(api_key='invalid')
event_id = 'INVALID_EVENT'

with requests_mock.Mocker() as m:
m.register_uri('POST', 'https://api.getlago.com/api/v1/events', status_code=401, text='')
m.register_uri('GET', 'https://api.getlago.com/api/v1/events/' + event_id, status_code=404, text='')

with self.assertRaises(LagoApiError):
client.events().find(event_id)

if __name__ == '__main__':
unittest.main()