Skip to content

Commit 2ee9f97

Browse files
SP-582 Python - Implement better refund exceptions
1 parent a6bfacf commit 2ee9f97

File tree

67 files changed

+837
-2064
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+837
-2064
lines changed

src/bitpay/client.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from bitpay.clients.wallet_client import WalletClient
2323
from .config import Config
2424
from .environment import Environment
25+
from .exceptions.bitpay_exception_provider import BitPayExceptionProvider
26+
from .exceptions.bitpay_generic_exception import BitPayGenericException
2527
from .models.facade import Facade
2628
from .models.bill.bill import Bill
2729
from .models.invoice.invoice_event_token import InvoiceEventToken
@@ -69,7 +71,7 @@ def __init__(
6971
guid_generator = GuidGenerator()
7072
self.__guid_generator = guid_generator
7173
except Exception as exe:
72-
raise BitPayException("failed to initiate clients: " + str(exe))
74+
BitPayExceptionProvider.throw_generic_exception_with_message("failed to initiate clients: " + str(exe))
7375

7476
@staticmethod
7577
def create_pos_client(self, pos_token: str, environment: Environment = Environment.PROD): # type: ignore
@@ -87,6 +89,9 @@ def create_client( # type: ignore
8789
environment: Environment = Environment.PROD,
8890
proxy: Optional[str] = None,
8991
):
92+
"""
93+
:raises BitPayGenericException
94+
"""
9095
try:
9196
base_url = Client.get_base_url(environment)
9297
ec_key = Client.get_ec_key(private_key_or_private_key_path)
@@ -95,12 +100,15 @@ def create_client( # type: ignore
95100

96101
return Client(bitpay_client, token_container, guid_generator)
97102
except Exception as exe:
98-
raise BitPayException("failed to process configuration: " + str(exe))
103+
BitPayExceptionProvider.throw_generic_exception_with_message("failed to process configuration: " + str(exe))
99104

100105
@staticmethod
101106
def create_client_by_config_file_path(config_file_path: str): # type: ignore
107+
"""
108+
:raises BitPayGenericException
109+
"""
102110
if not os.path.exists(config_file_path):
103-
raise BitPayException("Configuration file not found")
111+
BitPayExceptionProvider.throw_generic_exception_with_message("Configuration file not found")
104112

105113
try:
106114
with open(config_file_path, "r") as read_file:
@@ -129,12 +137,16 @@ def create_client_by_config_file_path(config_file_path: str): # type: ignore
129137
private_key_or_private_key_path, token_container, environment, proxy
130138
)
131139
except Exception as exe:
132-
raise BitPayException("Error when reading configuration file. " + str(exe))
140+
BitPayExceptionProvider.throw_generic_exception_with_message("Error when reading configuration file. " + str(exe))
133141

134142
@staticmethod
135143
def get_ec_key(private_key_or_private_key_path: Optional[str]) -> str:
144+
"""
145+
:raises BitPayGenericException
146+
"""
136147
if private_key_or_private_key_path is None:
137-
raise BitPayException("Private Key file not found")
148+
BitPayExceptionProvider.throw_generic_exception_with_message("Private Key file not found")
149+
raise BitPayGenericException("Private Key file not found")
138150

139151
if os.path.exists(private_key_or_private_key_path):
140152
with open(private_key_or_private_key_path, "r") as read_file:
@@ -151,12 +163,15 @@ def get_base_url(environment: Environment) -> str:
151163
)
152164

153165
def get_access_token(self, facade: Facade) -> str:
166+
"""
167+
:raises BitPayGenericException
168+
"""
154169
try:
155170
return self.__token_container.get_access_token(facade)
156171
except Exception as exe:
157-
raise BitPayException(
158-
"There is no token for the specified key: " + facade.value
159-
)
172+
message = "There is no token for the specified key: " + facade.value
173+
BitPayExceptionProvider.throw_generic_exception_with_message(message)
174+
raise BitPayGenericException(message)
160175

161176
# //////////////////////////////////////////////////////////////////////////////
162177
# //////////////////////////////////////////////////////////////////////////////

src/bitpay/clients/bill_client.py

Lines changed: 51 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from typing import List, Optional
22

