From 31f7dfd46dcc5ea628bd5d598ed061e77675ccfb 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 | 11 +++++++++++
 silkaj/tx.py            |  9 +--------
 4 files changed, 17 insertions(+), 24 deletions(-)

diff --git a/silkaj/cert.py b/silkaj/cert.py
index 0acd5e69..f6cb3903 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 a118ac5f..326aed86 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 f18b20f1..a447ebc9 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 529cbbde..cdb7faf7 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):
-- 
GitLab