From ec143800a8126db2d97bcd0108750a3942c22446 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Sun, 2 May 2021 16:20:30 +0200
Subject: [PATCH] [enh] #390, #396: Implement generic send_document()

Use it in cert, membership, and tx cmds
With urllib, the 200 error is no longer returned but an exception
TODO: write test_send_document()
---
 silkaj/cert.py          | 11 +++--------
 silkaj/membership.py    | 10 ++--------
 silkaj/network_tools.py | 12 ++++++++++++
 silkaj/tx.py            |  9 +--------
 4 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/silkaj/cert.py b/silkaj/cert.py
index ba040780..a828f5d1 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 93a901e9..ccb06635 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 c2f6fe42..149e9593 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 727e81e5..00e70cee 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):
-- 
GitLab