|
1 | 1 | import logging |
| 2 | +from enum import Enum |
2 | 3 | from typing import Dict, Union, List, Iterable |
3 | 4 |
|
4 | 5 | from threescale_api import auth |
@@ -778,6 +779,71 @@ def clear(self): |
778 | 779 | return self.update(params=params) |
779 | 780 |
|
780 | 781 |
|
| 782 | +class LineItems(DefaultClient): |
| 783 | + """Default client for LineItems""" |
| 784 | + def __init__(self, *args, entity_name='line_item', entity_collection='line_items', **kwargs): |
| 785 | + super().__init__(*args, entity_name=entity_name, |
| 786 | + entity_collection=entity_collection, **kwargs) |
| 787 | + |
| 788 | + @property |
| 789 | + def url(self) -> str: |
| 790 | + return self.parent.url + '/line_items' |
| 791 | + |
| 792 | + |
| 793 | +class InvoiceState(Enum): |
| 794 | + cancelled, failed, paid, unpaid, pending, finalized = range(6) |
| 795 | + |
| 796 | + |
| 797 | +class Invoices(DefaultClient): |
| 798 | + """Default client for Invoices""" |
| 799 | + def __init__(self, *args, entity_name='invoice', entity_collection='invoices', **kwargs): |
| 800 | + super().__init__(*args, entity_name=entity_name, |
| 801 | + entity_collection=entity_collection, **kwargs) |
| 802 | + |
| 803 | + @property |
| 804 | + def url(self) -> str: |
| 805 | + return self.threescale_client.url + '/api/invoices' |
| 806 | + |
| 807 | + @property |
| 808 | + def line_items(self) -> LineItems: |
| 809 | + return LineItems(parent=self, instance_klass=LineItem) |
| 810 | + |
| 811 | + def list_by_account(self, account: Union['Account', int], **kwargs): |
| 812 | + account_id = _extract_entity_id(account) |
| 813 | + url = self.threescale_client.url + f"/api/accounts/{account_id}/invoices" |
| 814 | + response = self.rest.get(url, **kwargs) |
| 815 | + instance = self._create_instance(response=response, collection=True) |
| 816 | + return instance |
| 817 | + |
| 818 | + def read_by_account(self, entity_id: int, account: Union['Account', int], **kwargs): |
| 819 | + account_id = _extract_entity_id(account) |
| 820 | + url = self.threescale_client.url + f"/api/accounts/{account_id}/invoices/{entity_id}" |
| 821 | + response = self.rest.get(url, **kwargs) |
| 822 | + instance = self._create_instance(response=response) |
| 823 | + return instance |
| 824 | + |
| 825 | + def state_update(self, entity_id: int, state: InvoiceState, **kwargs): |
| 826 | + """ |
| 827 | + Update the state of the Invoice. |
| 828 | + Values allowed (depend on the previous state): |
| 829 | + cancelled, failed, paid, unpaid, pending, finalized |
| 830 | + """ |
| 831 | + log.info(f"[Invoice] state changed for invoice ({entity_id}): {state}") |
| 832 | + params = dict(state=state.name) |
| 833 | + url = self._entity_url(entity_id) + '/state' |
| 834 | + response = self.rest.put(url=url, json=params, **kwargs) |
| 835 | + instance = self._create_instance(response=response) |
| 836 | + return instance |
| 837 | + |
| 838 | + def charge(self, entity_id: int): |
| 839 | + """Charge an Invoice.""" |
| 840 | + log.info(f"[Invoice] charge invoice ({entity_id})") |
| 841 | + url = self._entity_url(entity_id) + '/state' |
| 842 | + response = self.rest.post(url) |
| 843 | + instance = self._create_instance(response=response) |
| 844 | + return instance |
| 845 | + |
| 846 | + |
781 | 847 | # Resources |
782 | 848 |
|
783 | 849 |
|
@@ -1242,3 +1308,23 @@ def unsuspend(self): |
1242 | 1308 | def activate(self): |
1243 | 1309 | log.info("Changes the state of the user of the provider account to active") |
1244 | 1310 | return self.set_state(state='activate') |
| 1311 | + |
| 1312 | + |
| 1313 | +class LineItem(DefaultResource): |
| 1314 | + def __init__(self, entity_name='name', **kwargs): |
| 1315 | + super().__init__(entity_name=entity_name, **kwargs) |
| 1316 | + |
| 1317 | + |
| 1318 | +class Invoice(DefaultResource): |
| 1319 | + def __init__(self, entity_name='friendly_id', **kwargs): |
| 1320 | + super().__init__(entity_name=entity_name, **kwargs) |
| 1321 | + |
| 1322 | + @property |
| 1323 | + def line_items(self) -> LineItems: |
| 1324 | + return LineItems(parent=self, instance_klass=LineItem) |
| 1325 | + |
| 1326 | + def state_update(self, state: InvoiceState, **kwargs): |
| 1327 | + return self.client.state_update(entity_id=self.entity_id, state=state, **kwargs) |
| 1328 | + |
| 1329 | + def charge(self, **kwargs): |
| 1330 | + return self.client.charge(entity_id=self.entity_id, **kwargs) |
0 commit comments