Skip to content

Commit cb386eb

Browse files
authored
Merge pull request #61 from mwarzybok-sumoheavy/feature/SP-582
Feature/sp 582
2 parents a6bfacf + 8cd9aa7 commit cb386eb

File tree

67 files changed

+967
-2049
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

+967
-2049
lines changed

src/bitpay/client.py

Lines changed: 33 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,9 @@ 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(
75+
"failed to initiate clients: " + str(exe)
76+
)
7377

7478
@staticmethod
7579
def create_pos_client(self, pos_token: str, environment: Environment = Environment.PROD): # type: ignore
@@ -87,6 +91,9 @@ def create_client( # type: ignore
8791
environment: Environment = Environment.PROD,
8892
proxy: Optional[str] = None,
8993
):
94+
"""
95+
:raises BitPayGenericException
96+
"""
9097
try:
9198
base_url = Client.get_base_url(environment)
9299
ec_key = Client.get_ec_key(private_key_or_private_key_path)
@@ -95,12 +102,19 @@ def create_client( # type: ignore
95102

96103
return Client(bitpay_client, token_container, guid_generator)
97104
except Exception as exe:
98-
raise BitPayException("failed to process configuration: " + str(exe))
105+
BitPayExceptionProvider.throw_generic_exception_with_message(
106+
"failed to process configuration: " + str(exe)
107+
)
99108

100109
@staticmethod
101110
def create_client_by_config_file_path(config_file_path: str): # type: ignore
111+
"""
112+
:raises BitPayGenericException
113+
"""
102114
if not os.path.exists(config_file_path):
103-
raise BitPayException("Configuration file not found")
115+
BitPayExceptionProvider.throw_generic_exception_with_message(
116+
"Configuration file not found"
117+
)
104118

105119
try:
106120
with open(config_file_path, "r") as read_file:
@@ -129,12 +143,20 @@ def create_client_by_config_file_path(config_file_path: str): # type: ignore
129143
private_key_or_private_key_path, token_container, environment, proxy
130144
)
131145
except Exception as exe:
132-
raise BitPayException("Error when reading configuration file. " + str(exe))
146+
BitPayExceptionProvider.throw_generic_exception_with_message(
147+
"Error when reading configuration file. " + str(exe)
148+
)
133149

134150
@staticmethod
135151
def get_ec_key(private_key_or_private_key_path: Optional[str]) -> str:
152+
"""
153+
:raises BitPayGenericException
154+
"""
136155
if private_key_or_private_key_path is None:
137-
raise BitPayException("Private Key file not found")
156+
BitPayExceptionProvider.throw_generic_exception_with_message(
157+
"Private Key file not found"
158+
)
159+
raise BitPayGenericException("Private Key file not found")
138160

139161
if os.path.exists(private_key_or_private_key_path):
140162
with open(private_key_or_private_key_path, "r") as read_file:
@@ -151,12 +173,15 @@ def get_base_url(environment: Environment) -> str:
151173
)
152174

153175
def get_access_token(self, facade: Facade) -> str:
176+
"""
177+
:raises BitPayGenericException
178+
"""
154179
try:
155180
return self.__token_container.get_access_token(facade)
156181
except Exception as exe:
157-
raise BitPayException(
158-
"There is no token for the specified key: " + facade.value
159-
)
182+
message = "There is no token for the specified key: " + facade.value
183+
BitPayExceptionProvider.throw_generic_exception_with_message(message)
184+
raise BitPayGenericException(message)
160185

161186
# //////////////////////////////////////////////////////////////////////////////
162187
# //////////////////////////////////////////////////////////////////////////////

src/bitpay/clients/bill_client.py

Lines changed: 46 additions & 77 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
@@ -32,25 +30,18 @@ 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("bills", bill.to_json(), sign_request)
38+
response_json = ResponseParser.response_to_json_string(response)
4839

4940
try:
5041
return Bill(**response_json)
5142
except Exception as exe:
52-
raise BillCreationException(
53-
"failed to deserialize BitPay server response (Bill) : %s" % str(exe)
43+
BitPayExceptionProvider.throw_deserialize_resource_exception(
44+
"Bill", str(exe)
5445
)
5546

5647
def get(
@@ -64,25 +55,18 @@ 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("bills/%s" % bill_id, params, sign_request)
63+
response_json = ResponseParser.response_to_json_string(response)
8064

8165
try:
8266
return Bill(**response_json)
8367
except Exception as exe:
84-
raise BillQueryException(
85-
"failed to deserialize BitPay server response" " (Bill) : %s" % str(exe)
68+
BitPayExceptionProvider.throw_deserialize_resource_exception(
69+
"Bill", str(exe)
8670
)
8771

8872
def get_bills(self, status: Optional[str] = None) -> List[Bill]:
@@ -92,27 +76,23 @@ 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)
94+
BitPayExceptionProvider.throw_deserialize_resource_exception(
95+
"Bill", str(exe)
11696
)
11797

11898
return bills
@@ -125,27 +105,20 @@ def update(self, bill: Bill, bill_id: str) -> Bill:
125105
:param str bill_id: The Id of the Bill to update.
126106
:return: An updated Bill object.
127107
:rtype: Bill
128-
:raises BitPayException
129-
:raises BillUpdateException
108+
:raises BitPayApiException
109+
:raises BitPayGenericException
130110
"""
131-
try:
132-
if bill.token is None:
133-
raise BillUpdateException("missing Bill token")
111+
if bill.token is None:
112+
BitPayExceptionProvider.throw_missing_parameter_exception()
134113

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-
)
114+
response = self.__bitpay_client.update("bills/%s" % bill_id, bill.to_json())
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)
120+
BitPayExceptionProvider.throw_deserialize_resource_exception(
121+
"Bill", str(exe)
149122
)
150123

151124
def deliver(self, bill_id: str, bill_token: str) -> bool:
@@ -156,23 +129,19 @@ def deliver(self, bill_id: str, bill_token: str) -> bool:
156129
:param str bill_token: The token of the requested bill.
157130
:return: A response status returned from the API.
158131
:rtype: bool
159-
:raises BitPayException
160-
:raises BillDeliveryException
132+
:raises BitPayApiException
133+
:raises BitPayGenericException
161134
"""
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-
)
135+
params = {"token": bill_token}
136+
response = self.__bitpay_client.post(
137+
"bills/%s" % bill_id + "/deliveries", params
138+
)
139+
response_json = ResponseParser.response_to_json_string(response)
172140

173141
try:
174142
return response_json.lower() == "success"
175143
except Exception as exe:
176-
raise BillDeliveryException(
177-
"failed to deserialize BitPay server response" " (Bill) : %s" % str(exe)
144+
BitPayExceptionProvider.throw_deserialize_resource_exception(
145+
"Bill", str(exe)
178146
)
147+
raise BitPayGenericException

0 commit comments

Comments
 (0)