From a86b1295ee9e48ed281a572ea97c7e94d98ad7fb Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Fri, 14 May 2021 13:28:57 +0200
Subject: [PATCH] [ref] Rework send-documents examples since urllib usage
 switch

---
 examples/send_certification.py | 11 +++++------
 examples/send_identity.py      | 10 +++++-----
 examples/send_membership.py    | 11 +++++------
 examples/send_transaction.py   | 11 +++++------
 4 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/examples/send_certification.py b/examples/send_certification.py
index 54bf2b7d..5c9919d8 100644
--- a/examples/send_certification.py
+++ b/examples/send_certification.py
@@ -17,6 +17,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import getpass
 from typing import Optional
+import urllib
 
 from duniterpy.api import bma
 from duniterpy.api.client import Client
@@ -138,12 +139,10 @@ def send_certification():
     certification.sign([key])
 
     # Here we request for the path wot/certify
-    response = client(bma.wot.certify, certification.signed_raw())
-
-    if response.status == 200:
-        print(response.read())
-    else:
-        print("Error while publishing certification: {0}".format(response.read()))
+    try:
+        client(bma.wot.certify, certification.signed_raw())
+    except urllib.error.HTTPError as e:
+        print(f"Error while publishing certification: {e}")
 
 
 if __name__ == "__main__":
diff --git a/examples/send_identity.py b/examples/send_identity.py
index 2bdc9874..fbbb1723 100644
--- a/examples/send_identity.py
+++ b/examples/send_identity.py
@@ -16,6 +16,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
 import getpass
+import urllib
 
 from duniterpy.api import bma
 from duniterpy.api.client import Client
@@ -97,11 +98,10 @@ def send_identity():
     identity = get_identity_document(current_block, uid, key)
 
     # send the identity signed raw document to the node
-    response = client(bma.wot.add, identity.signed_raw())
-    if response.status == 200:
-        print(response.read())
-    else:
-        print("Error while publishing identity : {0}".format(response.read()))
+    try:
+        client(bma.wot.add, identity.signed_raw())
+    except urllib.error.HTTPError as e:
+        print(f"Error while publishing identity: {e}")
 
 
 if __name__ == "__main__":
diff --git a/examples/send_membership.py b/examples/send_membership.py
index 2506c9f4..ae1bb695 100644
--- a/examples/send_membership.py
+++ b/examples/send_membership.py
@@ -16,6 +16,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
 import getpass
+import urllib
 
 from duniterpy.api import bma
 from duniterpy.api.client import Client
@@ -106,12 +107,10 @@ def send_membership():
     membership = get_membership_document("IN", current_block, identity, key)
 
     # send the membership signed raw document to the node
-    response = client(bma.blockchain.membership, membership.signed_raw())
-
-    if response.status == 200:
-        print(response.text())
-    else:
-        print("Error while publishing membership : {0}".format(response.text()))
+    try:
+        client(bma.blockchain.membership, membership.signed_raw())
+    except urllib.error.HTTPError as e:
+        print(f"Error while publishing membership: {e}")
 
 
 if __name__ == "__main__":
diff --git a/examples/send_transaction.py b/examples/send_transaction.py
index 5a6792c0..50e5f278 100644
--- a/examples/send_transaction.py
+++ b/examples/send_transaction.py
@@ -16,6 +16,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
 import getpass
+import urllib
 
 from duniterpy.api import bma
 from duniterpy.api.client import Client
@@ -150,12 +151,10 @@ def send_transaction():
     transaction.sign([key])
 
     # send the Transaction document to the node
-    response = client(bma.tx.process, transaction.signed_raw())
-
-    if response.status == 200:
-        print(response.read())
-    else:
-        print("Error while publishing transaction: {0}".format(response.read()))
+    try:
+        client(bma.tx.process, transaction.signed_raw())
+    except urllib.error.HTTPError as e:
+        print(f"Error while publishing transaction: {e}")
 
 
 if __name__ == "__main__":
-- 
GitLab