diff --git a/duniterpy/api/ws2p/network.py b/duniterpy/api/ws2p/network.py new file mode 100644 index 0000000000000000000000000000000000000000..e39e6912b95a785af6facb5ace86547d5f405f03 --- /dev/null +++ b/duniterpy/api/ws2p/network.py @@ -0,0 +1,87 @@ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# Authors: +# Caner Candan <caner@candan.fr>, http://caner.candan.fr +# vit +import logging + +from duniterpy.api.client import Client + +logger = logging.getLogger("duniter/network") + +MODULE = 'network' + +WS2P_HEADS_SCHEMA = { + "type": "object", + "properties": { + "heads": { + "type": "array", + "items": { + "type": "object", + "properties": { + "message": { + "type": "string" + }, + "sig": { + "type": "string", + }, + "messageV2": { + "type": "string" + }, + "sigV2": { + "type": "string", + }, + "step": { + "type": "number", + }, + }, + "required": ["message", "sig"] + } + } + }, + "required": ["heads"] +} + + +def heads(client: Client): + """ + GET Certification data over a member + + :param client: Client to connect to the api + :rtype: dict + """ + return client.get(MODULE + '/ws2p/heads', schema=WS2P_HEADS_SCHEMA) + + +WS2P_CONNECT_MESSAGE_SCHEMA = { + "type": "object", + "properties": { + "auth": { + "type": "string" + }, + "challenge": { + "type": "string", + }, + "currency": { + "type": "string", + }, + "pub": { + "type": "string", + }, + "sig": { + "type": "string", + }, + } +} diff --git a/examples/request_web_socket_ws2p.py b/examples/request_web_socket_ws2p.py index 4b413199c6654eaeb7e31d49324d81dac8cafe1a..f4f5424586615384ff1e3238147da4dce3b1ef7d 100644 --- a/examples/request_web_socket_ws2p.py +++ b/examples/request_web_socket_ws2p.py @@ -4,7 +4,7 @@ from _socket import gaierror import aiohttp import jsonschema -from duniterpy.api import bma +from duniterpy.api import ws2p from duniterpy.api.client import Client, parse_text # CONFIG ####################################### @@ -12,7 +12,7 @@ from duniterpy.api.client import Client, parse_text # 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 WS2P API (WS2P) -WS2P_ENDPOINT = "WS2P 2f731dcd 127.0.0.1 35834" +WS2P_ENDPOINT = "WS2P 2f731dcd 127.0.0.1 20900" ################################################ @@ -27,7 +27,7 @@ async def main(): try: # Create Web Socket connection on block path - ws_connection = client.connect_ws('') + ws_connection = client.connect_ws() # From the documentation ws_connection should be a ClientWebSocketResponse object... # @@ -39,19 +39,20 @@ async def main(): # and close the ClientWebSocketResponse in it. connect_message = """ """ - ws_connection.send(connect_message) + #await ws_connection.send(connect_message) # 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") + print("Received a message") # Validate jsonschema and return a the json dict - block_data = parse_text(msg.data, bma.ws.WS_BLOCK_SCHEMA) - print(block_data) + data = parse_text(msg.data, ws2p.network.WS2P_CONNECT_MESSAGE_SCHEMA) + print(data) elif msg.type == aiohttp.WSMsgType.CLOSED: # Connection is closed print("Web socket connection closed !")