diff --git a/duniterpy/api/ws2p/network.py b/duniterpy/api/ws2p/network.py index a5c37808c0d5b9d9961ac4cb1bcddebf3305d3c7..ec9b3a149309083f077926aca1cb5cf79982103a 100644 --- a/duniterpy/api/ws2p/network.py +++ b/duniterpy/api/ws2p/network.py @@ -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) diff --git a/duniterpy/documents/document.py b/duniterpy/documents/document.py index bced25b4486189dab1496fb711dd54c9d72d45b8..51de8094851b8cdaa52e6fc808a154a3dcebed06 100644 --- a/duniterpy/documents/document.py +++ b/duniterpy/documents/document.py @@ -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 """ diff --git a/examples/request_ws2p.py b/examples/request_ws2p.py deleted file mode 100644 index 095215d124b3e23222e772fa93134a19d298e298..0000000000000000000000000000000000000000 --- a/examples/request_ws2p.py +++ /dev/null @@ -1,73 +0,0 @@ -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()) diff --git a/tests/documents/test_peer.py b/tests/documents/test_peer.py index 3b491146feee1d1dcd9ece205c51f5275b32d1ee..192cb052f1aa4395bb45044c9742fd03ea4f2457 100644 --- a/tests/documents/test_peer.py +++ b/tests/documents/test_peer.py @@ -1,6 +1,7 @@ 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) diff --git a/tests/documents/test_ws2p_heads.py b/tests/documents/test_ws2p_heads.py index 3e07810e09880fe93b0d0b917888161d2c7a87cd..579c88c162c13a05aaec1af8631c426681de6c52 100644 --- a/tests/documents/test_ws2p_heads.py +++ b/tests/documents/test_ws2p_heads.py @@ -1,6 +1,6 @@ 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:" diff --git a/tests/key/test_verifying_key.py b/tests/key/test_verifying_key.py index e7b4d0bbdee5e5b2809aade9d5a4fc5e28025732..3affd4f0c67292480caa57bbe0536f3f54bc1fdc 100644 --- a/tests/key/test_verifying_key.py +++ b/tests/key/test_verifying_key.py @@ -1,5 +1,5 @@ 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