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 API client to assign a coupon to a customer #3

Merged
merged 1 commit into from
May 25, 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
4 changes: 4 additions & 0 deletions lago_python_client/client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions lago_python_client/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 13 additions & 0 deletions lago_python_client/clients/applied_coupon_client.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 5 additions & 1 deletion lago_python_client/clients/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lago_python_client/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lago_python_client/models/applied_coupon.py
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions tests/fixtures/applied_coupon.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
44 changes: 44 additions & 0 deletions tests/test_applied_coupon_client.py
Original file line number Diff line number Diff line change
@@ -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()