Skip to content

Commit 50d07f6

Browse files
SP-582 Python - Implement better refund exceptions - fix code style
1 parent 2ee9f97 commit 50d07f6

19 files changed

+237
-148
lines changed

src/bitpay/client.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def __init__(
7171
guid_generator = GuidGenerator()
7272
self.__guid_generator = guid_generator
7373
except Exception as exe:
74-
BitPayExceptionProvider.throw_generic_exception_with_message("failed to initiate clients: " + str(exe))
74+
BitPayExceptionProvider.throw_generic_exception_with_message(
75+
"failed to initiate clients: " + str(exe)
76+
)
7577

7678
@staticmethod
7779
def create_pos_client(self, pos_token: str, environment: Environment = Environment.PROD): # type: ignore
@@ -100,15 +102,19 @@ def create_client( # type: ignore
100102

101103
return Client(bitpay_client, token_container, guid_generator)
102104
except Exception as exe:
103-
BitPayExceptionProvider.throw_generic_exception_with_message("failed to process configuration: " + str(exe))
105+
BitPayExceptionProvider.throw_generic_exception_with_message(
106+
"failed to process configuration: " + str(exe)
107+
)
104108

105109
@staticmethod
106110
def create_client_by_config_file_path(config_file_path: str): # type: ignore
107111
"""
108112
:raises BitPayGenericException
109113
"""
110114
if not os.path.exists(config_file_path):
111-
BitPayExceptionProvider.throw_generic_exception_with_message("Configuration file not found")
115+
BitPayExceptionProvider.throw_generic_exception_with_message(
116+
"Configuration file not found"
117+
)
112118

113119
try:
114120
with open(config_file_path, "r") as read_file:
@@ -137,15 +143,19 @@ def create_client_by_config_file_path(config_file_path: str): # type: ignore
137143
private_key_or_private_key_path, token_container, environment, proxy
138144
)
139145
except Exception as exe:
140-
BitPayExceptionProvider.throw_generic_exception_with_message("Error when reading configuration file. " + str(exe))
146+
BitPayExceptionProvider.throw_generic_exception_with_message(
147+
"Error when reading configuration file. " + str(exe)
148+
)
141149

142150
@staticmethod
143151
def get_ec_key(private_key_or_private_key_path: Optional[str]) -> str:
144152
"""
145153
:raises BitPayGenericException
146154
"""
147155
if private_key_or_private_key_path is None:
148-
BitPayExceptionProvider.throw_generic_exception_with_message("Private Key file not found")
156+
BitPayExceptionProvider.throw_generic_exception_with_message(
157+
"Private Key file not found"
158+
)
149159
raise BitPayGenericException("Private Key file not found")
150160

151161
if os.path.exists(private_key_or_private_key_path):

src/bitpay/clients/bill_client.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class BillClient:
1414
__token_container = TokenContainer
1515

1616
def __init__(
17-
self, bitpay_client: BitPayClient, token_container: TokenContainer
17+
self, bitpay_client: BitPayClient, token_container: TokenContainer
1818
) -> None:
1919
self.__bitpay_client = bitpay_client
2020
self.__token_container = token_container
2121

2222
def create(
23-
self, bill: Bill, facade: Facade = Facade.MERCHANT, sign_request: bool = True
23+
self, bill: Bill, facade: Facade = Facade.MERCHANT, sign_request: bool = True
2424
) -> Bill:
2525
"""
2626
Create a BitPay Bill.
@@ -34,18 +34,18 @@ def create(
3434
:raises BitPayGenericException
3535
"""
3636
bill.token = self.__token_container.get_access_token(facade)
37-
response = self.__bitpay_client.post(
38-
"bills", bill.to_json(), sign_request
39-
)
37+
response = self.__bitpay_client.post("bills", bill.to_json(), sign_request)
4038
response_json = ResponseParser.response_to_json_string(response)
4139

4240
try:
4341
return Bill(**response_json)
4442
except Exception as exe:
45-
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
43+
BitPayExceptionProvider.throw_deserialize_resource_exception(
44+
"Bill", str(exe)
45+
)
4646

4747
def get(
48-
self, bill_id: str, facade: Facade = Facade.MERCHANT, sign_request: bool = True
48+
self, bill_id: str, facade: Facade = Facade.MERCHANT, sign_request: bool = True
4949
) -> Bill:
5050
"""
5151
Retrieve a BitPay bill by bill id using the specified facade.
@@ -59,15 +59,15 @@ def get(
5959
:raises BitPayGenericException
6060
"""
6161
params = {"token": self.__token_container.get_access_token(facade)}
62-
response = self.__bitpay_client.get(
63-
"bills/%s" % bill_id, params, sign_request
64-
)
62+
response = self.__bitpay_client.get("bills/%s" % bill_id, params, sign_request)
6563
response_json = ResponseParser.response_to_json_string(response)
6664

6765
try:
6866
return Bill(**response_json)
6967
except Exception as exe:
70-
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
68+
BitPayExceptionProvider.throw_deserialize_resource_exception(
69+
"Bill", str(exe)
70+
)
7171

7272
def get_bills(self, status: Optional[str] = None) -> List[Bill]:
7373
"""
@@ -91,7 +91,9 @@ def get_bills(self, status: Optional[str] = None) -> List[Bill]:
9191
for bill_data in response_json:
9292
bills.append(Bill(**bill_data))
9393
except Exception as exe:
94-
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
94+
BitPayExceptionProvider.throw_deserialize_resource_exception(
95+
"Bill", str(exe)
96+
)
9597

9698
return bills
9799

@@ -109,15 +111,15 @@ def update(self, bill: Bill, bill_id: str) -> Bill:
109111
if bill.token is None:
110112
BitPayExceptionProvider.throw_missing_parameter_exception()
111113

112-
response = self.__bitpay_client.update(
113-
"bills/%s" % bill_id, bill.to_json()
114-
)
114+
response = self.__bitpay_client.update("bills/%s" % bill_id, bill.to_json())
115115
response_json = ResponseParser.response_to_json_string(response)
116116

117117
try:
118118
return Bill(**response_json)
119119
except Exception as exe:
120-
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
120+
BitPayExceptionProvider.throw_deserialize_resource_exception(
121+
"Bill", str(exe)
122+
)
121123

122124
def deliver(self, bill_id: str, bill_token: str) -> bool:
123125
"""
@@ -139,5 +141,7 @@ def deliver(self, bill_id: str, bill_token: str) -> bool:
139141
try:
140142
return response_json.lower() == "success"
141143
except Exception as exe:
142-
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
144+
BitPayExceptionProvider.throw_deserialize_resource_exception(
145+
"Bill", str(exe)
146+
)
143147
raise BitPayGenericException

src/bitpay/clients/bitpay_client.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,3 @@ def update(self, uri: str, form_data: Any = {}) -> Response:
123123
self.__headers["x-identity"] = get_compressed_public_key_from_pem(self.__ec_key)
124124

125125
return requests.put(full_url, data=form_data, headers=self.__headers)
126-
127-

src/bitpay/clients/currency_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def get_currencies(self) -> Dict[str, Currency]:
3131
currency_obj = Currency(**currency)
3232
currencies[currency_obj.code] = currency_obj
3333
except Exception as exe:
34-
BitPayExceptionProvider.throw_deserialize_resource_exception("Currency", str(exe))
34+
BitPayExceptionProvider.throw_deserialize_resource_exception(
35+
"Currency", str(exe)
36+
)
3537

3638
return currencies

src/bitpay/clients/invoice_client.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ def create(
4848

4949
invoice.token = self.__token_container.get_access_token(facade)
5050
invoice_json = invoice.to_json()
51-
response = self.__bitpay_client.post(
52-
"invoices", invoice_json, sign_request
53-
)
51+
response = self.__bitpay_client.post("invoices", invoice_json, sign_request)
5452
response_json = ResponseParser.response_to_json_string(response)
5553

5654
try:
5755
invoice = Invoice(**response_json)
5856
except Exception as exe:
59-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
57+
BitPayExceptionProvider.throw_deserialize_resource_exception(
58+
"Invoice", str(exe)
59+
)
6060

6161
return invoice
6262

@@ -88,7 +88,9 @@ def get(
8888
try:
8989
invoice = Invoice(**response_json)
9090
except Exception as exe:
91-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
91+
BitPayExceptionProvider.throw_deserialize_resource_exception(
92+
"Invoice", str(exe)
93+
)
9294
raise
9395

9496
return invoice
@@ -118,7 +120,9 @@ def get_by_guid(
118120
try:
119121
invoice = Invoice(**response_json)
120122
except Exception as exe:
121-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
123+
BitPayExceptionProvider.throw_deserialize_resource_exception(
124+
"Invoice", str(exe)
125+
)
122126
raise
123127

124128
return invoice
@@ -169,7 +173,9 @@ def get_invoices(
169173
for invoice_data in response_json:
170174
invoices.append(Invoice(**invoice_data))
171175
except Exception as exe:
172-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
176+
BitPayExceptionProvider.throw_deserialize_resource_exception(
177+
"Invoice", str(exe)
178+
)
173179
raise
174180

175181
return invoices
@@ -204,15 +210,15 @@ def update(
204210
if sms_code is not None:
205211
params["smsCode"] = sms_code
206212

207-
response = self.__bitpay_client.update(
208-
"invoices/%s" % invoice_id, params
209-
)
213+
response = self.__bitpay_client.update("invoices/%s" % invoice_id, params)
210214
response_json = ResponseParser.response_to_json_string(response)
211215

212216
try:
213217
invoice = Invoice(**response_json)
214218
except Exception as exe:
215-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
219+
BitPayExceptionProvider.throw_deserialize_resource_exception(
220+
"Invoice", str(exe)
221+
)
216222
raise
217223

218224
return invoice
@@ -233,15 +239,15 @@ def cancel(self, invoice_id: str, force_cancel: bool = False) -> Invoice:
233239
"token": self.__token_container.get_access_token(Facade.MERCHANT),
234240
"forceCancel": force_cancel,
235241
}
236-
response = self.__bitpay_client.delete(
237-
"invoices/%s" % invoice_id, params
238-
)
242+
response = self.__bitpay_client.delete("invoices/%s" % invoice_id, params)
239243
response_json = ResponseParser.response_to_json_string(response)
240244

241245
try:
242246
invoice = Invoice(**response_json)
243247
except Exception as exe:
244-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
248+
BitPayExceptionProvider.throw_deserialize_resource_exception(
249+
"Invoice", str(exe)
250+
)
245251
raise
246252

247253
return invoice
@@ -262,15 +268,15 @@ def cancel_by_guid(self, guid: str, force_cancel: bool = False) -> Invoice:
262268
"token": self.__token_container.get_access_token(Facade.MERCHANT),
263269
"forceCancel": force_cancel,
264270
}
265-
response = self.__bitpay_client.delete(
266-
"invoices/guid/%s" % guid, params
267-
)
271+
response = self.__bitpay_client.delete("invoices/guid/%s" % guid, params)
268272
response_json = ResponseParser.response_to_json_string(response)
269273

270274
try:
271275
invoice = Invoice(**response_json)
272276
except Exception as exe:
273-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
277+
BitPayExceptionProvider.throw_deserialize_resource_exception(
278+
"Invoice", str(exe)
279+
)
274280
raise
275281

276282
return invoice
@@ -296,7 +302,9 @@ def get_event_token(self, invoice_id: str) -> InvoiceEventToken:
296302
try:
297303
return InvoiceEventToken(**response_json)
298304
except Exception as exe:
299-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
305+
BitPayExceptionProvider.throw_deserialize_resource_exception(
306+
"Invoice", str(exe)
307+
)
300308
raise
301309

302310
def pay(self, invoice_id: str, status: str) -> Invoice:
@@ -314,15 +322,15 @@ def pay(self, invoice_id: str, status: str) -> Invoice:
314322
"token": self.__token_container.get_access_token(Facade.MERCHANT),
315323
"status": status,
316324
}
317-
response = self.__bitpay_client.update(
318-
"invoices/pay/%s" % invoice_id, params
319-
)
325+
response = self.__bitpay_client.update("invoices/pay/%s" % invoice_id, params)
320326
response_json = ResponseParser.response_to_json_string(response)
321327

322328
try:
323329
invoice = Invoice(**response_json)
324330
except Exception as exe:
325-
BitPayExceptionProvider.throw_deserialize_resource_exception("Invoice", str(exe))
331+
BitPayExceptionProvider.throw_deserialize_resource_exception(
332+
"Invoice", str(exe)
333+
)
326334
raise
327335

328336
return invoice

src/bitpay/clients/ledger_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def get_entries(
4545
for ledger in response_json:
4646
ledgers.append(LedgerEntry(**ledger))
4747
except Exception as exe:
48-
BitPayExceptionProvider.throw_deserialize_resource_exception("Ledger", str(exe))
48+
BitPayExceptionProvider.throw_deserialize_resource_exception(
49+
"Ledger", str(exe)
50+
)
4951
raise
5052

5153
return ledgers
@@ -69,7 +71,9 @@ def get_ledgers(self) -> List[Ledger]:
6971
for ledger in response_json:
7072
ledgers.append(Ledger(**ledger))
7173
except Exception as exe:
72-
BitPayExceptionProvider.throw_deserialize_resource_exception("Ledger", str(exe))
74+
BitPayExceptionProvider.throw_deserialize_resource_exception(
75+
"Ledger", str(exe)
76+
)
7377
raise
7478

7579
return ledgers

0 commit comments

Comments
 (0)