Skip to content

Commit c8694de

Browse files
authored
Merge pull request #8 from roaldnefs/add-invoice-service
Add InvoiceService to retrieve and list invoices
2 parents 84774be + f31806a commit c8694de

7 files changed

Lines changed: 160 additions & 0 deletions

File tree

docs/user/quickstart.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ object by its name::
9191

9292
>>> client.domains.delete('transipdemonstratie.nl')
9393

94+
Invoices
95+
--------
96+
97+
Using the
98+
:class:`InvoiceService <transip.v6.services.InvoiceService>`
99+
service we can retrieve all invoices in your TransIP account in the form of a
100+
:class:`Invoice <transip.v6.objects.Invoice>` object::
101+
102+
>>> invoices = client.invoices.list()
103+
>>> for invoice in invoices:
104+
... print(invoice)
105+
<class 'transip.v6.objects.invoice.Invoice'> => {'invoiceNumber': 'F0000.1911.0000.0004', 'creationDate': '2020-01-01', 'payDate': '2020-01-01', 'dueDate': '2020-02-01', 'invoiceStatus': 'waitsforpayment', 'currency': 'EUR', 'totalAmount': 1000, 'totalAmountInclVat': 1240}
106+
107+
We could also retrieve a single :class:`Invoice <transip.v6.objects.Invoice>`
108+
object by its invoice number::
109+
110+
>>> invoice = client.invoices.get('F0000.1911.0000.0004')
111+
>>> print(f"{invoice.invoiceNumber} has status '{invoice.invoiceStatus}'")
112+
F0000.1911.0000.0004 has status 'waitsforpayment'
113+
94114
VPSs
95115
----
96116

tests/services/test_invoices.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2020 Roald Nefs <info@roaldnefs.com>
4+
#
5+
# This file is part of python-transip.
6+
7+
# python-transip is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
12+
# python-transip is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
19+
20+
from typing import Type, List
21+
import responses # type: ignore
22+
23+
from transip import TransIP
24+
from transip.v6.objects import Invoice
25+
26+
27+
@responses.activate
28+
def test_invoices_list(transip_minimal_client: Type[TransIP]) -> None:
29+
responses.add(
30+
responses.GET,
31+
"https://api.transip.nl/v6/invoices",
32+
json={
33+
"invoices": [
34+
{
35+
"invoiceNumber": "F0000.1911.0000.0004",
36+
"creationDate": "2020-01-01",
37+
"payDate": "2020-01-01",
38+
"dueDate": "2020-02-01",
39+
"invoiceStatus": "waitsforpayment",
40+
"currency": "EUR",
41+
"totalAmount": 1000,
42+
"totalAmountInclVat": 1240
43+
}
44+
]
45+
},
46+
status=200,
47+
)
48+
49+
invoices: List[Type[Invoice]] = transip_minimal_client.invoices.list()
50+
invoice: Type[Invoice] = invoices[0]
51+
assert len(invoices) == 1
52+
assert invoice.get_id() == "F0000.1911.0000.0004" # type: ignore
53+
54+
55+
@responses.activate
56+
def test_invoices_get(transip_minimal_client: Type[TransIP]) -> None:
57+
responses.add(
58+
responses.GET,
59+
"https://api.transip.nl/v6/invoices/F0000.1911.0000.0004",
60+
json={
61+
"invoice": {
62+
"invoiceNumber": "F0000.1911.0000.0004",
63+
"creationDate": "2020-01-01",
64+
"payDate": "2020-01-01",
65+
"dueDate": "2020-02-01",
66+
"invoiceStatus": "waitsforpayment",
67+
"currency": "EUR",
68+
"totalAmount": 1000,
69+
"totalAmountInclVat": 1240
70+
}
71+
},
72+
status=200,
73+
)
74+
75+
invoice_id: str = "F0000.1911.0000.0004"
76+
invoice: Type[Invoice] = transip_minimal_client.invoices.get(invoice_id)
77+
assert invoice.get_id() == "F0000.1911.0000.0004" # type: ignore

transip/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def __init__(
6868
services.AvailabilityZoneService(self) # type: ignore
6969
)
7070
self.domains: Type[Any] = services.DomainService(self) # type: ignore
71+
self.invoices: Type[Any] = (
72+
services.InvoiceService(self) # type: ignore
73+
)
7174
self.ssh_keys: Type[Any] = services.SshKeyService(self) # type: ignore
7275
self.vpss: Type[Any] = services.VpsService(self) # type: ignore
7376

transip/v6/objects/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
1919

2020
from transip.v6.objects.availability_zone import AvailabilityZone # noqa: 401
21+
from transip.v6.objects.invoice import Invoice # noqa: 401
2122
from transip.v6.objects.domain import Domain # noqa: 401
2223
from transip.v6.objects.ssh_key import SshKey # noqa: 401
2324
from transip.v6.objects.vps import Vps # noqa: 401

transip/v6/objects/invoice.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2020 Roald Nefs <info@roaldnefs.com>
4+
#
5+
# This file is part of python-transip.
6+
7+
# python-transip is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
12+
# python-transip is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
19+
20+
from transip.base import ApiObject
21+
22+
23+
class Invoice(ApiObject):
24+
25+
_id_attr: str = "invoiceNumber"

transip/v6/services/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919

2020
from transip.v6.services.availability_zone import AvailabilityZoneService # noqa: 401
2121
from transip.v6.services.domain import DomainService # noqa: 401
22+
from transip.v6.services.invoice import InvoiceService # noqa: 401
2223
from transip.v6.services.ssh_key import SshKeyService # noqa: 401
2324
from transip.v6.services.vps import VpsService # noqa: 401

transip/v6/services/invoice.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2020 Roald Nefs <info@roaldnefs.com>
4+
#
5+
# This file is part of python-transip.
6+
7+
# python-transip is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Lesser General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
12+
# python-transip is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU Lesser General Public License for more details.
16+
17+
# You should have received a copy of the GNU Lesser General Public License
18+
# along with python-transip. If not, see <https://www.gnu.org/licenses/>.
19+
20+
from typing import Optional, Type
21+
22+
from transip.base import ApiService, ApiObject
23+
from transip.mixins import GetMixin, ListMixin
24+
from transip.v6.objects.invoice import Invoice
25+
26+
27+
class InvoiceService(GetMixin, ListMixin, ApiService):
28+
29+
_path: str = "/invoices"
30+
_obj_cls: Optional[Type[ApiObject]] = Invoice
31+
32+
_resp_list_attr: str = "invoices"
33+
_resp_get_attr: str = "invoice"

0 commit comments

Comments
 (0)