Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DuniterPy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clients
python
DuniterPy
Commits
279295fe
Commit
279295fe
authored
6 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
[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
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
duniterpy/api/ws2p/network.py
+87
-0
87 additions, 0 deletions
duniterpy/api/ws2p/network.py
examples/request_web_socket_ws2p.py
+8
-7
8 additions, 7 deletions
examples/request_web_socket_ws2p.py
with
95 additions
and
7 deletions
duniterpy/api/ws2p/network.py
0 → 100644
+
87
−
0
View file @
279295fe
#
# 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
"
,
},
}
}
This diff is collapsed.
Click to expand it.
examples/request_web_socket_ws2p.py
+
8
−
7
View file @
279295fe
...
@@ -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 !
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment