|
9 | 9 | import json |
10 | 10 |
|
11 | 11 | from .config import Config |
12 | | -from .exceptions.invoice_creation_exception import InvoiceCreationException |
13 | 12 | from .tokens import Tokens |
14 | 13 | from .models.facade import Facade |
15 | 14 | from .models.bill.bill import Bill |
|
20 | 19 | from .models.invoice.invoice import Invoice |
21 | 20 | from .models.ledger.ledger_entry import LedgerEntry |
22 | 21 | from .exceptions.bitpay_exception import BitPayException |
| 22 | +from .models.payout.payout_recipient import PayoutRecipient |
| 23 | +from .models.payout.payout_recipients import PayoutRecipients |
23 | 24 | from .exceptions.bill_query_exception import BillQueryException |
24 | 25 | from .exceptions.bill_update_exception import BillUpdateException |
25 | 26 | from .exceptions.ledger_query_exception import LedgerQueryException |
|
31 | 32 | from .exceptions.invoice_query_exception import InvoiceQueryException |
32 | 33 | from .exceptions.invoice_update_exception import InvoiceUpdateException |
33 | 34 | from .exceptions.refund_creation_exception import RefundCreationException |
| 35 | +from .exceptions.invoice_creation_exception import InvoiceCreationException |
34 | 36 | from .exceptions.refund_notification_exception import RefundNotificationException |
35 | 37 | from .exceptions.refund_cancellation_exception import RefundCancellationException |
36 | 38 | from .exceptions.invoice_cancellation_exception import InvoiceCancellationException |
37 | 39 | from .exceptions.invoice_notification_exception import InvoiceNotificationException |
| 40 | +from .exceptions.payout_recipient_query_exception import PayoutRecipientQueryException |
| 41 | +from .exceptions.payout_recipient_update_exception import PayoutRecipientUpdateException |
| 42 | +from .exceptions.payout_recipient_creation_exception import PayoutRecipientCreationException |
| 43 | +from .exceptions.payout_recipient_cancellation_exception import PayoutRecipientCancellationException |
| 44 | +from .exceptions.payout_recipient_notification_exception import PayoutRecipientNotificationException |
38 | 45 |
|
39 | 46 |
|
40 | 47 | class Client: |
@@ -100,6 +107,7 @@ def build_config(self, private_key_path: str, tokens: Tokens): |
100 | 107 | read_file = open(private_key_path, 'r') |
101 | 108 | plain_private_key = read_file.read() |
102 | 109 | self.__ec_key = plain_private_key |
| 110 | + read_file.close() |
103 | 111 | else: |
104 | 112 | raise BitPayException("Private Key file not found") |
105 | 113 |
|
@@ -132,7 +140,10 @@ def init_keys(self): |
132 | 140 |
|
133 | 141 | def init(self): |
134 | 142 | try: |
135 | | - proxy = self.__configuration.get_envconfig()[self.__env]["proxy"] |
| 143 | + proxy = None |
| 144 | + if "proxy" in self.__configuration.get_envconfig()[self.__env]: |
| 145 | + proxy = self.__configuration.get_envconfig()[self.__env]["proxy"] |
| 146 | + |
136 | 147 | self.__restcli = RESTcli(self.__env, self.__ec_key, proxy) |
137 | 148 | self.load_access_tokens() |
138 | 149 | self.__currencies_info = self.load_currencies() |
@@ -754,3 +765,102 @@ def get_ledgers(self) -> [Ledger]: |
754 | 765 | raise LedgerQueryException("failed to deserialize BitPay server response" |
755 | 766 | " (Ledger) : %s" % str(exe)) |
756 | 767 | return ledgers |
| 768 | + |
| 769 | + def submit_payout_recipients(self, recipients: PayoutRecipients) -> [PayoutRecipient]: |
| 770 | + try: |
| 771 | + recipients.set_token(self.get_access_token(Facade.Payout)) |
| 772 | + recipients.to_json() |
| 773 | + |
| 774 | + response_json = self.__restcli.post("recipients", recipients, True) |
| 775 | + except BitPayException as exe: |
| 776 | + raise PayoutRecipientCreationException("failed to serialize PayoutRecipients object : %s" % str(exe), |
| 777 | + exe.get_api_code()) |
| 778 | + |
| 779 | + try: |
| 780 | + payout_recipients = PayoutRecipient(**response_json) |
| 781 | + except Exception as exe: |
| 782 | + raise PayoutRecipientCreationException("failed to deserialize BitPay server response " |
| 783 | + " (PayoutRecipients) : %s" % str(exe)) |
| 784 | + return payout_recipients |
| 785 | + |
| 786 | + def get_payout_recipient(self, recipient_id: str) -> PayoutRecipient: |
| 787 | + try: |
| 788 | + params = {"token": self.get_access_token(Facade.Payout)} |
| 789 | + response_json = self.__restcli.get("recipients/%s" % recipient_id, params) |
| 790 | + except BitPayException as exe: |
| 791 | + raise PayoutRecipientQueryException("failed to serialize PayoutRecipients object : %s" % str(exe), |
| 792 | + exe.get_api_code()) |
| 793 | + |
| 794 | + try: |
| 795 | + payout_recipient = PayoutRecipient(**response_json) |
| 796 | + except Exception as exe: |
| 797 | + raise PayoutRecipientQueryException("failed to deserialize BitPay server response " |
| 798 | + " (PayoutRecipients) : %s" % str(exe)) |
| 799 | + return payout_recipient |
| 800 | + |
| 801 | + def get_payout_recipients(self, status, limit=100, offset=0) -> [PayoutRecipient]: |
| 802 | + try: |
| 803 | + params = {"token": self.get_access_token(Facade.Payout), |
| 804 | + "limit": str(limit), "offset": str(offset)} |
| 805 | + if status: |
| 806 | + params["status"] = status |
| 807 | + |
| 808 | + response_json = self.__restcli.get("recipients", params) |
| 809 | + except BitPayException as exe: |
| 810 | + raise PayoutRecipientQueryException("failed to serialize PayoutRecipients object : %s" % str(exe), |
| 811 | + exe.get_api_code()) |
| 812 | + |
| 813 | + try: |
| 814 | + payout_recipients = [] |
| 815 | + for payout_recipient in response_json: |
| 816 | + payout_recipients.append(PayoutRecipient(**payout_recipient)) |
| 817 | + except Exception as exe: |
| 818 | + raise PayoutRecipientQueryException("failed to deserialize BitPay server response " |
| 819 | + " (PayoutRecipients) : %s" % str(exe)) |
| 820 | + return payout_recipients |
| 821 | + |
| 822 | + def update_payout_recipient(self, recipient_id, recipient: PayoutRecipient) -> PayoutRecipient: |
| 823 | + try: |
| 824 | + recipient.set_token(self.get_access_token(Facade.Payout)) |
| 825 | + |
| 826 | + response_json = self.__restcli.update("recipients/%s" % recipient_id, recipient.to_json()) |
| 827 | + except BitPayException as exe: |
| 828 | + raise PayoutRecipientUpdateException("failed to serialize PayoutRecipients object : %s" % str(exe), |
| 829 | + exe.get_api_code()) |
| 830 | + |
| 831 | + try: |
| 832 | + payout_recipient = PayoutRecipient(**response_json) |
| 833 | + except Exception as exe: |
| 834 | + raise PayoutRecipientUpdateException("failed to deserialize BitPay server response " |
| 835 | + " (PayoutRecipients) : %s" % str(exe)) |
| 836 | + return payout_recipient |
| 837 | + |
| 838 | + def delete_payout_recipient(self, recipient_id) -> bool: |
| 839 | + try: |
| 840 | + params = {"token": self.get_access_token(Facade.Payout)} |
| 841 | + response_json = self.__restcli.delete("recipients/%s" % recipient_id, params) |
| 842 | + except BitPayException as exe: |
| 843 | + raise PayoutRecipientCancellationException("failed to serialize PayoutRecipients object : %s" % str(exe), |
| 844 | + exe.get_api_code()) |
| 845 | + |
| 846 | + try: |
| 847 | + payout_recipient = PayoutRecipient(**response_json) |
| 848 | + except Exception as exe: |
| 849 | + raise PayoutRecipientCancellationException("failed to deserialize BitPay server response " |
| 850 | + " (PayoutRecipients) : %s" % str(exe)) |
| 851 | + return payout_recipient |
| 852 | + |
| 853 | + def request_payout_recipient_notification(self, recipient_id) -> bool: |
| 854 | + try: |
| 855 | + params = {"token": self.get_access_token(Facade.Payout)} |
| 856 | + response_json = self.__restcli.post("recipients/%s" % recipient_id + "/notifications", params) |
| 857 | + except BitPayException as exe: |
| 858 | + raise PayoutRecipientNotificationException("failed to serialize PayoutRecipients object : %s" % str(exe), |
| 859 | + exe.get_api_code()) |
| 860 | + |
| 861 | + try: |
| 862 | + payout_recipient = PayoutRecipient(**response_json) |
| 863 | + except Exception as exe: |
| 864 | + raise PayoutRecipientNotificationException("failed to deserialize BitPay server response " |
| 865 | + " (PayoutRecipients) : %s" % str(exe)) |
| 866 | + return payout_recipient |
0 commit comments