Skip to content
Snippets Groups Projects
Commit 279295fe authored by Vincent Texier's avatar Vincent Texier
Browse files

[enh] #58 new network module for ws2p API

Add jsonschema CONNECT message validator
parent db2af4a8
No related branches found
No related tags found
No related merge requests found
#
# 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",
},
}
}
...@@ -4,7 +4,7 @@ from _socket import gaierror ...@@ -4,7 +4,7 @@ from _socket import gaierror
import aiohttp import aiohttp
import jsonschema import jsonschema
from duniterpy.api import bma from duniterpy.api import ws2p
from duniterpy.api.client import Client, parse_text from duniterpy.api.client import Client, parse_text
# CONFIG ####################################### # CONFIG #######################################
...@@ -12,7 +12,7 @@ from duniterpy.api.client import Client, parse_text ...@@ -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] # 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] # or the simple definition : [NAME_OF_THE_API] [DOMAIN] [PORT]
# Here we use the WS2P API (WS2P) # 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(): ...@@ -27,7 +27,7 @@ async def main():
try: try:
# Create Web Socket connection on block path # 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... # From the documentation ws_connection should be a ClientWebSocketResponse object...
# #
...@@ -39,19 +39,20 @@ async def main(): ...@@ -39,19 +39,20 @@ async def main():
# and close the ClientWebSocketResponse in it. # and close the ClientWebSocketResponse in it.
connect_message = """ connect_message = """
""" """
ws_connection.send(connect_message) #await ws_connection.send(connect_message)
# Mandatory to get the "for msg in ws" to work ! # Mandatory to get the "for msg in ws" to work !
async with ws_connection as ws: async with ws_connection as ws:
print("Connected successfully to web socket block path") print("Connected successfully to web socket block path")
# Iterate on each message received... # Iterate on each message received...
async for msg in ws: async for msg in ws:
# if message type is text... # if message type is text...
if msg.type == aiohttp.WSMsgType.TEXT: if msg.type == aiohttp.WSMsgType.TEXT:
print("Received a block") print("Received a message")
# Validate jsonschema and return a the json dict # Validate jsonschema and return a the json dict
block_data = parse_text(msg.data, bma.ws.WS_BLOCK_SCHEMA) data = parse_text(msg.data, ws2p.network.WS2P_CONNECT_MESSAGE_SCHEMA)
print(block_data) print(data)
elif msg.type == aiohttp.WSMsgType.CLOSED: elif msg.type == aiohttp.WSMsgType.CLOSED:
# Connection is closed # Connection is closed
print("Web socket connection closed !") print("Web socket connection closed !")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment