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
@@ -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