diff --git a/duniterpy/api/ws2p/requests.py b/duniterpy/api/ws2p/requests.py index 66f5b60e77e627b120fbbfa791fd7915ce957176..55e8426f642082ee1fb4c83541347e5ba3606a18 100644 --- a/duniterpy/api/ws2p/requests.py +++ b/duniterpy/api/ws2p/requests.py @@ -17,6 +17,18 @@ ERROR_RESPONSE_SCHEMA = { "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: """ @@ -39,14 +51,25 @@ def get_current(request_id: Optional[str] = None) -> str: }) -GET_CURRENT_RESPONSE_SCHEMA = { - "type": "object", - "properties": { - "resId": { - "type": "string", - "pattern": "^[0-9,a-z,A-Z]{8}$" - }, - "body": BLOCK_SCHEMA - }, - "required": ["resId", "body"] -} +def get_block(block_number: int, request_id: Optional[str] = None) -> str: + """ + Return ws2p getBlock() request as json string + + :return: + """ + + if request_id is None: + request_id = get_ws2p_challenge()[:8] + 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 + } + } + }) + diff --git a/examples/request_ws2p.py b/examples/request_ws2p.py index 6975054e73335016dd69f17b643e6d48b3dd87a8..c55107ccb8d01713446d9a75d5112dd9a9e042bc 100644 --- a/examples/request_ws2p.py +++ b/examples/request_ws2p.py @@ -128,7 +128,28 @@ async def main(): response = await ws.receive_str() try: # 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 print("Response: " + response) except ValidationError as exception: diff --git a/tests/api/ws2p/test_ws2p.py b/tests/api/ws2p/test_ws2p.py index 07158c8085772dc33283d90c441cecc168458897..6c2c9d1fd91e5edc463852478cc3dace6846505d 100644 --- a/tests/api/ws2p/test_ws2p.py +++ b/tests/api/ws2p/test_ws2p.py @@ -5,7 +5,7 @@ import jsonschema from duniterpy.api.client import Client, parse_text from duniterpy.api.endpoint import BMAEndpoint 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 @@ -63,7 +63,7 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase): 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, "currency":"g1-test","hash":"000024399D612753E59D44415CFA61F3A663919110CD2EB8D30C93F49C61E07F", "previousHash":"00007A2931B1B33351151058E8FE5C8368C9A7C6F13F37FEB92AA67B17B7EC46", @@ -76,8 +76,10 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase): "signature":"Ks0ugrWCZ/jBDyFQ77TnzTIKJrv2lBJKwQqVW64ZEESgD++J4pjPCEP0WDmcbm65VAomKbnkWOJsThdAIgj2DA==", "nonce":10400000002073,"monetaryMass":144418724,"writtenOn":367572, "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) + + def test_error_response_validation(self): error_response_string = """{"resId":"cfe10cc4","err":"Error message"}""" error_response = parse_text(error_response_string, ERROR_RESPONSE_SCHEMA) self.assertIsInstance(error_response, dict)