Skip to content

Commit e9f5bb9

Browse files
SP-738 Type Review: Python
1 parent cb386eb commit e9f5bb9

32 files changed

+213
-104
lines changed

src/bitpay/clients/bitpay_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import urllib
3+
from datetime import datetime, timezone
34
from typing import Dict, Any, Optional
45

56
import requests
@@ -50,7 +51,7 @@ def post(
5051
:raises BitPayApiException
5152
"""
5253
full_url = self.__base_url + uri
53-
form_data = json.dumps(form_data)
54+
form_data = json.dumps(form_data, default=json_serialize)
5455
if signature_required:
5556
self.__headers["x-signature"] = sign(full_url + form_data, self.__ec_key)
5657
self.__headers["x-identity"] = get_compressed_public_key_from_pem(
@@ -117,9 +118,15 @@ def update(self, uri: str, form_data: Any = {}) -> Response:
117118
"""
118119

119120
full_url = self.__base_url + uri
120-
form_data = json.dumps(form_data)
121+
form_data = json.dumps(form_data, default=json_serialize)
121122

122123
self.__headers["x-signature"] = sign(full_url + form_data, self.__ec_key)
123124
self.__headers["x-identity"] = get_compressed_public_key_from_pem(self.__ec_key)
124125

125126
return requests.put(full_url, data=form_data, headers=self.__headers)
127+
128+
129+
def json_serialize(obj):
130+
if isinstance(obj, datetime):
131+
return obj.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
132+
raise TypeError("Type %s not serializable" % type(obj))

src/bitpay/models/bill/bill.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Bill
33
"""
4+
from datetime import datetime
45
from typing import List, Union
56
from .item import Item
67
from ..bitpay_model import BitPayModel
@@ -27,10 +28,10 @@ class Bill(BitPayModel):
2728
country: Union[str, None] = None
2829
cc: Union[List[str], None] = None
2930
phone: Union[str, None] = None
30-
due_date: Union[str, None] = None
31+
due_date: Union[datetime, None] = None
3132
pass_processing_fee: bool = False
3233
status: Union[str, None] = None
3334
url: Union[str, None] = None
34-
created_date: Union[str, None] = None
35+
created_date: Union[datetime, None] = None
3536
id: Union[str, None] = None
3637
merchant: Union[str, None] = None
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""
22
BillStatus
33
"""
4+
from enum import Enum
45

56

6-
class BillStatus:
7+
class BillStatus(Enum):
78
"""
89
Bill Status
910
"""
1011

11-
Draft = "draft"
12-
Sent = "sent"
13-
New = "new"
14-
Paid = "paid"
15-
Complete = "complete"
12+
DRAFT = "draft"
13+
SENT = "sent"
14+
NEW = "new"
15+
PAID = "paid"
16+
COMPLETE = "complete"

src/bitpay/models/bitpay_model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from pydantic import BaseModel, field_validator, ConfigDict
44

5-
from bitpay.exceptions.bitpay_exception import BitPayException
65
from bitpay.exceptions.bitpay_exception_provider import BitPayExceptionProvider
76
from bitpay.utils.model_util import ModelUtil
87

src/bitpay/models/currency.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,16 @@ class Currency(BitPayModel):
200200
symbol: Union[str, None] = None
201201
precision: int
202202
name: str
203-
plural: Union[str, None] = None
204-
alts: Union[str, None] = None
205-
minimum: Union[float, None] = None
203+
plural: str
204+
alts: str
205+
minimum: float
206206
sanctioned: bool = False
207207
decimals: int
208+
display_code: Union[str, None] = None
208209
chain: Union[str, None] = None
210+
max_supply: Union[float, None] = None
211+
tranche_decimals: Union[int, None] = None
212+
contract_address: Union[str, None] = None
209213

210214
@classmethod
211215
def is_valid(cls, value: str) -> bool:

src/bitpay/models/invoice/buyer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ class Buyer(BitPayModel):
2020
email: Union[str, None] = None
2121
phone: Union[str, None] = None
2222
notify: bool = False
23-
buyer_email: Union[str, None] = None
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Union
2+
3+
from bitpay.models.bitpay_model import BitPayModel
4+
5+
6+
class BuyerFields(BitPayModel):
7+
buyer_address1: Union[str, None] = None
8+
buyer_address2: Union[str, None] = None
9+
buyer_city: Union[str, None] = None
10+
buyer_country: Union[str, None] = None
11+
buyer_email: Union[str, None] = None
12+
buyer_name: Union[str, None] = None
13+
buyer_notify: bool = False
14+
buyer_phone: Union[str, None] = None
15+
buyer_state: Union[str, None] = None
16+
buyer_zip: Union[str, None] = None

src/bitpay/models/invoice/invoice.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Invoice
33
"""
4-
from typing import List, Union
4+
from typing import List, Union, Dict
55
from pydantic import Field
66
from .buyer import Buyer
77
from .buyer_provided_info import BuyerProvidedInfo
@@ -39,17 +39,17 @@ class Invoice(BitPayModel):
3939
item_code: Union[str, None] = None
4040
physical: Union[bool, None] = False
4141
payment_currencies: Union[List[str], None] = None
42-
payment_subtotals: Union[dict, None] = None
43-
payment_totals: Union[dict, None] = None
44-
payment_display_totals: Union[dict, None] = None
45-
payment_display_subtotals: Union[dict, None] = None
46-
payment_codes: Union[dict, None] = None
42+
payment_subtotals: Union[Dict[str, int], None] = None
43+
payment_totals: Union[Dict[str, int], None] = None
44+
payment_display_totals: Union[Dict[str, str], None] = None
45+
payment_display_subtotals: Union[Dict[str, str], None] = None
46+
payment_codes: Union[Dict[str, Dict[str, str]], None] = None
4747
acceptance_window: Union[int, None] = None
4848
buyer: Union[Buyer, None] = None
4949
refund_addresses: Union[List[str], None] = None
5050
close_url: Union[str, None] = Field(alias="closeURL", default=None)
5151
auto_redirect: Union[bool, None] = False
52-
json_paypro_required: bool = False
52+
json_paypro_required: Union[bool, None] = False
5353
id: Union[str, None] = None
5454
url: Union[str, None] = None
5555
status: Union[str, None] = None
@@ -71,16 +71,12 @@ class Invoice(BitPayModel):
7171
bill_id: Union[str, None] = None
7272
refund_info: Union[List[RefundInfo], None] = None
7373
extended_notifications: Union[bool, None] = False
74-
invoice_buyer_provided_info: Union[BuyerProvidedInfo, None] = None
7574
transaction_currency: Union[str, None] = None
7675
underpaid_amount: Union[float, None] = None
7776
overpaid_amount: Union[float, None] = None
7877
amount_paid: Union[float, None] = None
7978
display_amount_paid: Union[str, None] = None
80-
exchange_rates: Union[dict, None] = None
81-
payment_string: Union[str, None] = None
82-
verification_link: Union[str, None] = None
83-
buyer_email: Union[str, None] = None
79+
exchange_rates: Union[Dict[str, Dict[str, float]], None] = None
8480
merchant_name: Union[str, None] = None
8581
forced_buyer_selected_wallet: Union[str, None] = None
8682
forced_buyer_selected_transaction_currency: Union[str, None] = None
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
"""
22
InvoiceStatus
33
"""
4+
from enum import Enum
45

56

6-
class InvoiceStatus:
7+
class InvoiceStatus(Enum):
78
"""
89
Invoice Status
910
"""
1011

11-
New = "new"
12-
Paid = "paid"
13-
Confirmed = "confirmed"
14-
Complete = "complete"
15-
Expired = "expired"
16-
Invalid = "invalid"
12+
NEW = "new"
13+
PAID = "paid"
14+
CONFIRMED = "confirmed"
15+
COMPLETE = "complete"
16+
EXPIRED = "expired"
17+
INVALID = "invalid"
18+
DECLINED = "declined"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import Union, Dict
2+
3+
from bitpay.models.bitpay_model import BitPayModel
4+
from bitpay.models.invoice.buyer_fields import BuyerFields
5+
6+
7+
class InvoiceWebhook(BitPayModel):
8+
amount_paid: Union[float, None] = None
9+
buyer_fields: Union[BuyerFields, None] = None
10+
currency: Union[str, None] = None
11+
currency_time: Union[str, None] = None
12+
exception_status: Union[str, None] = None
13+
exchange_rates: Union[Dict[str, Dict[str, float]], None] = None
14+
id: Union[str, None] = None
15+
invoice_time: Union[str, None] = None
16+
order_id: Union[str, None] = None
17+
payment_subtotals: Union[Dict[str, int], None] = None
18+
payment_totals: Union[Dict[str, int], None] = None
19+
pos_data: Union[str, None] = None
20+
price: Union[float, None] = None
21+
status: Union[str, None] = None
22+
transaction_currency: Union[str, None] = None
23+
url: Union[str, None] = None

0 commit comments

Comments
 (0)