|
| 1 | +from typing import List |
| 2 | + |
| 3 | +from bitpay.client.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 |
| 9 | +from bitpay.models.bill.bill import Bill |
| 10 | +from bitpay.models.facade import Facade |
| 11 | +from bitpay.utils.token_container import TokenContainer |
| 12 | + |
| 13 | + |
| 14 | +class BillClient: |
| 15 | + __bitpay_client = BitPayClient |
| 16 | + __token_container = TokenContainer |
| 17 | + |
| 18 | + def __init__(self, bitpay_client: BitPayClient, token_container: TokenContainer): |
| 19 | + self.__bitpay_client = bitpay_client |
| 20 | + self.__token_container = token_container |
| 21 | + |
| 22 | + def create( |
| 23 | + self, bill: Bill, facade: str = Facade.MERCHANT, sign_request: bool = True |
| 24 | + ) -> Bill: |
| 25 | + """ |
| 26 | + Create a BitPay Bill. |
| 27 | +
|
| 28 | + :param Bill bill: A Bill object with request parameters defined. |
| 29 | + :param str facade: The facade used to create it. |
| 30 | + :param bool sign_request: Signed request. |
| 31 | + :return: A BitPay generated Bill object. |
| 32 | + :rtype: Bill |
| 33 | + :raises BitPayException |
| 34 | + :raises BillCreationException |
| 35 | + """ |
| 36 | + try: |
| 37 | + bill.set_token(self.__token_container.get_access_token(facade)) |
| 38 | + response_json = self.__bitpay_client.post( |
| 39 | + "bills", bill.to_json(), sign_request |
| 40 | + ) |
| 41 | + except BitPayException as exe: |
| 42 | + raise BillCreationException( |
| 43 | + "failed to serialize bill object : %s" % str(exe), exe.get_api_code() |
| 44 | + ) |
| 45 | + |
| 46 | + try: |
| 47 | + return Bill(**response_json) |
| 48 | + except Exception as exe: |
| 49 | + raise BillCreationException( |
| 50 | + "failed to deserialize BitPay server response (Bill) : %s" % str(exe) |
| 51 | + ) |
| 52 | + |
| 53 | + def get(self, bill_id: str) -> Bill: |
| 54 | + """ |
| 55 | + Retrieve a BitPay bill by bill id using the specified facade. |
| 56 | +
|
| 57 | + :param str bill_id: The id of the bill to retrieve. |
| 58 | + :return: A BitPay Bill object. |
| 59 | + :rtype: Bill |
| 60 | + :raises BitPayException |
| 61 | + :raises BillQueryException |
| 62 | + """ |
| 63 | + try: |
| 64 | + params = {"token": self.__token_container.get_access_token(Facade.MERCHANT)} |
| 65 | + response_json = self.__bitpay_client.get("bills/%s" % bill_id, params) |
| 66 | + except BitPayException as exe: |
| 67 | + raise BillQueryException( |
| 68 | + "failed to serialize bill object : %s" % str(exe), exe.get_api_code() |
| 69 | + ) |
| 70 | + |
| 71 | + try: |
| 72 | + return Bill(**response_json) |
| 73 | + except Exception as exe: |
| 74 | + raise BillQueryException( |
| 75 | + "failed to deserialize BitPay server response" " (Bill) : %s" % str(exe) |
| 76 | + ) |
| 77 | + |
| 78 | + def get_bills(self, status: str = None) -> List[Bill]: |
| 79 | + """ |
| 80 | + Retrieve a collection of BitPay bills. |
| 81 | +
|
| 82 | + :param str status: The status to filter the bills. |
| 83 | + :return: A list of BitPay Bill objects. |
| 84 | + :rtype: [Bill] |
| 85 | + :raises BitPayException |
| 86 | + :raises BillQueryException |
| 87 | + """ |
| 88 | + try: |
| 89 | + params = {"token": self.__token_container.get_access_token(Facade.MERCHANT)} |
| 90 | + if status is not None: |
| 91 | + params["status"] = status |
| 92 | + response_json = self.__bitpay_client.get("bills", params, True) |
| 93 | + except BitPayException as exe: |
| 94 | + raise BillQueryException( |
| 95 | + "failed to serialize bill object : %s" % str(exe), exe.get_api_code() |
| 96 | + ) |
| 97 | + |
| 98 | + try: |
| 99 | + bills = [] |
| 100 | + for bill_data in response_json: |
| 101 | + bills.append(Bill(**bill_data)) |
| 102 | + except Exception as exe: |
| 103 | + raise BillQueryException( |
| 104 | + "failed to deserialize BitPay server response" " (Bill) : %s" % str(exe) |
| 105 | + ) |
| 106 | + |
| 107 | + return bills |
| 108 | + |
| 109 | + def update(self, bill: Bill, bill_id: str) -> Bill: |
| 110 | + """ |
| 111 | + Update a BitPay Bill. |
| 112 | +
|
| 113 | + :param Bill bill: A Bill object with the parameters to update defined. |
| 114 | + :param str bill_id: The Id of the Bill to update. |
| 115 | + :return: An updated Bill object. |
| 116 | + :rtype: Bill |
| 117 | + :raises BitPayException |
| 118 | + :raises BillUpdateException |
| 119 | + """ |
| 120 | + try: |
| 121 | + response_json = self.__bitpay_client.update( |
| 122 | + "bills/%s" % bill_id, bill.to_json() |
| 123 | + ) |
| 124 | + except BitPayException as exe: |
| 125 | + raise BillUpdateException( |
| 126 | + "failed to serialize bill object : %s" % str(exe), exe.get_api_code() |
| 127 | + ) |
| 128 | + |
| 129 | + try: |
| 130 | + return Bill(**response_json) |
| 131 | + except Exception as exe: |
| 132 | + raise BillUpdateException( |
| 133 | + "failed to deserialize BitPay server response" " (Bill) : %s" % str(exe) |
| 134 | + ) |
| 135 | + |
| 136 | + def deliver(self, bill_id: str, bill_token: str) -> bool: |
| 137 | + """ |
| 138 | + Deliver a BitPay Bill. |
| 139 | +
|
| 140 | + :param str bill_id: The id of the requested bill. |
| 141 | + :param str bill_token: The token of the requested bill. |
| 142 | + :return: A response status returned from the API. |
| 143 | + :rtype: bool |
| 144 | + :raises BitPayException |
| 145 | + :raises BillDeliveryException |
| 146 | + """ |
| 147 | + try: |
| 148 | + params = {"token": bill_token} |
| 149 | + response_json = self.__bitpay_client.post( |
| 150 | + "bills/%s" % bill_id + "/deliveries", params |
| 151 | + ) |
| 152 | + except BitPayException as exe: |
| 153 | + raise BillDeliveryException( |
| 154 | + "failed to serialize bill object : %s" % str(exe), exe.get_api_code() |
| 155 | + ) |
| 156 | + |
| 157 | + try: |
| 158 | + return response_json.lower() == "success" |
| 159 | + except Exception as exe: |
| 160 | + raise BillDeliveryException( |
| 161 | + "failed to deserialize BitPay server response" " (Bill) : %s" % str(exe) |
| 162 | + ) |
0 commit comments