33
from bitpay.clients.bitpay_client import BitPayClient
4-
from bitpay.exceptions.bill_creation_exception import BillCreationException
5-
from bitpay.exceptions.bill_delivery_exception import BillDeliveryException
6-
from bitpay.exceptions.bill_query_exception import BillQueryException
7-
from bitpay.exceptions.bill_update_exception import BillUpdateException
8-
from bitpay.exceptions.bitpay_exception import BitPayException
4+
from bitpay.clients.response_parser import ResponseParser
5+
from bitpay.exceptions.bitpay_exception_provider import BitPayExceptionProvider
6+
from bitpay.exceptions.bitpay_generic_exception import BitPayGenericException
97
from bitpay.models.bill.bill import Bill
108
from bitpay.models.facade import Facade
119
from bitpay.utils.token_container import TokenContainer
@@ -16,13 +14,13 @@ class BillClient:
1614
__token_container = TokenContainer
1715

1816
def __init__(
19-
self, bitpay_client: BitPayClient, token_container: TokenContainer
17+
self, bitpay_client: BitPayClient, token_container: TokenContainer
2018
) -> None:
2119
self.__bitpay_client = bitpay_client
2220
self.__token_container = token_container
2321

2422
def create(
25-
self, bill: Bill, facade: Facade = Facade.MERCHANT, sign_request: bool = True
23+
self, bill: Bill, facade: Facade = Facade.MERCHANT, sign_request: bool = True
2624
) -> Bill:
2725
"""
2826
Create a BitPay Bill.
@@ -32,29 +30,22 @@ def create(
3230
:param bool sign_request: Signed request.
3331
:return: A BitPay generated Bill object.
3432
:rtype: Bill
35-
:raises BitPayException
36-
:raises BillCreationException
33+
:raises BitPayApiException
34+
:raises BitPayGenericException
3735
"""
38-
try:
39-
bill.token = self.__token_container.get_access_token(facade)
40-
response_json = self.__bitpay_client.post(
41-
"bills", bill.to_json(), sign_request
42-
)
43-
except BitPayException as exe:
44-
raise BillCreationException(
45-
"failed to serialize bill object : %s" % str(exe),
46-
api_code=exe.get_api_code(),
47-
)
36+
bill.token = self.__token_container.get_access_token(facade)
37+
response = self.__bitpay_client.post(
38+
"bills", bill.to_json(), sign_request
39+
)
40+
response_json = ResponseParser.response_to_json_string(response)
4841

4942
try:
5043
return Bill(**response_json)
5144
except Exception as exe:
52-
raise BillCreationException(
53-
"failed to deserialize BitPay server response (Bill) : %s" % str(exe)
54-
)
45+
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
5546

5647
def get(
57-
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
5849
) -> Bill:
5950
"""
6051
Retrieve a BitPay bill by bill id using the specified facade.
@@ -64,26 +55,19 @@ def get(
6455
:param bool sign_request: Signed request.
6556
:return: A BitPay Bill object.
6657
:rtype: Bill
67-
:raises BitPayException
68-
:raises BillQueryException
58+
:raises BitPayApiException
59+
:raises BitPayGenericException
6960
"""
70-
try:
71-
params = {"token": self.__token_container.get_access_token(facade)}
72-
response_json = self.__bitpay_client.get(
73-
"bills/%s" % bill_id, params, sign_request
74-
)
75-
except BitPayException as exe:
76-
raise BillQueryException(
77-
"failed to serialize bill object : %s" % str(exe),
78-
api_code=exe.get_api_code(),
79-
)
61+
params = {"token": self.__token_container.get_access_token(facade)}
62+
response = self.__bitpay_client.get(
63+
"bills/%s" % bill_id, params, sign_request
64+
)
65+
response_json = ResponseParser.response_to_json_string(response)
8066

8167
try:
8268
return Bill(**response_json)
8369
except Exception as exe:
84-
raise BillQueryException(
85-
"failed to deserialize BitPay server response" " (Bill) : %s" % str(exe)
86-
)
70+
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
8771

