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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clients
python
DuniterPy
Commits
fecd341e
Commit
fecd341e
authored
5 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
[enh]
#58
refactor request_ws2p.py with a send function
parent
ec148fb3
No related branches found
No related tags found
2 merge requests
!94
Merge dev into master for release 0.56.0
,
!84
#58: WS2P support
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/request_ws2p.py
+67
-96
67 additions, 96 deletions
examples/request_ws2p.py
with
67 additions
and
96 deletions
examples/request_ws2p.py
+
67
−
96
View file @
fecd341e
...
...
@@ -7,13 +7,14 @@ from _socket import gaierror
import
aiohttp
import
jsonschema
from
jsonschema
import
ValidationError
from
typing
import
Any
from
duniterpy.tools
import
get_ws2p_challenge
from
duniterpy.key
import
SigningKey
from
duniterpy.helpers.ws2p
import
handshake
from
duniterpy.api.ws2p
import
requests
from
duniterpy.api.client
import
Client
from
duniterpy.api.client
import
Client
,
WSConnection
# CONFIG #######################################
...
...
@@ -29,6 +30,43 @@ CURRENCY = "g1-test"
################################################
async
def
send
(
ws
:
WSConnection
,
request
:
str
,
request_id
:
str
,
schema
:
dict
)
->
Any
:
"""
Send a WS2P request
:rtype: Any
:param ws: WSConnection instance
:param request: Request string to send
:param request_id: Unique request id
:param schema: Validation schema
"""
# Send request
await
ws
.
send_str
(
request
)
# Wait response with request id
response
=
await
ws
.
receive_json
()
while
"
resId
"
not
in
response
or
(
"
resId
"
in
response
and
response
[
"
resId
"
]
!=
request_id
):
response
=
await
ws
.
receive_json
()
time
.
sleep
(
1
)
try
:
# Check response format
jsonschema
.
validate
(
response
,
schema
)
# If valid display response
except
ValidationError
:
# If invalid response...
try
:
# Check error response format
jsonschema
.
validate
(
response
,
requests
.
ERROR_RESPONSE_SCHEMA
)
# If valid, display error response
except
ValidationError
as
exception
:
# If invalid, display exception on response validation
print
(
exception
)
return
response
async
def
main
():
"""
Main code
...
...
@@ -67,113 +105,46 @@ async def main():
# Send ws2p request
print
(
"
Send getCurrent() request
"
)
request_id
=
get_ws2p_challenge
()[:
8
]
await
ws
.
send_str
(
requests
.
get_current
(
request_id
))
# Wait response with request id
response
=
await
ws
.
receive_json
()
while
"
resId
"
not
in
response
or
(
"
resId
"
in
response
and
response
[
"
resId
"
]
!=
request_id
):
response
=
await
ws
.
receive_json
()
time
.
sleep
(
1
)
try
:
# Check response format
jsonschema
.
validate
(
response
,
requests
.
BLOCK_RESPONSE_SCHEMA
)
# If valid display response
response
=
await
send
(
ws
,
requests
.
get_current
(
request_id
),
request_id
,
requests
.
BLOCK_RESPONSE_SCHEMA
,
)
print
(
"
Response:
"
+
json
.
dumps
(
response
,
indent
=
2
))
except
ValidationError
:
# If invalid response...
try
:
# Check error response format
jsonschema
.
validate
(
response
,
requests
.
ERROR_RESPONSE_SCHEMA
)
# If valid, display error response
print
(
"
Error response:
"
+
json
.
dumps
(
response
,
indent
=
2
))
except
ValidationError
as
exception
:
# If invalid, display exception on response validation
print
(
exception
)
# Send ws2p request
print
(
"
Send getBlock(360000) request
"
)
request_id
=
get_ws2p_challenge
()[:
8
]
await
ws
.
send_str
(
requests
.
get_block
(
request_id
,
360000
))
# Wait response with request id
response
=
await
ws
.
receive_json
()
while
"
resId
"
not
in
response
or
(
"
resId
"
in
response
and
response
[
"
resId
"
]
!=
request_id
):
response
=
await
ws
.
receive_json
()
time
.
sleep
(
1
)
try
:
# Check response format
jsonschema
.
validate
(
response
,
requests
.
BLOCK_RESPONSE_SCHEMA
)
# If valid display response
response
=
await
send
(
ws
,
requests
.
get_block
(
request_id
,
360000
),
request_id
,
requests
.
BLOCK_RESPONSE_SCHEMA
,
)
print
(
"
Response:
"
+
json
.
dumps
(
response
,
indent
=
2
))
except
ValidationError
:
# If invalid response...
try
:
# Check error response format
jsonschema
.
validate
(
response
,
requests
.
ERROR_RESPONSE_SCHEMA
)
# If valid, display error response
print
(
"
Error response:
"
+
json
.
dumps
(
response
,
indent
=
2
))
except
ValidationError
as
exception
:
# If invalid, display exception on response validation
print
(
exception
)
# Send ws2p request
print
(
"
Send getBlocks(360000, 2) request
"
)
request_id
=
get_ws2p_challenge
()[:
8
]
await
ws
.
send_str
(
requests
.
get_blocks
(
request_id
,
360000
,
2
))
# Wait response with request id
response
=
await
ws
.
receive_json
()
while
"
resId
"
not
in
response
or
(
"
resId
"
in
response
and
response
[
"
resId
"
]
!=
request_id
):
response
=
await
ws
.
receive_json
()
time
.
sleep
(
1
)
try
:
# Check response format
jsonschema
.
validate
(
response
,
requests
.
BLOCKS_RESPONSE_SCHEMA
)
# If valid display response
response
=
await
send
(
ws
,
requests
.
get_blocks
(
request_id
,
360000
,
2
),
request_id
,
requests
.
BLOCKS_RESPONSE_SCHEMA
,
)
print
(
"
Response:
"
+
json
.
dumps
(
response
,
indent
=
2
))
except
ValidationError
:
# If invalid response...
try
:
# Check error response format
jsonschema
.
validate
(
response
,
requests
.
ERROR_RESPONSE_SCHEMA
)
# If valid, display error response
print
(
"
Error response:
"
+
json
.
dumps
(
response
,
indent
=
2
))
except
ValidationError
as
exception
:
# If invalid, display exception on response validation
print
(
exception
)
# Send ws2p request
print
(
"
Send getRequirementsPending(3) request
"
)
request_id
=
get_ws2p_challenge
()[:
8
]
await
ws
.
send_str
(
requests
.
get_requirements_pending
(
request_id
,
3
))
# Wait response with request id
response
=
await
ws
.
receive_json
()
while
"
resId
"
not
in
response
or
(
"
resId
"
in
response
and
response
[
"
resId
"
]
!=
request_id
):
response
=
await
ws
.
receive_json
()
time
.
sleep
(
1
)
try
:
# Check response format
jsonschema
.
validate
(
response
,
requests
.
REQUIREMENTS_RESPONSE_SCHEMA
)
# If valid display response
response
=
await
send
(
ws
,
requests
.
get_requirements_pending
(
request_id
,
3
),
request_id
,
requests
.
REQUIREMENTS_RESPONSE_SCHEMA
,
)
print
(
"
Response:
"
+
json
.
dumps
(
response
,
indent
=
2
))
except
ValidationError
:
# If invalid response...
try
:
# Check error response format
jsonschema
.
validate
(
response
,
requests
.
ERROR_RESPONSE_SCHEMA
)
# If valid, display error response
print
(
"
Error response:
"
+
json
.
dumps
(
response
,
indent
=
2
))
except
ValidationError
as
exception
:
# If invalid, display exception on response validation
print
(
exception
)
# Close session
await
client
.
close
()
...
...
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