Commit 2c0f6891 authored by Vincent Texier's avatar Vincent Texier

issue #56 WIP - WS2P code and tests updated and example removed

Duniterpy should not support WS2P.
parent e00c1eb6
Pipeline #2603 passed with stages
in 3 minutes and 35 seconds
......@@ -62,4 +62,4 @@ def heads(client: Client):
:param client: Client to connect to the api
:rtype: dict
"""
return client.connect_ws(MODULE + '/ws2p/heads')
return client.get(MODULE + '/ws2p/heads', schema=WS2P_HEADS_SCHEMA)
......@@ -59,7 +59,7 @@ class Document:
logging.debug("Signature : \n{0}".format(signing.decode("ascii")))
self.signatures.append(signing.decode("ascii"))
def raw(self):
def raw(self, **kwargs):
"""
Returns the raw document in string format
"""
......
import asyncio
from _socket import gaierror
import aiohttp
import jsonschema
from duniterpy.api.client import Client, parse_text
from duniterpy.api import ws2p
# CONFIG #######################################
# You can either use a complete defined endpoint : [NAME_OF_THE_API] [DOMAIN] [IPv4] [IPv6] [PORT]
# or the simple definition : [NAME_OF_THE_API] [DOMAIN] [PORT]
# Here we use the secure BASIC_MERKLED_API (BMAS)
WS2P_ENDPOINT = "WS2P id ip port"
################################################
async def main():
"""
Main code (synchronous requests)
"""
# Create Client from endpoint string in Duniter format
client = Client(WS2P_ENDPOINT)
# Get the node summary infos by dedicated method (with json schema validation)
print("\nCall ws2p.heads:")
try:
# Create Web Socket connection on block path
ws_connection = client(ws2p.network.heads)
# From the documentation ws_connection should be a ClientWebSocketResponse object...
#
# https://docs.aiohttp.org/en/stable/client_quickstart.html#websockets
#
# In reality, aiohttp.session.ws_connect() returns a aiohttp.client._WSRequestContextManager instance.
# It must be used in a with statement to get the ClientWebSocketResponse instance from it (__aenter__).
# At the end of the with statement, aiohttp.client._WSRequestContextManager.__aexit__ is called
# and close the ClientWebSocketResponse in it.
# Mandatory to get the "for msg in ws" to work !
async with ws_connection as ws:
print("Connected successfully to web socket block path")
# Iterate on each message received...
async for msg in ws:
# if message type is text...
if msg.type == aiohttp.WSMsgType.TEXT:
print("Received a block")
# Validate jsonschema and return a the json dict
block_data = parse_text(msg.data, ws2p.network.WS2P_HEADS_SCHEMA)
print(block_data)
elif msg.type == aiohttp.WSMsgType.CLOSED:
# Connection is closed
print("Web socket connection closed !")
elif msg.type == aiohttp.WSMsgType.ERROR:
# Connection error
print("Web socket connection error !")
# Close session
await client.close()
except (aiohttp.WSServerHandshakeError, ValueError) as e:
print("Websocket block {0} : {1}".format(type(e).__name__, str(e)))
except (aiohttp.ClientError, gaierror, TimeoutError) as e:
print("{0} : {1}".format(str(e), WS2P_ENDPOINT))
except jsonschema.ValidationError as e:
print("{:}:{:}".format(str(e.__class__.__name__), str(e)))
# Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data.
# ( https://docs.python.org/3/library/asyncio.html )
asyncio.get_event_loop().run_until_complete(main())
import unittest
from duniterpy.documents.peer import Peer, BMAEndpoint, UnknownEndpoint, WS2PEndpoint
from duniterpy.api.endpoint import BMAEndpoint, UnknownEndpoint, WS2PEndpoint
from duniterpy.documents.peer import Peer
rawpeer = """Version: 2
Type: Peer
......@@ -15,7 +16,6 @@ OTHER_PROTOCOL 88.77.66.55 9001
dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==
"""
test_weird_ipv6_peer = """Version: 10
Type: Peer
Currency: g1
......@@ -54,7 +54,8 @@ class TestPeer(unittest.TestCase):
self.assertEqual(peer.endpoints[2].ws2pid, "d2edcb92")
self.assertEqual(peer.endpoints[2].port, 20902)
self.assertEqual(peer.signatures[0], "dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==")
self.assertEqual(peer.signatures[0],
"dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==")
def test_fromraw_toraw(self):
peer = Peer.from_signed_raw(rawpeer)
......@@ -84,11 +85,11 @@ class TestPeer(unittest.TestCase):
self.assertEqual(peer.endpoints[2].ws2pid, "d2edcb92")
self.assertEqual(peer.endpoints[2].port, 20902)
self.assertEqual(from_rendered_peer.signatures[0], "dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==")
self.assertEqual(from_rendered_peer.signatures[0],
"dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==")
self.assertEqual(rawpeer, from_rendered_peer.signed_raw())
def test_incorrect(self):
peer = Peer.from_signed_raw(test_weird_ipv6_peer)
rendered_peer = peer.signed_raw()
from_rendered_peer = Peer.from_signed_raw(rendered_peer)
Peer.from_signed_raw(rendered_peer)
import unittest
from duniterpy.documents.ws2p.heads import *
from duniterpy.documents.ws2p.heads import *
headv1_clear = ""
......@@ -12,15 +12,15 @@ headv2 = ""
class TestWS2PHeads(unittest.TestCase):
def test_headv0(self):
headv0, _ = HeadV0.from_inline("WS2P:HEAD:3dnbnYY9i2bHMQUGyFp5GVvJ2wBkVpus31cDJA5cfRpj:"
"54813-00000A24802B33B71A91B6E990038C145A4815A45C71E57B2F2EF393183C7E2C",
"a1vAAM666kPsMCFTbkgkcCsqHf8nmXR+Lh3D3u+BaXzmArj7kwlItbdGUs4fc9QUG5Lp4TwPS7nhOM5t1Kt6CA==")
"54813-00000A24802B33B71A91B6E990038C145A4815A45C71E57B2F2EF393183C7E2C",
"a1vAAM666kPsMCFTbkgkcCsqHf8nmXR+Lh3D3u+BaXzmArj7kwlItbdGUs4fc9QUG5Lp4TwPS7nhOM5t1Kt6CA==")
self.assertEqual(headv0.api.public, "")
self.assertEqual(headv0.api.private, "")
self.assertEqual(headv0.head.version, 0)
self.assertEqual(headv0.pubkey, "3dnbnYY9i2bHMQUGyFp5GVvJ2wBkVpus31cDJA5cfRpj")
self.assertEqual(headv0.blockstamp, BlockUID.from_str("54813-00000A24802B33B71A91B6E990038C145A4815A45C71E57B2F2EF393183C7E2C"))
self.assertEqual(headv0.blockstamp,
BlockUID.from_str("54813-00000A24802B33B71A91B6E990038C145A4815A45C71E57B2F2EF393183C7E2C"))
def test_ws2p_headv1(self):
headv1, _ = HeadV1.from_inline("WS2POCAIC:HEAD:1:HbTqJ1Ts3RhJ8Rx4XkNyh1oSKmoZL1kY5U7t9mKTSjAB:"
......
from duniterpy.key import VerifyingKey, SigningKey, ScryptParams
from duniterpy.documents import Peer
from duniterpy.documents.peer import Peer
from duniterpy.documents.ws2p.heads import *
import unittest
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment