From 4ad9109e108452f9848aeb760493b5528983a4d2 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Wed, 15 Jan 2020 08:05:21 +0100
Subject: [PATCH] [enh] #58: Use generate_ws2p_endpoint() helper in examples

- Remove comments
---
 examples/listen_ws2p.py  | 16 +++++++++-------
 examples/request_ws2p.py | 15 +++++++++------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/examples/listen_ws2p.py b/examples/listen_ws2p.py
index 9a12216f..5e2884cc 100644
--- a/examples/listen_ws2p.py
+++ b/examples/listen_ws2p.py
@@ -9,7 +9,7 @@ from jsonschema import ValidationError
 
 from duniterpy.key import SigningKey
 
-from duniterpy.helpers.ws2p import handshake
+from duniterpy.helpers.ws2p import handshake, generate_ws2p_endpoint
 from duniterpy.api.client import Client
 
 # CONFIG #######################################
@@ -17,10 +17,7 @@ from duniterpy.api.client import Client
 # You can either use a complete defined endpoint : [NAME_OF_THE_API] [DOMAIN] [IPv4] [IPv6] [PORT] [PATH]
 # or the simple definition : [NAME_OF_THE_API] [DOMAIN] [PORT] [PATH]
 # Here we use the WS2P API (WS2P [UUID] [DOMAIN] [PORT] [PATH])
-# You can find the UUID of a node with the /network/ws2p/heads BMA API request,
-# using the UUID of the HEAD with step 0
-# or in your node user interface in the network view in the WS2PID column
-WS2P_ENDPOINT = "WS2P 96675302 g1-test.duniter.org 443"
+BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443"
 CURRENCY = "g1-test"
 
 
@@ -38,7 +35,12 @@ async def main():
     signing_key = SigningKey.from_credentials(salt, password)
 
     # Create Client from endpoint string in Duniter format
-    client = Client(WS2P_ENDPOINT)
+    try:
+        ws2p_endpoint = await generate_ws2p_endpoint(BMAS_ENDPOINT)
+    except ValueError as e:
+        print(e)
+        return
+    client = Client(ws2p_endpoint)
 
     try:
         # Create a Web Socket connection
@@ -73,7 +75,7 @@ async def main():
     except (aiohttp.WSServerHandshakeError, ValueError) as e:
         print("Websocket handshake {0} : {1}".format(type(e).__name__, str(e)))
     except (aiohttp.ClientError, gaierror, TimeoutError) as e:
-        print("{0} : {1}".format(str(e), WS2P_ENDPOINT))
+        print("{0} : {1}".format(str(e), ws2p_endpoint.inline()))
     except jsonschema.ValidationError as e:
         print("{:}:{:}".format(str(e.__class__.__name__), str(e)))
 
diff --git a/examples/request_ws2p.py b/examples/request_ws2p.py
index f6fbf00e..545a1951 100644
--- a/examples/request_ws2p.py
+++ b/examples/request_ws2p.py
@@ -12,7 +12,7 @@ from typing import Any
 from duniterpy.tools import get_ws2p_challenge
 from duniterpy.key import SigningKey
 
-from duniterpy.helpers.ws2p import handshake
+from duniterpy.helpers.ws2p import handshake, generate_ws2p_endpoint
 from duniterpy.api.ws2p import requests
 from duniterpy.api.client import Client, WSConnection
 
@@ -21,9 +21,7 @@ from duniterpy.api.client import Client, WSConnection
 # You can either use a complete defined endpoint : [NAME_OF_THE_API] [DOMAIN] [IPv4] [IPv6] [PORT] [PATH]
 # or the simple definition : [NAME_OF_THE_API] [DOMAIN] [PORT] [PATH]
 # Here we use the WS2P API (WS2P [UUID] [DOMAIN] [PORT] [PATH])
-# You can find the UUID of a node with the /network/ws2p/heads BMA API request
-# or in your node user interface in the network view in the WS2PID column
-WS2P_ENDPOINT = "WS2P 96675302 g1-test.duniter.org 443"
+BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443"
 CURRENCY = "g1-test"
 
 
@@ -86,7 +84,12 @@ async def main():
     signing_key = SigningKey.from_credentials(salt, password)
 
     # Create Client from endpoint string in Duniter format
-    client = Client(WS2P_ENDPOINT)
+    try:
+        ws2p_endpoint = await generate_ws2p_endpoint(BMAS_ENDPOINT)
+    except ValueError as e:
+        print(e)
+        return
+    client = Client(ws2p_endpoint)
 
     try:
         # Create a Web Socket connection
@@ -152,7 +155,7 @@ async def main():
     except (aiohttp.WSServerHandshakeError, ValueError) as e:
         print("Websocket handshake {0} : {1}".format(type(e).__name__, str(e)))
     except (aiohttp.ClientError, gaierror, TimeoutError) as e:
-        print("{0} : {1}".format(str(e), WS2P_ENDPOINT))
+        print("{0} : {1}".format(str(e), ws2p_endpoint.inline()))
     except jsonschema.ValidationError as e:
         print("{:}:{:}".format(str(e.__class__.__name__), str(e)))
 
-- 
GitLab