diff --git a/silkaj/cert.py b/silkaj/cert.py index ba040780a07b4c4bb184b206ef2014e81d953fd5..a828f5d1781a38378dec9252c4c4ec4dffcaca5f 100644 --- a/silkaj/cert.py +++ b/silkaj/cert.py @@ -24,12 +24,12 @@ from tabulate import tabulate from silkaj import tui, wot from silkaj import wot_tools as wt from silkaj.auth import auth_method -from silkaj.tools import message_exit -from silkaj.network_tools import ClientInstance from silkaj.blockchain_tools import BlockchainParams, HeadBlock from silkaj.constants import ALL, DATE, SUCCESS_EXIT_STATUS from silkaj.crypto_tools import is_pubkey_and_check from silkaj.license import license_approval +from silkaj.network_tools import ClientInstance, send_document +from silkaj.tools import message_exit @click.command("cert", help="Send certification") @@ -78,12 +78,7 @@ def send_certification(ctx, uid_pubkey_to_certify): certification.sign([key]) # Send certification document - response = client(bma.wot.certify, certification.signed_raw()) - - if response.status == 200: - print("Certification successfully sent.") - else: - print("Error while publishing certification: {}".format(response.text())) + send_document(bma.wot.certify, certification) def pre_checks(client, issuer_pubkey, pubkey_to_certify): diff --git a/silkaj/membership.py b/silkaj/membership.py index 93a901e996a755e4718facdd5d0b7a48456d5795..ccb06635361084edb6db0231edae519647161b17 100644 --- a/silkaj/membership.py +++ b/silkaj/membership.py @@ -26,7 +26,7 @@ from silkaj import auth, tui, wot from silkaj.blockchain_tools import BlockchainParams, HeadBlock from silkaj.constants import DATE, SUCCESS_EXIT_STATUS from silkaj.license import license_approval -from silkaj.network_tools import ClientInstance +from silkaj.network_tools import ClientInstance, send_document @click.command( @@ -81,13 +81,7 @@ def send_membership(ctx): tui.send_doc_confirmation("membership document for this identity") # Send the membership signed raw document to the node - response = client(bma.blockchain.membership, membership.signed_raw()) - - if response.status == 200: - print("Membership successfully sent") - else: - print("Error while publishing membership: {}".format(response.text())) - logging.debug(response.text()) + send_document(bma.blockchain.membership, membership) def display_confirmation_table(identity_uid, pubkey, identity_timestamp): diff --git a/silkaj/network_tools.py b/silkaj/network_tools.py index c2f6fe42ae24e2101e0a39adb19a0131e14d29a3..149e959398388fa8eb8a9e83d9000d80f8508290 100644 --- a/silkaj/network_tools.py +++ b/silkaj/network_tools.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU Affero General Public License # along with Silkaj. If not, see <https://www.gnu.org/licenses/>. +import urllib + from duniterpy.api.client import Client from silkaj.constants import G1_DEFAULT_ENDPOINT, G1_TEST_DEFAULT_ENDPOINT @@ -63,3 +65,13 @@ class EndPoint: class ClientInstance: def __init__(self): self.client = Client(EndPoint().BMA_ENDPOINT) + + +def send_document(bma_path, document): + client = ClientInstance().client + doc_name = document.__class__.__name__ + try: + client(bma_path, document.signed_raw()) + print(f"{doc_name} successfully sent") + except urllib.error.HTTPError as e: + print(f"Error while publishing {doc_name.lower()}: {e}") diff --git a/silkaj/tx.py b/silkaj/tx.py index 727e81e537c41a177e28eecf320222b88c1854f4..00e70cee25e24fac51134d86fcc1f566f3f40225 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -403,14 +403,7 @@ def generate_and_send_transaction( OutputbackChange, ) transaction.sign([key]) - - response = client(process, transaction.signed_raw()) - if response.status == 200: - print("Transaction successfully sent.") - else: - tools.message_exit( - "Error while publishing transaction: {}".format(response.text()) - ) + nt.send_document(process, transaction) def display_sent_tx(outputAddress, amount):