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
b4bc62c3
Commit
b4bc62c3
authored
5 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
[fix]
#58
fix request_web_socket_block example
parent
9e532d2c
No related branches found
No related tags found
No related merge requests found
Pipeline
#7078
failed
5 years ago
Stage: format
Stage: test
Stage: build
Stage: release
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/request_web_socket_block.py
+15
-33
15 additions, 33 deletions
examples/request_web_socket_block.py
with
15 additions
and
33 deletions
examples/request_web_socket_block.py
+
15
−
33
View file @
b4bc62c3
import
asyncio
import
json
from
_socket
import
gaierror
import
aiohttp
import
jsonschema
from
duniterpy.api
import
bma
from
duniterpy.api.client
import
Client
,
parse_text
from
duniterpy.api.client
import
Client
# CONFIG #######################################
...
...
@@ -26,38 +27,19 @@ async def main():
client
=
Client
(
BMAS_ENDPOINT
)
try
:
# Create Web Socket connection on block path
ws_connection
=
client
(
bma
.
ws
.
block
)
# 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
,
bma
.
ws
.
WS_BLOCK_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
()
# Create Web Socket connection on block path (async method)
ws
=
await
client
(
bma
.
ws
.
block
)
# Type: WSConnection
print
(
"
Connected successfully to web socket block path
"
)
data
=
await
ws
.
receive_json
()
jsonschema
.
validate
(
data
,
bma
.
ws
.
WS_BLOCK_SCHEMA
)
print
(
"
Received a block
"
)
print
(
json
.
dumps
(
data
,
indent
=
2
))
# Close session
await
client
.
close
()
except
(
aiohttp
.
WSServerHandshakeError
,
ValueError
)
as
e
:
print
(
"
Websocket block {0} : {1}
"
.
format
(
type
(
e
).
__name__
,
str
(
e
)))
...
...
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