Skip to content

Commit fe62d35

Browse files
authored
feat(billing): add support for RedeemCoupon (#1019)
1 parent 99c8c1a commit fe62d35

File tree

8 files changed

+328
-228
lines changed

8 files changed

+328
-228
lines changed

scaleway-async/scaleway_async/billing/v2beta1/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .types import ListInvoicesResponse
2828
from .types import ListTaxesRequest
2929
from .types import ListTaxesResponse
30+
from .types import RedeemCouponRequest
3031
from .api import BillingV2Beta1API
3132

3233
__all__ = [
@@ -57,5 +58,6 @@
5758
"ListInvoicesResponse",
5859
"ListTaxesRequest",
5960
"ListTaxesResponse",
61+
"RedeemCouponRequest",
6062
"BillingV2Beta1API",
6163
]

scaleway-async/scaleway_async/billing/v2beta1/api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
ListTaxesResponseTax,
3535
)
3636
from .marshalling import (
37+
unmarshal_Discount,
3738
unmarshal_Invoice,
3839
unmarshal_ListConsumptionsResponse,
3940
unmarshal_ListDiscountsResponse,
@@ -506,3 +507,37 @@ async def list_discounts_all(
506507
"organization_id": organization_id,
507508
},
508509
)
510+
511+
async def redeem_coupon(
512+
self,
513+
*,
514+
code: str,
515+
organization_id: Optional[str] = None,
516+
) -> Discount:
517+
"""
518+
Redeem coupon.
519+
Redeem a coupon given the related code.
520+
:param code: The code of the coupon to redeem.
521+
:param organization_id: The Organization ID of the discount.
522+
:return: :class:`Discount <Discount>`
523+
524+
Usage:
525+
::
526+
527+
result = await api.redeem_coupon(
528+
code="example",
529+
)
530+
"""
531+
532+
res = self._request(
533+
"POST",
534+
"/billing/v2beta1/redeem-coupon",
535+
params={
536+
"code": code,
537+
"organization_id": organization_id
538+
or self.client.default_organization_id,
539+
},
540+
)
541+
542+
self._throw_on_error(res)
543+
return unmarshal_Discount(res.json())

scaleway-async/scaleway_async/billing/v2beta1/marshalling.py

Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,130 @@
88
unmarshal_Money,
99
)
1010
from .types import (
11-
Invoice,
12-
ListConsumptionsResponseConsumption,
13-
ListConsumptionsResponse,
1411
DiscountCoupon,
1512
DiscountFilter,
1613
Discount,
14+
Invoice,
15+
ListConsumptionsResponseConsumption,
16+
ListConsumptionsResponse,
1717
ListDiscountsResponse,
1818
ListInvoicesResponse,
1919
ListTaxesResponseTax,
2020
ListTaxesResponse,
2121
)
2222

2323

24+
def unmarshal_DiscountCoupon(data: Any) -> DiscountCoupon:
25+
if not isinstance(data, dict):
26+
raise TypeError(
27+
"Unmarshalling the type 'DiscountCoupon' failed as data isn't a dictionary."
28+
)
29+
30+
args: Dict[str, Any] = {}
31+
32+
field = data.get("description", None)
33+
if field is not None:
34+
args["description"] = field
35+
else:
36+
args["description"] = None
37+
38+
return DiscountCoupon(**args)
39+
40+
41+
def unmarshal_DiscountFilter(data: Any) -> DiscountFilter:
42+
if not isinstance(data, dict):
43+
raise TypeError(
44+
"Unmarshalling the type 'DiscountFilter' failed as data isn't a dictionary."
45+
)
46+
47+
args: Dict[str, Any] = {}
48+
49+
field = data.get("type", None)
50+
if field is not None:
51+
args["type_"] = field
52+
53+
field = data.get("value", None)
54+
if field is not None:
55+
args["value"] = field
56+
57+
field = data.get("exclude", None)
58+
if field is not None:
59+
args["exclude"] = field
60+
61+
return DiscountFilter(**args)
62+
63+
64+
def unmarshal_Discount(data: Any) -> Discount:
65+
if not isinstance(data, dict):
66+
raise TypeError(
67+
"Unmarshalling the type 'Discount' failed as data isn't a dictionary."
68+
)
69+
70+
args: Dict[str, Any] = {}
71+
72+
field = data.get("id", None)
73+
if field is not None:
74+
args["id"] = field
75+
76+
field = data.get("organization_id", None)
77+
if field is not None:
78+
args["organization_id"] = field
79+
80+
field = data.get("description", None)
81+
if field is not None:
82+
args["description"] = field
83+
84+
field = data.get("value", None)
85+
if field is not None:
86+
args["value"] = field
87+
88+
field = data.get("value_used", None)
89+
if field is not None:
90+
args["value_used"] = field
91+
92+
field = data.get("value_remaining", None)
93+
if field is not None:
94+
args["value_remaining"] = field
95+
96+
field = data.get("mode", None)
97+
if field is not None:
98+
args["mode"] = field
99+
100+
field = data.get("filters", None)
101+
if field is not None:
102+
args["filters"] = (
103+
[unmarshal_DiscountFilter(v) for v in field] if field is not None else None
104+
)
105+
106+
field = data.get("creation_date", None)
107+
if field is not None:
108+
args["creation_date"] = (
109+
parser.isoparse(field) if isinstance(field, str) else field
110+
)
111+
else:
112+
args["creation_date"] = None
113+
114+
field = data.get("start_date", None)
115+
if field is not None:
116+
args["start_date"] = parser.isoparse(field) if isinstance(field, str) else field
117+
else:
118+
args["start_date"] = None
119+
120+
field = data.get("stop_date", None)
121+
if field is not None:
122+
args["stop_date"] = parser.isoparse(field) if isinstance(field, str) else field
123+
else:
124+
args["stop_date"] = None
125+
126+
field = data.get("coupon", None)
127+
if field is not None:
128+
args["coupon"] = unmarshal_DiscountCoupon(field)
129+
else:
130+
args["coupon"] = None
131+
132+
return Discount(**args)
133+
134+
24135
def unmarshal_Invoice(data: Any) -> Invoice:
25136
if not isinstance(data, dict):
26137
raise TypeError(
@@ -204,117 +315,6 @@ def unmarshal_ListConsumptionsResponse(data: Any) -> ListConsumptionsResponse:
204315
return ListConsumptionsResponse(**args)
205316

206317

207-
def unmarshal_DiscountCoupon(data: Any) -> DiscountCoupon:
208-
if not isinstance(data, dict):
209-
raise TypeError(
210-
"Unmarshalling the type 'DiscountCoupon' failed as data isn't a dictionary."
211-
)
212-
213-
args: Dict[str, Any] = {}
214-
215-
field = data.get("description", None)
216-
if field is not None:
217-
args["description"] = field
218-
else:
219-
args["description"] = None
220-
221-
return DiscountCoupon(**args)
222-
223-
224-
def unmarshal_DiscountFilter(data: Any) -> DiscountFilter:
225-
if not isinstance(data, dict):
226-
raise TypeError(
227-
"Unmarshalling the type 'DiscountFilter' failed as data isn't a dictionary."
228-
)
229-
230-
args: Dict[str, Any] = {}
231-
232-
field = data.get("type", None)
233-
if field is not None:
234-
args["type_"] = field
235-
236-
field = data.get("value", None)
237-
if field is not None:
238-
args["value"] = field
239-
240-
field = data.get("exclude", None)
241-
if field is not None:
242-
args["exclude"] = field
243-
244-
return DiscountFilter(**args)
245-
246-
247-
def unmarshal_Discount(data: Any) -> Discount:
248-
if not isinstance(data, dict):
249-
raise TypeError(
250-
"Unmarshalling the type 'Discount' failed as data isn't a dictionary."
251-
)
252-
253-
args: Dict[str, Any] = {}
254-
255-
field = data.get("id", None)
256-
if field is not None:
257-
args["id"] = field
258-
259-
field = data.get("organization_id", None)
260-
if field is not None:
261-
args["organization_id"] = field
262-
263-
field = data.get("description", None)
264-
if field is not None:
265-
args["description"] = field
266-
267-
field = data.get("value", None)
268-
if field is not None:
269-
args["value"] = field
270-
271-
field = data.get("value_used", None)
272-
if field is not None:
273-
args["value_used"] = field
274-
275-
field = data.get("value_remaining", None)
276-
if field is not None:
277-
args["value_remaining"] = field
278-
279-
field = data.get("mode", None)
280-
if field is not None:
281-
args["mode"] = field
282-
283-
field = data.get("filters", None)
284-
if field is not None:
285-
args["filters"] = (
286-
[unmarshal_DiscountFilter(v) for v in field] if field is not None else None
287-
)
288-
289-
field = data.get("creation_date", None)
290-
if field is not None:
291-
args["creation_date"] = (
292-
parser.isoparse(field) if isinstance(field, str) else field
293-
)
294-
else:
295-
args["creation_date"] = None
296-
297-
field = data.get("start_date", None)
298-
if field is not None:
299-
args["start_date"] = parser.isoparse(field) if isinstance(field, str) else field
300-
else:
301-
args["start_date"] = None
302-
303-
field = data.get("stop_date", None)
304-
if field is not None:
305-
args["stop_date"] = parser.isoparse(field) if isinstance(field, str) else field
306-
else:
307-
args["stop_date"] = None
308-
309-
field = data.get("coupon", None)
310-
if field is not None:
311-
args["coupon"] = unmarshal_DiscountCoupon(field)
312-
else:
313-
args["coupon"] = None
314-
315-
return Discount(**args)
316-
317-
318318
def unmarshal_ListDiscountsResponse(data: Any) -> ListDiscountsResponse:
319319
if not isinstance(data, dict):
320320
raise TypeError(

scaleway-async/scaleway_async/billing/v2beta1/types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,16 @@ class ListTaxesResponse:
617617
"""
618618
Last consumption update date.
619619
"""
620+
621+
622+
@dataclass
623+
class RedeemCouponRequest:
624+
code: str
625+
"""
626+
The code of the coupon to redeem.
627+
"""
628+
629+
organization_id: Optional[str]
630+
"""
631+
The Organization ID of the discount.
632+
"""

scaleway/scaleway/billing/v2beta1/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .types import ListInvoicesResponse
2828
from .types import ListTaxesRequest
2929
from .types import ListTaxesResponse
30+
from .types import RedeemCouponRequest
3031
from .api import BillingV2Beta1API
3132

3233
__all__ = [
@@ -57,5 +58,6 @@
5758
"ListInvoicesResponse",
5859
"ListTaxesRequest",
5960
"ListTaxesResponse",
61+
"RedeemCouponRequest",
6062
"BillingV2Beta1API",
6163
]

scaleway/scaleway/billing/v2beta1/api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
ListTaxesResponseTax,
3535
)
3636
from .marshalling import (
37+
unmarshal_Discount,
3738
unmarshal_Invoice,
3839
unmarshal_ListConsumptionsResponse,
3940
unmarshal_ListDiscountsResponse,
@@ -506,3 +507,37 @@ def list_discounts_all(
506507
"organization_id": organization_id,
507508
},
508509
)
510+
511+
def redeem_coupon(
512+
self,
513+
*,
514+
code: str,
515+
organization_id: Optional[str] = None,
516+
) -> Discount:
517+
"""
518+
Redeem coupon.
519+
Redeem a coupon given the related code.
520+
:param code: The code of the coupon to redeem.
521+
:param organization_id: The Organization ID of the discount.
522+
:return: :class:`Discount <Discount>`
523+
524+
Usage:
525+
::
526+
527+
result = api.redeem_coupon(
528+
code="example",
529+
)
530+
"""
531+
532+
res = self._request(
533+
"POST",
534+
"/billing/v2beta1/redeem-coupon",
535+
params={
536+
"code": code,
537+
"organization_id": organization_id
538+
or self.client.default_organization_id,
539+
},
540+
)
541+
542+
self._throw_on_error(res)
543+
return unmarshal_Discount(res.json())

0 commit comments

Comments
 (0)