Skip to content
Snippets Groups Projects
Commit 29dd3bcd authored by Vincent Texier's avatar Vincent Texier
Browse files

[enh] #58 add ws2p document message class to send documents

parent 36f95354
No related branches found
No related tags found
No related merge requests found
import json import json
from typing import Optional from typing import Optional, Any
from duniterpy.documents import Document from duniterpy.documents import Document
from duniterpy.key import VerifyingKey, SigningKey from duniterpy.key import VerifyingKey, SigningKey
...@@ -46,7 +46,7 @@ class Connect(Document): ...@@ -46,7 +46,7 @@ class Connect(Document):
def get_signed_json(self, signing_key: SigningKey) -> str: def get_signed_json(self, signing_key: SigningKey) -> str:
""" """
Return the signed document in json format Return the signed message in json format
:param signing_key: Signing key instance :param signing_key: Signing key instance
...@@ -100,7 +100,7 @@ class Ack(Document): ...@@ -100,7 +100,7 @@ class Ack(Document):
def get_signed_json(self, signing_key: SigningKey) -> str: def get_signed_json(self, signing_key: SigningKey) -> str:
""" """
Return the signed document in json format Return the signed message in json format
:param signing_key: Signing key instance :param signing_key: Signing key instance
...@@ -153,7 +153,7 @@ class Ok(Document): ...@@ -153,7 +153,7 @@ class Ok(Document):
def get_signed_json(self, signing_key: SigningKey) -> str: def get_signed_json(self, signing_key: SigningKey) -> str:
""" """
Return the signed document in json format Return the signed message in json format
:param signing_key: Signing key instance :param signing_key: Signing key instance
...@@ -168,3 +168,39 @@ class Ok(Document): ...@@ -168,3 +168,39 @@ class Ok(Document):
def __str__(self) -> str: def __str__(self) -> str:
return self.raw() return self.raw()
# fixme: the document format to send is to be determine
# does raw or inline format works ?
class DocumentMessage:
PEER_TYPE_ID = 0
TRANSACTION_TYPE_ID = 1
MEMBERSHIP_TYPE_ID = 2
CERTIFICATION_TYPE_ID = 3
IDENTITY_TYPE_ID = 4
BLOCK_TYPE_ID = 5
DOCUMENT_TYPE_NAMES = {
0: "peer",
1: "transaction",
2: "membership",
3: "certification",
4: "identity",
5: "block"
}
def get_json(self, document_type_id: int, document: Any) -> str:
"""
Return the document message in json format
:param document_type_id: Id of the document type, use class properties
:param document: Document object to send
:return:
"""
data = {
"body": {
"name": document_type_id,
self.DOCUMENT_TYPE_NAMES[document_type_id]: document
}
}
return json.dumps(data)
import json
import unittest import unittest
import jsonschema import jsonschema
...@@ -7,6 +8,8 @@ from duniterpy.api.endpoint import BMAEndpoint ...@@ -7,6 +8,8 @@ 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 BLOCK_RESPONSE_SCHEMA, ERROR_RESPONSE_SCHEMA, BLOCKS_RESPONSE_SCHEMA, \ from duniterpy.api.ws2p.requests import BLOCK_RESPONSE_SCHEMA, ERROR_RESPONSE_SCHEMA, BLOCKS_RESPONSE_SCHEMA, \
REQUIREMENTS_RESPONSE_SCHEMA REQUIREMENTS_RESPONSE_SCHEMA
from duniterpy.documents import Identity, BlockUID
from duniterpy.documents.ws2p.messages import DocumentMessage
from tests.api.webserver import WebFunctionalSetupMixin, web from tests.api.webserver import WebFunctionalSetupMixin, web
...@@ -176,3 +179,26 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase): ...@@ -176,3 +179,26 @@ class TestWs2pHeads(WebFunctionalSetupMixin, unittest.TestCase):
]}} """ ]}} """
response = parse_text(response_string, REQUIREMENTS_RESPONSE_SCHEMA) response = parse_text(response_string, REQUIREMENTS_RESPONSE_SCHEMA)
self.assertIsInstance(response, dict) self.assertIsInstance(response, dict)
def test_document_message(self):
# prepare message
document_message = DocumentMessage()
# prepare document
identity_document = Identity(
10, "beta_brousouf", "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd", "lolcat",
BlockUID(32, "DB30D958EE5CB75186972286ED3F4686B8A1C2CD"),
"J3G9oM5AKYZNLAB5Wx499w61NuUoS57JVccTShUbGpCMjCqj9yXXqNq7dyZpDWA6BxipsiaMZhujMeBfCznzyci")
# get json string message
json_document_message = document_message.get_json(DocumentMessage.IDENTITY_TYPE_ID, identity_document.inline())
# convert to dict to verify
dict_document_message = json.loads(json_document_message)
# verify
self.assertIn("body", dict_document_message)
self.assertIn("name", dict_document_message["body"])
self.assertIn("identity", dict_document_message["body"])
self.assertEqual(4, dict_document_message["body"]["name"])
expected = """HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd\
:J3G9oM5AKYZNLAB5Wx499w61NuUoS57JVccTShUbGpCMjCqj9yXXqNq7dyZpDWA6BxipsiaMZhujMeBfCznzyci:32\
-DB30D958EE5CB75186972286ED3F4686B8A1C2CD:lolcat"""
self.assertEqual(expected, dict_document_message["body"]["identity"])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment