diff --git a/silkaj/cert.py b/silkaj/cert.py index 0acd5e69a2c6354e5ac4209ed2813f7fccfbc094..f6cb39036b46268cf469b69d056941f12aa06ab5 100644 --- a/silkaj/cert.py +++ b/silkaj/cert.py @@ -26,12 +26,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") @@ -80,12 +80,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: {0}".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 a118ac5f9cc10c1f964917b3d7c034064b64c97e..326aed869be30316bccae27b4289098d0abb70ab 100644 --- a/silkaj/membership.py +++ b/silkaj/membership.py @@ -28,7 +28,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( @@ -83,13 +83,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: {0}".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 f18b20f194ee26f73484a41cd1e42afa55523e0f..a447ebc9b3b868fd883f847a3b069da700b61f27 100644 --- a/silkaj/network_tools.py +++ b/silkaj/network_tools.py @@ -18,6 +18,7 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. import logging import re import socket +import urllib from sys import exit, stderr from duniterpy.api.client import Client @@ -173,3 +174,13 @@ def check_port(port): print("Wrong port number", file=stderr) return False return True + + +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 {lower(doc_name)}: {e}") diff --git a/silkaj/tx.py b/silkaj/tx.py index 529cbbdebc70c47e9142e2ed9f8d4c09eb6e8109..cdb7faf741161243471680d45f182c39e5c35411 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -405,14 +405,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: {0}".format(response.text()) - ) + nt.send_document(process, transaction) def display_sent_tx(outputAddress, amount):