11from typing import List , Optional
22
33from 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
97from bitpay .models .bill .bill import Bill
108from bitpay .models .facade import Facade
119from 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