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
dcb2f9c3
Commit
dcb2f9c3
authored
5 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
[enh]
#58
add ws2p request getBlock(number)
parent
ec635294
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
duniterpy/api/ws2p/requests.py
+34
-11
34 additions, 11 deletions
duniterpy/api/ws2p/requests.py
examples/request_ws2p.py
+22
-1
22 additions, 1 deletion
examples/request_ws2p.py
tests/api/ws2p/test_ws2p.py
+5
-3
5 additions, 3 deletions
tests/api/ws2p/test_ws2p.py
with
61 additions
and
15 deletions
duniterpy/api/ws2p/requests.py
+
34
−
11
View file @
dcb2f9c3
...
@@ -17,6 +17,18 @@ ERROR_RESPONSE_SCHEMA = {
...
@@ -17,6 +17,18 @@ ERROR_RESPONSE_SCHEMA = {
"
required
"
:
[
"
resId
"
,
"
err
"
]
"
required
"
:
[
"
resId
"
,
"
err
"
]
}
}
BLOCK_RESPONSE_SCHEMA
=
{
"
type
"
:
"
object
"
,
"
properties
"
:
{
"
resId
"
:
{
"
type
"
:
"
string
"
,
"
pattern
"
:
"
^[0-9,a-z,A-Z]{8}$
"
},
"
body
"
:
BLOCK_SCHEMA
},
"
required
"
:
[
"
resId
"
,
"
body
"
]
}
def
get_current
(
request_id
:
Optional
[
str
]
=
None
)
->
str
:
def
get_current
(
request_id
:
Optional
[
str
]
=
None
)
->
str
:
"""
"""
...
@@ -39,14 +51,25 @@ def get_current(request_id: Optional[str] = None) -> str:
...
@@ -39,14 +51,25 @@ def get_current(request_id: Optional[str] = None) -> str:
})
})
GET_CURRENT_RESPONSE_SCHEMA
=
{
def
get_block
(
block_number
:
int
,
request_id
:
Optional
[
str
]
=
None
)
->
str
:
"
type
"
:
"
object
"
,
"""
"
properties
"
:
{
Return ws2p getBlock() request as json string
"
resId
"
:
{
"
type
"
:
"
string
"
,
:return:
"
pattern
"
:
"
^[0-9,a-z,A-Z]{8}$
"
"""
},
"
body
"
:
BLOCK_SCHEMA
if
request_id
is
None
:
},
request_id
=
get_ws2p_challenge
()[:
8
]
"
required
"
:
[
"
resId
"
,
"
body
"
]
else
:
}
if
not
re
.
fullmatch
(
"
^[0-9a-zA-Z]{8}$
"
,
request_id
):
raise
Exception
(
"
Invalid ws2p request unique id
"
)
return
json
.
dumps
({
"
reqId
"
:
request_id
,
"
body
"
:
{
"
name
"
:
"
BLOCK_BY_NUMBER
"
,
"
params
"
:
{
"
number
"
:
block_number
}
}
})
This diff is collapsed.
Click to expand it.
examples/request_ws2p.py
+
22
−
1
View file @
dcb2f9c3
...
@@ -128,7 +128,28 @@ async def main():
...
@@ -128,7 +128,28 @@ async def main():
response
=
await
ws
.
receive_str
()
response
=
await
ws
.
receive_str
()
try
:
try
:
# check response format
# check response format
parse_text
(
response
,
requests
.
GET_CURRENT_RESPONSE_SCHEMA
)
parse_text
(
response
,
requests
.
BLOCK_RESPONSE_SCHEMA
)
# if valid display response
print
(
"
Response:
"
+
response
)
except
ValidationError
as
exception
:
# if invalid response...
try
:
# check error response format
parse_text
(
response
,
requests
.
ERROR_RESPONSE_SCHEMA
)
# if valid, display error response
print
(
"
Error response:
"
+
response
)
except
ValidationError
as
e
:
# if invalid, display exception on response validation
print
(
exception
)
# send ws2p request
print
(
"
Send getBlock(360000) request
"
)
await
ws
.
send_str
(
requests
.
get_block
(
360000
))
# receive response as string
response
=
await
ws
.
receive_str
()
try
:
# check response format
parse_text
(
response
,
requests
.
BLOCK_RESPONSE_SCHEMA
)
# if valid display response
# if valid display response
print
(
"
Response:
"
+
response
)
print
(
"
Response:
"
+
response
)
except
ValidationError
as
exception
:
except
ValidationError
as
exception
:
...
...
This diff is collapsed.
Click to expand it.
tests/api/ws2p/test_ws2p.py
+
5
−
3
View file @
dcb2f9c3
...
@@ -5,7 +5,7 @@ import jsonschema
...
@@ -5,7 +5,7 @@ import jsonschema
from
duniterpy.api.client
import
Client
,
parse_text
from
duniterpy.api.client
import
Client
,
parse_text
from
duniterpy.api.endpoint
import
BMAEndpoint
from
duniterpy.api.endpoint
import
BMAEndpoint
from
duniterpy.api.ws2p.network
import
heads
,
WS2P_HEADS_SCHEMA
from
duniterpy.api.ws2p.network
import
heads
,
WS2P_HEADS_SCHEMA
from
duniterpy.api.ws2p.requests
import
GET_CURRENT
_RESPONSE_SCHEMA
,
ERROR_RESPONSE_SCHEMA
from
duniterpy.api.ws2p.requests
import
BLOCK
_RESPONSE_SCHEMA
,
ERROR_RESPONSE_SCHEMA
from
tests.api.webserver
import
WebFunctionalSetupMixin
,
web
from
tests.api.webserver
import
WebFunctionalSetupMixin
,
web
...
@@ -63,7 +63,7 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase):
...
@@ -63,7 +63,7 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase):
self
.
loop
.
run_until_complete
(
go
())
self
.
loop
.
run_until_complete
(
go
())
def
test_
get_current
_validation
(
self
):
def
test_
block_response
_validation
(
self
):
response_string
=
"""
{
"
resId
"
:
"
cfe10cc4
"
,
"
body
"
:{
"
wrong
"
:false,
"
version
"
:11,
"
number
"
:367572,
response_string
=
"""
{
"
resId
"
:
"
cfe10cc4
"
,
"
body
"
:{
"
wrong
"
:false,
"
version
"
:11,
"
number
"
:367572,
"
currency
"
:
"
g1-test
"
,
"
hash
"
:
"
000024399D612753E59D44415CFA61F3A663919110CD2EB8D30C93F49C61E07F
"
,
"
currency
"
:
"
g1-test
"
,
"
hash
"
:
"
000024399D612753E59D44415CFA61F3A663919110CD2EB8D30C93F49C61E07F
"
,
"
previousHash
"
:
"
00007A2931B1B33351151058E8FE5C8368C9A7C6F13F37FEB92AA67B17B7EC46
"
,
"
previousHash
"
:
"
00007A2931B1B33351151058E8FE5C8368C9A7C6F13F37FEB92AA67B17B7EC46
"
,
...
@@ -76,8 +76,10 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase):
...
@@ -76,8 +76,10 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase):
"
signature
"
:
"
Ks0ugrWCZ/jBDyFQ77TnzTIKJrv2lBJKwQqVW64ZEESgD++J4pjPCEP0WDmcbm65VAomKbnkWOJsThdAIgj2DA==
"
,
"
signature
"
:
"
Ks0ugrWCZ/jBDyFQ77TnzTIKJrv2lBJKwQqVW64ZEESgD++J4pjPCEP0WDmcbm65VAomKbnkWOJsThdAIgj2DA==
"
,
"
nonce
"
:10400000002073,
"
monetaryMass
"
:144418724,
"
writtenOn
"
:367572,
"
nonce
"
:10400000002073,
"
monetaryMass
"
:144418724,
"
writtenOn
"
:367572,
"
written_on
"
:
"
367572-000024399D612753E59D44415CFA61F3A663919110CD2EB8D30C93F49C61E07F
"
}}
"""
"
written_on
"
:
"
367572-000024399D612753E59D44415CFA61F3A663919110CD2EB8D30C93F49C61E07F
"
}}
"""
response
=
parse_text
(
response_string
,
GET_CURRENT
_RESPONSE_SCHEMA
)
response
=
parse_text
(
response_string
,
BLOCK
_RESPONSE_SCHEMA
)
self
.
assertIsInstance
(
response
,
dict
)
self
.
assertIsInstance
(
response
,
dict
)
def
test_error_response_validation
(
self
):
error_response_string
=
"""
{
"
resId
"
:
"
cfe10cc4
"
,
"
err
"
:
"
Error message
"
}
"""
error_response_string
=
"""
{
"
resId
"
:
"
cfe10cc4
"
,
"
err
"
:
"
Error message
"
}
"""
error_response
=
parse_text
(
error_response_string
,
ERROR_RESPONSE_SCHEMA
)
error_response
=
parse_text
(
error_response_string
,
ERROR_RESPONSE_SCHEMA
)
self
.
assertIsInstance
(
error_response
,
dict
)
self
.
assertIsInstance
(
error_response
,
dict
)
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