8872
def get_bills(self, status: Optional[str] = None) -> List[Bill]:
8973
"""
@@ -92,28 +76,22 @@ def get_bills(self, status: Optional[str] = None) -> List[Bill]:
9276
:param str status: The status to filter the bills.
9377
:return: A list of BitPay Bill objects.
9478
:rtype: [Bill]
95-
:raises BitPayException
96-
:raises BillQueryException
79+
:raises BitPayApiException
80+
:raises BitPayGenericException
9781
"""
98-
try:
99-
params = {"token": self.__token_container.get_access_token(Facade.MERCHANT)}
100-
if status is not None:
101-
params["status"] = status
102-
response_json = self.__bitpay_client.get("bills", params, True)
103-
except BitPayException as exe:
104-
raise BillQueryException(
105-
"failed to serialize bill object : %s" % str(exe),
106-
api_code=exe.get_api_code(),
107-
)
82+
params = {"token": self.__token_container.get_access_token(Facade.MERCHANT)}
83+
if status is not None:
84+
params["status"] = status
85+
response = self.__bitpay_client.get("bills", params, True)
86+
response_json = ResponseParser.response_to_json_string(response)
87+
88+
bills = []
10889

10990
try:
110-
bills = []
11191
for bill_data in response_json:
11292
bills.append(Bill(**bill_data))
11393
except Exception as exe:
114-
raise BillQueryException(
115-
"failed to deserialize BitPay server response" " (Bill) : %s" % str(exe)
116-
)
94+
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
11795

11896
return bills
11997

@@ -125,28 +103,21 @@ def update(self, bill: Bill, bill_id: str) -> Bill:
125103
:param str bill_id: The Id of the Bill to update.
126104
:return: An updated Bill object.
127105
:rtype: Bill
128-
:raises BitPayException
129-
:raises BillUpdateException
106+
:raises BitPayApiException
107+
:raises BitPayGenericException
130108
"""
131-
try:
132-
if bill.token is None:
133-
raise BillUpdateException("missing Bill token")
134-
135-
response_json = self.__bitpay_client.update(
136-
"bills/%s" % bill_id, bill.to_json()
137-
)
138-
except BitPayException as exe:
139-
raise BillUpdateException(
140-
"failed to serialize bill object : %s" % str(exe),
141-
api_code=exe.get_api_code(),
142-
)
109+
if bill.token is None:
110+
BitPayExceptionProvider.throw_missing_parameter_exception()
111+
112+
response = self.__bitpay_client.update(
113+
"bills/%s" % bill_id, bill.to_json()
114+
)
115+
response_json = ResponseParser.response_to_json_string(response)
143116

144117
try:
145118
return Bill(**response_json)
146119
except Exception as exe:
147-
raise BillUpdateException(
148-
"failed to deserialize BitPay server response" " (Bill) : %s" % str(exe)
149-
)
120+
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
150121

151122
def deliver(self, bill_id: str, bill_token: str) -> bool:
152123
"""
@@ -156,23 +127,17 @@ def deliver(self, bill_id: str, bill_token: str) -> bool:
156127
:param str bill_token: The token of the requested bill.
157128
:return: A response status returned from the API.
158129
:rtype: bool
159-
:raises BitPayException
160-
:raises BillDeliveryException
130+
:raises BitPayApiException
131+
:raises BitPayGenericException
161132
"""
162-
try:
163-
params = {"token": bill_token}
164-
response_json = self.__bitpay_client.post(
165-
"bills/%s" % bill_id + "/deliveries", params
166-
)
167-
except BitPayException as exe:
168-
raise BillDeliveryException(
169-
"failed to serialize bill object : %s" % str(exe),
170-
api_code=exe.get_api_code(),
171-
)
133+
params = {"token": bill_token}
134+
response = self.__bitpay_client.post(
135+
"bills/%s" % bill_id + "/deliveries", params
136+
)
137+
response_json = ResponseParser.response_to_json_string(response)
172138

173139
try:
174140
return response_json.lower() == "success"
175141
except Exception as exe:
176-
raise BillDeliveryException(
177-
"failed to deserialize BitPay server response" " (Bill) : %s" % str(exe)
178-
)
142+
BitPayExceptionProvider.throw_deserialize_resource_exception("Bill", str(exe))
143+
raise BitPayGenericException

0 commit comments

Comments
 (0)