diff --git a/lago_python_client/client.py b/lago_python_client/client.py index e399f876..f3ffe93c 100644 --- a/lago_python_client/client.py +++ b/lago_python_client/client.py @@ -1,3 +1,4 @@ +from lago_python_client.clients.applied_coupon_client import AppliedCouponClient from lago_python_client.clients.subscription_client import SubscriptionClient from lago_python_client.clients.customer_client import CustomerClient from lago_python_client.clients.event_client import EventClient @@ -28,5 +29,8 @@ def subscriptions(self): def customers(self): return CustomerClient(self.base_api_url(), self.api_key) + def applied_coupons(self): + return AppliedCouponClient(self.base_api_url(), self.api_key) + def webhooks(self): return WebhookClient(self.base_api_url(), self.api_key) diff --git a/lago_python_client/clients/__init__.py b/lago_python_client/clients/__init__.py index 0e9d7c3c..b90e65ff 100644 --- a/lago_python_client/clients/__init__.py +++ b/lago_python_client/clients/__init__.py @@ -1,3 +1,4 @@ +from lago_python_client.clients.applied_coupon_client import AppliedCouponClient from lago_python_client.clients.event_client import EventClient from lago_python_client.clients.subscription_client import SubscriptionClient from lago_python_client.clients.customer_client import CustomerClient diff --git a/lago_python_client/clients/applied_coupon_client.py b/lago_python_client/clients/applied_coupon_client.py new file mode 100644 index 00000000..1b76e125 --- /dev/null +++ b/lago_python_client/clients/applied_coupon_client.py @@ -0,0 +1,13 @@ +from .base_client import BaseClient +from lago_python_client.models.applied_coupon import AppliedCouponResponse +from typing import Dict + +class AppliedCouponClient(BaseClient): + def api_resource(self): + return 'applied_coupons' + + def root_name(self): + return 'applied_coupon' + + def prepare_response(self, data: Dict): + return AppliedCouponResponse.parse_obj(data) diff --git a/lago_python_client/clients/base_client.py b/lago_python_client/clients/base_client.py index 2bef88b0..3a79a3e6 100644 --- a/lago_python_client/clients/base_client.py +++ b/lago_python_client/clients/base_client.py @@ -38,7 +38,11 @@ def delete(self, params: Dict): def headers(self): bearer = "Bearer " + self.api_key - headers = {'Content-type': 'application/json', 'Authorization': bearer} + headers = { + 'Content-type': 'application/json', + 'Authorization': bearer, + 'User-agent': 'Lago Python v0.1.1' + } return headers diff --git a/lago_python_client/models/__init__.py b/lago_python_client/models/__init__.py index 82264be4..99ada6f0 100644 --- a/lago_python_client/models/__init__.py +++ b/lago_python_client/models/__init__.py @@ -1,3 +1,4 @@ +from lago_python_client.models.applied_coupon import AppliedCoupon from lago_python_client.models.event import Event from lago_python_client.models.customer import Customer from lago_python_client.models.subscription import Subscription diff --git a/lago_python_client/models/applied_coupon.py b/lago_python_client/models/applied_coupon.py new file mode 100644 index 00000000..dca8108f --- /dev/null +++ b/lago_python_client/models/applied_coupon.py @@ -0,0 +1,19 @@ +from pydantic import BaseModel, Field +from typing import Optional + +class AppliedCoupon(BaseModel): + customer_id: str + coupon_code: str + amount_cents: Optional[int] + amount_currency: Optional[str] + +class AppliedCouponResponse(BaseModel): + lago_id: str + lago_coupon_id: str + customer_id: str + lago_customer_id: str + amount_cents: str + amount_currency: str + expiration_date: Optional[str] + created_at: str + terminated_at: Optional[str] diff --git a/setup.cfg b/setup.cfg index f67c7e89..82131b2d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = lago-python-client -version = 0.1.0 +version = 0.1.1 author = Lovro Colic author_email = lovro@getlago.com description = Lago Python API Client diff --git a/tests/__init__.py b/tests/__init__.py index 47816c71..c19601c4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,5 +1,6 @@ from tests.test_event_client import TestEventClient from tests.test_subscription_client import TestSubscriptionClient from tests.test_customer_client import TestCustomerClient +from tests.test_applied_coupon_client import TestAppliedCouponClient from tests.test_webhook_client import TestWebhookClient from tests.test_client import TestClient diff --git a/tests/fixtures/applied_coupon.json b/tests/fixtures/applied_coupon.json new file mode 100644 index 00000000..0ea9c86d --- /dev/null +++ b/tests/fixtures/applied_coupon.json @@ -0,0 +1,13 @@ +{ + "applied_coupon": { + "lago_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "lago_coupon_id": "b7ab2926-1de8-4428-9bcd-779314ac129b", + "customer_id": "5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba", + "lago_customer_id": "99a6094e-199b-4101-896a-54e927ce7bd7", + "amount_cents": 123, + "amount_currency": "EUR", + "expiration_date": "2022-04-29", + "created_at": "2022-04-29T08:59:51Z", + "terminated_at": "2022-04-29T08:59:51Z" + } +} diff --git a/tests/test_applied_coupon_client.py b/tests/test_applied_coupon_client.py new file mode 100644 index 00000000..31e54303 --- /dev/null +++ b/tests/test_applied_coupon_client.py @@ -0,0 +1,44 @@ +import unittest +import requests_mock +import os + +from lago_python_client.client import Client +from lago_python_client.models import AppliedCoupon +from lago_python_client.clients.base_client import LagoApiError + +def create_applied_coupon(): + return AppliedCoupon( + customer_id='5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba', + coupon_code='Free-Lemon-Juice' + ) + +def mock_response(): + this_dir = os.path.dirname(os.path.abspath(__file__)) + data_path = os.path.join(this_dir, 'fixtures/applied_coupon.json') + + with open(data_path, 'r') as applied_coupon_response: + return applied_coupon_response.read() + + +class TestAppliedCouponClient(unittest.TestCase): + def test_valid_create_applied_coupon_request(self): + client = Client(api_key='886fe239-927d-4072-ab72-6dd345e8dd0d') + + with requests_mock.Mocker() as m: + m.register_uri('POST', 'https://api.getlago.com/api/v1/applied_coupons', text=mock_response()) + response = client.applied_coupons().create(create_applied_coupon()) + + self.assertEqual(response.customer_id, '5eb02857-a71e-4ea2-bcf9-57d3a41bc6ba') + + def test_invalid_create_applied_coupon_request(self): + client = Client(api_key='invalid') + + with requests_mock.Mocker() as m: + m.register_uri('POST', 'https://api.getlago.com/api/v1/applied_coupons', status_code=401, text='') + + with self.assertRaises(LagoApiError): + client.applied_coupons().create(create_applied_coupon()) + + +if __name__ == '__main__': + unittest.main()