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

[enh] format code

parent 14e3f3a3
No related branches found
No related tags found
No related merge requests found
Pipeline #6974 passed
...@@ -301,7 +301,7 @@ class Client: ...@@ -301,7 +301,7 @@ class Client:
return result return result
def connect_ws(self, path: str = '') -> _WSRequestContextManager: def connect_ws(self, path: str = "") -> _WSRequestContextManager:
""" """
Connect to a websocket in order to use API parameters Connect to a websocket in order to use API parameters
......
...@@ -389,12 +389,20 @@ class WS2PEndpoint(Endpoint): ...@@ -389,12 +389,20 @@ class WS2PEndpoint(Endpoint):
:param proxy: Proxy url :param proxy: Proxy url
:return: :return:
""" """
http_scheme = 'http' http_scheme = "http"
websocket_scheme = 'ws' websocket_scheme = "ws"
if self.port == 443: if self.port == 443:
http_scheme += 's' http_scheme += "s"
websocket_scheme += 's' websocket_scheme += "s"
return ConnectionHandler(http_scheme, websocket_scheme, self.server, self.port, self.path, session, proxy) return ConnectionHandler(
http_scheme,
websocket_scheme,
self.server,
self.port,
self.path,
session,
proxy,
)
def __str__(self) -> str: def __str__(self) -> str:
return self.inline() return self.inline()
......
...@@ -21,7 +21,7 @@ from duniterpy.api.client import Client ...@@ -21,7 +21,7 @@ from duniterpy.api.client import Client
logger = logging.getLogger("duniter/network") logger = logging.getLogger("duniter/network")
MODULE = 'network' MODULE = "network"
WS2P_HEADS_SCHEMA = { WS2P_HEADS_SCHEMA = {
"type": "object", "type": "object",
...@@ -31,27 +31,17 @@ WS2P_HEADS_SCHEMA = { ...@@ -31,27 +31,17 @@ WS2P_HEADS_SCHEMA = {
"items": { "items": {
"type": "object", "type": "object",
"properties": { "properties": {
"message": { "message": {"type": "string"},
"type": "string" "sig": {"type": "string"},
"messageV2": {"type": "string"},
"sigV2": {"type": "string"},
"step": {"type": "number"},
}, },
"sig": { "required": ["message", "sig"],
"type": "string",
}, },
"messageV2": {
"type": "string"
},
"sigV2": {
"type": "string",
},
"step": {
"type": "number",
},
},
"required": ["message", "sig"]
}
} }
}, },
"required": ["heads"] "required": ["heads"],
} }
...@@ -62,59 +52,36 @@ def heads(client: Client): ...@@ -62,59 +52,36 @@ def heads(client: Client):
:param client: Client to connect to the api :param client: Client to connect to the api
:rtype: dict :rtype: dict
""" """
return client.get(MODULE + '/ws2p/heads', schema=WS2P_HEADS_SCHEMA) return client.get(MODULE + "/ws2p/heads", schema=WS2P_HEADS_SCHEMA)
WS2P_CONNECT_MESSAGE_SCHEMA = { WS2P_CONNECT_MESSAGE_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"auth": { "auth": {"type": "string", "pattern": "^CONNECT$"},
"type": "string", "challenge": {"type": "string"},
"pattern": "^CONNECT$" "currency": {"type": "string"},
}, "pub": {"type": "string"},
"challenge": { "sig": {"type": "string"},
"type": "string",
},
"currency": {
"type": "string",
},
"pub": {
"type": "string",
}, },
"sig": { "required": ["auth", "challenge", "currency", "pub", "sig"],
"type": "string",
},
},
"required": ["auth", "challenge", "currency", "pub", "sig"]
} }
WS2P_ACK_MESSAGE_SCHEMA = { WS2P_ACK_MESSAGE_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"auth": { "auth": {"type": "string", "pattern": "^ACK$"},
"type": "string", "pub": {"type": "string"},
"pattern": "^ACK$" "sig": {"type": "string"},
}, },
"pub": { "required": ["auth", "pub", "sig"],
"type": "string",
},
"sig": {
"type": "string",
}
},
"required": ["auth", "pub", "sig"]
} }
WS2P_OK_MESSAGE_SCHEMA = { WS2P_OK_MESSAGE_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"auth": { "auth": {"type": "string", "pattern": "^OK$"},
"type": "string", "sig": {"type": "string"},
"pattern": "^OK$"
},
"sig": {
"type": "string",
}
}, },
"required": ["auth", "sig"] "required": ["auth", "sig"],
} }
...@@ -5,37 +5,28 @@ from duniterpy.api.bma.blockchain import BLOCK_SCHEMA, BLOCKS_SCHEMA ...@@ -5,37 +5,28 @@ from duniterpy.api.bma.blockchain import BLOCK_SCHEMA, BLOCKS_SCHEMA
ERROR_RESPONSE_SCHEMA = { ERROR_RESPONSE_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"resId": { "resId": {"type": "string", "pattern": "^[0-9,a-z,A-Z]{8}$"},
"type": "string", "err": {"type": "string"},
"pattern": "^[0-9,a-z,A-Z]{8}$"
}, },
"err": {"type": "string"} "required": ["resId", "err"],
},
"required": ["resId", "err"]
} }
BLOCK_RESPONSE_SCHEMA = { BLOCK_RESPONSE_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"resId": { "resId": {"type": "string", "pattern": "^[0-9,a-z,A-Z]{8}$"},
"type": "string", "body": BLOCK_SCHEMA,
"pattern": "^[0-9,a-z,A-Z]{8}$"
},
"body": BLOCK_SCHEMA
}, },
"required": ["resId", "body"] "required": ["resId", "body"],
} }
BLOCKS_RESPONSE_SCHEMA = { BLOCKS_RESPONSE_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"resId": { "resId": {"type": "string", "pattern": "^[0-9,a-z,A-Z]{8}$"},
"type": "string", "body": BLOCKS_SCHEMA,
"pattern": "^[0-9,a-z,A-Z]{8}$"
},
"body": BLOCKS_SCHEMA
}, },
"required": ["resId", "body"] "required": ["resId", "body"],
} }
REQUIREMENTS_SCHEMA = { REQUIREMENTS_SCHEMA = {
...@@ -51,46 +42,24 @@ REQUIREMENTS_SCHEMA = { ...@@ -51,46 +42,24 @@ REQUIREMENTS_SCHEMA = {
"items": { "items": {
"type": "object", "type": "object",
"properties": { "properties": {
"from": { "from": {"type": "string"},
"type": "string" "to": {"type": "string"},
}, "expiresIn": {"type": "number"},
"to": { "timestamp": {"type": "number"},
"type": "string"
},
"expiresIn": {
"type": "number"
},
"timestamp": {
"type": "number"
}
},
"required": ["from", "to", "expiresIn", "timestamp"]
}
},
"expired": {
"type": "boolean"
},
"isSentry": {
"type": "boolean"
}, },
"membershipExpiresIn": { "required": ["from", "to", "expiresIn", "timestamp"],
"type": "number"
}, },
"membershipPendingExpiresIn": {
"type": "number"
}, },
"expired": {"type": "boolean"},
"isSentry": {"type": "boolean"},
"membershipExpiresIn": {"type": "number"},
"membershipPendingExpiresIn": {"type": "number"},
"meta": { "meta": {
"type": "object", "type": "object",
"properties": { "properties": {"timestamp": {"type": "string"}},
"timestamp": { "required": ["timestamp"],
"type": "string"
}
},
"required": ["timestamp"]
},
"outdistanced": {
"type": "boolean"
}, },
"outdistanced": {"type": "boolean"},
"pendingCerts": { "pendingCerts": {
"type": "array", "type": "array",
"items": { "items": {
...@@ -108,10 +77,16 @@ REQUIREMENTS_SCHEMA = { ...@@ -108,10 +77,16 @@ REQUIREMENTS_SCHEMA = {
"target": {"type": "string"}, "target": {"type": "string"},
"to": {"type": "string"}, "to": {"type": "string"},
"written": {"type": "boolean", "const": False}, "written": {"type": "boolean", "const": False},
"written_block": {"type": ["string", "null"], "const": None}, "written_block": {
"written_hash": {"type": ["string", "null"], "const": None} "type": ["string", "null"],
} "const": None,
} },
"written_hash": {
"type": ["string", "null"],
"const": None,
},
},
},
}, },
"pendingMemberships": { "pendingMemberships": {
"type": "array", "type": "array",
...@@ -135,48 +110,50 @@ REQUIREMENTS_SCHEMA = { ...@@ -135,48 +110,50 @@ REQUIREMENTS_SCHEMA = {
"type": {"type": "string"}, "type": {"type": "string"},
"userid": {"type": "string"}, "userid": {"type": "string"},
"linked": {"type": "boolean", "const": False}, "linked": {"type": "boolean", "const": False},
"written_number": {"type": ["number", "null"], "const": None}, "written_number": {
} "type": ["number", "null"],
} "const": None,
}, },
"pubkey": {
"type": "string"
}, },
"revokation_sig": {
"type": ["string", "null"]
}, },
"revoked": {
"type": "boolean"
}, },
"sig": { "pubkey": {"type": "string"},
"type": "string" "revokation_sig": {"type": ["string", "null"]},
}, "revoked": {"type": "boolean"},
"uid": { "sig": {"type": "string"},
"type": "string" "uid": {"type": "string"},
}, "wasMember": {"type": "boolean"},
"wasMember": {
"type": "boolean"
}, },
"required": [
"certifications",
"expired",
"isSentry",
"membershipExpiresIn",
"membershipPendingExpiresIn",
"meta",
"outdistanced",
"pendingCerts",
"pendingMemberships",
"pubkey",
"revocation_sig",
"revoked",
"sig",
"uid",
"wasMember",
],
}, },
"required": ["certifications", "expired", "isSentry", "membershipExpiresIn",
"membershipPendingExpiresIn", "meta", "outdistanced", "pendingCerts",
"pendingMemberships", "pubkey", "revocation_sig", "revoked", "sig", "uid", "wasMember"]
}
} }
}, },
"required": ["identities"] "required": ["identities"],
} }
REQUIREMENTS_RESPONSE_SCHEMA = { REQUIREMENTS_RESPONSE_SCHEMA = {
"type": "object", "type": "object",
"properties": { "properties": {
"resId": { "resId": {"type": "string", "pattern": "^[0-9,a-z,A-Z]{8}$"},
"type": "string", "body": REQUIREMENTS_SCHEMA,
"pattern": "^[0-9,a-z,A-Z]{8}$"
},
"body": REQUIREMENTS_SCHEMA
}, },
"required": ["resId", "body"] "required": ["resId", "body"],
} }
...@@ -188,13 +165,7 @@ def get_current(request_id: str) -> str: ...@@ -188,13 +165,7 @@ def get_current(request_id: str) -> str:
""" """
if not re.fullmatch("^[0-9a-zA-Z]{8}$", request_id): if not re.fullmatch("^[0-9a-zA-Z]{8}$", request_id):
raise Exception("Invalid ws2p request unique id") raise Exception("Invalid ws2p request unique id")
return json.dumps({ return json.dumps({"reqId": request_id, "body": {"name": "CURRENT", "params": {}}})
"reqId": request_id,
"body": {
"name": "CURRENT",
"params": {}
}
})
def get_block(request_id: str, block_number: int) -> str: def get_block(request_id: str, block_number: int) -> str:
...@@ -205,15 +176,12 @@ def get_block(request_id: str, block_number: int) -> str: ...@@ -205,15 +176,12 @@ def get_block(request_id: str, block_number: int) -> str:
""" """
if not re.fullmatch("^[0-9a-zA-Z]{8}$", request_id): if not re.fullmatch("^[0-9a-zA-Z]{8}$", request_id):
raise Exception("Invalid ws2p request unique id") raise Exception("Invalid ws2p request unique id")
return json.dumps({ return json.dumps(
{
"reqId": request_id, "reqId": request_id,
"body": { "body": {"name": "BLOCK_BY_NUMBER", "params": {"number": block_number}},
"name": "BLOCK_BY_NUMBER",
"params": {
"number": block_number
}
} }
}) )
def get_blocks(request_id: str, from_number: int, count: int) -> str: def get_blocks(request_id: str, from_number: int, count: int) -> str:
...@@ -224,16 +192,15 @@ def get_blocks(request_id: str, from_number: int, count: int) -> str: ...@@ -224,16 +192,15 @@ def get_blocks(request_id: str, from_number: int, count: int) -> str:
""" """
if not re.fullmatch("^[0-9a-zA-Z]{8}$", request_id): if not re.fullmatch("^[0-9a-zA-Z]{8}$", request_id):
raise Exception("Invalid ws2p request unique id") raise Exception("Invalid ws2p request unique id")
return json.dumps({ return json.dumps(
{
"reqId": request_id, "reqId": request_id,
"body": { "body": {
"name": "BLOCKS_CHUNK", "name": "BLOCKS_CHUNK",
"params": { "params": {"fromNumber": from_number, "count": count},
"fromNumber": from_number, },
"count": count
}
} }
}) )
def get_requirements_pending(request_id: str, min_cert: int) -> str: def get_requirements_pending(request_id: str, min_cert: int) -> str:
...@@ -244,12 +211,12 @@ def get_requirements_pending(request_id: str, min_cert: int) -> str: ...@@ -244,12 +211,12 @@ def get_requirements_pending(request_id: str, min_cert: int) -> str:
""" """
if not re.fullmatch("^[0-9a-zA-Z]{8}$", request_id): if not re.fullmatch("^[0-9a-zA-Z]{8}$", request_id):
raise Exception("Invalid ws2p request unique id") raise Exception("Invalid ws2p request unique id")
return json.dumps({ return json.dumps(
{
"reqId": request_id, "reqId": request_id,
"body": { "body": {
"name": "WOT_REQUIREMENTS_OF_PENDING", "name": "WOT_REQUIREMENTS_OF_PENDING",
"params": { "params": {"minCert": min_cert},
"minCert": min_cert },
}
} }
}) )
...@@ -11,8 +11,13 @@ class Connect(Document): ...@@ -11,8 +11,13 @@ class Connect(Document):
version = 2 version = 2
auth = "CONNECT" auth = "CONNECT"
def __init__(self, currency: str, pubkey: str, challenge: Optional[str] = None, def __init__(
signature: Optional[str] = None) -> None: self,
currency: str,
pubkey: str,
challenge: Optional[str] = None,
signature: Optional[str] = None,
) -> None:
""" """
Init Connect message document Init Connect message document
...@@ -47,8 +52,9 @@ class Connect(Document): ...@@ -47,8 +52,9 @@ class Connect(Document):
:return: :return:
""" """
return "WS2P:CONNECT:{currency}:{pub}:{challenge}".format(currency=self.currency, pub=self.pubkey, return "WS2P:CONNECT:{currency}:{pub}:{challenge}".format(
challenge=self.challenge) currency=self.currency, pub=self.pubkey, challenge=self.challenge
)
def get_signed_json(self, signing_key: SigningKey) -> str: def get_signed_json(self, signing_key: SigningKey) -> str:
""" """
...@@ -63,7 +69,7 @@ class Connect(Document): ...@@ -63,7 +69,7 @@ class Connect(Document):
"auth": self.auth, "auth": self.auth,
"pub": self.pubkey, "pub": self.pubkey,
"challenge": self.challenge, "challenge": self.challenge,
"sig": self.signatures[0] "sig": self.signatures[0],
} }
return json.dumps(data) return json.dumps(data)
...@@ -75,8 +81,13 @@ class Ack(Document): ...@@ -75,8 +81,13 @@ class Ack(Document):
version = 2 version = 2
auth = "ACK" auth = "ACK"
def __init__(self, currency: str, pubkey: str, challenge: str, def __init__(
signature: Optional[str] = None) -> None: self,
currency: str,
pubkey: str,
challenge: str,
signature: Optional[str] = None,
) -> None:
""" """
Init Ack message document Init Ack message document
...@@ -106,8 +117,9 @@ class Ack(Document): ...@@ -106,8 +117,9 @@ class Ack(Document):
:return: :return:
""" """
return "WS2P:ACK:{currency}:{pub}:{challenge}".format(currency=self.currency, pub=self.pubkey, return "WS2P:ACK:{currency}:{pub}:{challenge}".format(
challenge=self.challenge) currency=self.currency, pub=self.pubkey, challenge=self.challenge
)
def get_signed_json(self, signing_key: SigningKey) -> str: def get_signed_json(self, signing_key: SigningKey) -> str:
""" """
...@@ -118,11 +130,7 @@ class Ack(Document): ...@@ -118,11 +130,7 @@ class Ack(Document):
:return: :return:
""" """
self.sign([signing_key]) self.sign([signing_key])
data = { data = {"auth": self.auth, "pub": self.pubkey, "sig": self.signatures[0]}
"auth": self.auth,
"pub": self.pubkey,
"sig": self.signatures[0]
}
return json.dumps(data) return json.dumps(data)
def __str__(self) -> str: def __str__(self) -> str:
...@@ -133,8 +141,13 @@ class Ok(Document): ...@@ -133,8 +141,13 @@ class Ok(Document):
version = 2 version = 2
auth = "OK" auth = "OK"
def __init__(self, currency: str, pubkey: str, challenge: str, def __init__(
signature: Optional[str] = None) -> None: self,
currency: str,
pubkey: str,
challenge: str,
signature: Optional[str] = None,
) -> None:
""" """
Init Ok message document Init Ok message document
...@@ -163,8 +176,9 @@ class Ok(Document): ...@@ -163,8 +176,9 @@ class Ok(Document):
:return: :return:
""" """
return "WS2P:OK:{currency}:{pub}:{challenge}".format(currency=self.currency, pub=self.pubkey, return "WS2P:OK:{currency}:{pub}:{challenge}".format(
challenge=self.challenge) currency=self.currency, pub=self.pubkey, challenge=self.challenge
)
def get_signed_json(self, signing_key: SigningKey) -> str: def get_signed_json(self, signing_key: SigningKey) -> str:
""" """
...@@ -175,10 +189,7 @@ class Ok(Document): ...@@ -175,10 +189,7 @@ class Ok(Document):
:return: :return:
""" """
self.sign([signing_key]) self.sign([signing_key])
data = { data = {"auth": self.auth, "sig": self.signatures[0]}
"auth": self.auth,
"sig": self.signatures[0]
}
return json.dumps(data) return json.dumps(data)
def __str__(self) -> str: def __str__(self) -> str:
...@@ -199,7 +210,7 @@ class DocumentMessage: ...@@ -199,7 +210,7 @@ class DocumentMessage:
2: "membership", 2: "membership",
3: "certification", 3: "certification",
4: "identity", 4: "identity",
5: "block" 5: "block",
} }
def get_json(self, document_type_id: int, document: str) -> str: def get_json(self, document_type_id: int, document: str) -> str:
...@@ -212,7 +223,7 @@ class DocumentMessage: ...@@ -212,7 +223,7 @@ class DocumentMessage:
data = { data = {
"body": { "body": {
"name": document_type_id, "name": document_type_id,
self.DOCUMENT_TYPE_NAMES[document_type_id]: document self.DOCUMENT_TYPE_NAMES[document_type_id]: document,
} }
} }
return json.dumps(data) return json.dumps(data)
...@@ -83,24 +83,35 @@ async def main(): ...@@ -83,24 +83,35 @@ async def main():
# print(msg.data) # print(msg.data)
try: try:
# Validate json string with jsonschema and return a dict # Validate json string with jsonschema and return a dict
data = parse_text(msg.data, ws2p.network.WS2P_CONNECT_MESSAGE_SCHEMA) data = parse_text(
msg.data, ws2p.network.WS2P_CONNECT_MESSAGE_SCHEMA
)
except jsonschema.exceptions.ValidationError: except jsonschema.exceptions.ValidationError:
try: try:
# Validate json string with jsonschema and return a dict # Validate json string with jsonschema and return a dict
data = parse_text(msg.data, ws2p.network.WS2P_ACK_MESSAGE_SCHEMA) data = parse_text(
msg.data, ws2p.network.WS2P_ACK_MESSAGE_SCHEMA
)
except jsonschema.exceptions.ValidationError: except jsonschema.exceptions.ValidationError:
try: try:
# Validate json string with jsonschema and return a dict # Validate json string with jsonschema and return a dict
data = parse_text(msg.data, ws2p.network.WS2P_OK_MESSAGE_SCHEMA) data = parse_text(
msg.data, ws2p.network.WS2P_OK_MESSAGE_SCHEMA
)
except jsonschema.exceptions.ValidationError: except jsonschema.exceptions.ValidationError:
continue continue
print("Received a OK message") print("Received a OK message")
Ok(CURRENCY, remote_connect_document.pubkey, connect_document.challenge, data["sig"]) Ok(
CURRENCY,
remote_connect_document.pubkey,
connect_document.challenge,
data["sig"],
)
print("Received OK message signature is valid") print("Received OK message signature is valid")
# END HANDSHAKE ####################################################### # END HANDSHAKE #######################################################
...@@ -116,11 +127,17 @@ async def main(): ...@@ -116,11 +127,17 @@ async def main():
print("Received a ACK message") print("Received a ACK message")
# Create ACK document from ACK response to verify signature # Create ACK document from ACK response to verify signature
Ack(CURRENCY, data["pub"], connect_document.challenge, data["sig"]) Ack(
CURRENCY,
data["pub"],
connect_document.challenge,
data["sig"],
)
print("Received ACK message signature is valid") print("Received ACK message signature is valid")
# If ACK response is ok, create OK message # If ACK response is ok, create OK message
ok_message = Ok(CURRENCY, signing_key.pubkey, connect_document.challenge).get_signed_json( ok_message = Ok(
signing_key) CURRENCY, signing_key.pubkey, connect_document.challenge
).get_signed_json(signing_key)
# Send OK message # Send OK message
print("Send OK message...") print("Send OK message...")
...@@ -129,12 +146,14 @@ async def main(): ...@@ -129,12 +146,14 @@ async def main():
print("Received a CONNECT message") print("Received a CONNECT message")
remote_connect_document = Connect(CURRENCY, data["pub"], data["challenge"], data["sig"]) remote_connect_document = Connect(
CURRENCY, data["pub"], data["challenge"], data["sig"]
)
print("Received CONNECT message signature is valid") print("Received CONNECT message signature is valid")
ack_message = Ack(CURRENCY, signing_key.pubkey, ack_message = Ack(
remote_connect_document.challenge).get_signed_json( CURRENCY, signing_key.pubkey, remote_connect_document.challenge
signing_key) ).get_signed_json(signing_key)
# Send ACK message # Send ACK message
print("Send ACK message...") print("Send ACK message...")
await ws.send_str(ack_message) await ws.send_str(ack_message)
...@@ -154,7 +173,9 @@ async def main(): ...@@ -154,7 +173,9 @@ async def main():
# Wait response with request id # Wait response with request id
response_str = await ws.receive_str() response_str = await ws.receive_str()
while "resId" not in json.loads(response_str) or ( while "resId" not in json.loads(response_str) or (
"resId" in json.loads(response_str) and json.loads(response_str)["resId"] != request_id): "resId" in json.loads(response_str)
and json.loads(response_str)["resId"] != request_id
):
response_str = await ws.receive_str() response_str = await ws.receive_str()
time.sleep(1) time.sleep(1)
try: try:
...@@ -181,7 +202,9 @@ async def main(): ...@@ -181,7 +202,9 @@ async def main():
# Wait response with request id # Wait response with request id
response_str = await ws.receive_str() response_str = await ws.receive_str()
while "resId" not in json.loads(response_str) or ( while "resId" not in json.loads(response_str) or (
"resId" in json.loads(response_str) and json.loads(response_str)["resId"] != request_id): "resId" in json.loads(response_str)
and json.loads(response_str)["resId"] != request_id
):
response_str = await ws.receive_str() response_str = await ws.receive_str()
time.sleep(1) time.sleep(1)
try: try:
...@@ -208,7 +231,9 @@ async def main(): ...@@ -208,7 +231,9 @@ async def main():
# Wait response with request id # Wait response with request id
response_str = await ws.receive_str() response_str = await ws.receive_str()
while "resId" not in json.loads(response_str) or ( while "resId" not in json.loads(response_str) or (
"resId" in json.loads(response_str) and json.loads(response_str)["resId"] != request_id): "resId" in json.loads(response_str)
and json.loads(response_str)["resId"] != request_id
):
response_str = await ws.receive_str() response_str = await ws.receive_str()
time.sleep(1) time.sleep(1)
try: try:
...@@ -234,7 +259,9 @@ async def main(): ...@@ -234,7 +259,9 @@ async def main():
# Wait response with request id # Wait response with request id
response_str = await ws.receive_str() response_str = await ws.receive_str()
while "resId" not in json.loads(response_str) or ( while "resId" not in json.loads(response_str) or (
"resId" in json.loads(response_str) and json.loads(response_str)["resId"] != request_id): "resId" in json.loads(response_str)
and json.loads(response_str)["resId"] != request_id
):
response_str = await ws.receive_str() response_str = await ws.receive_str()
time.sleep(1) time.sleep(1)
try: try:
......
...@@ -5,15 +5,18 @@ import jsonschema ...@@ -5,15 +5,18 @@ import jsonschema
from duniterpy.api.client import parse_text from duniterpy.api.client import parse_text
from duniterpy.api.ws2p.network import WS2P_HEADS_SCHEMA from duniterpy.api.ws2p.network import WS2P_HEADS_SCHEMA
from duniterpy.api.ws2p.requests import BLOCK_RESPONSE_SCHEMA, ERROR_RESPONSE_SCHEMA, BLOCKS_RESPONSE_SCHEMA, \ from duniterpy.api.ws2p.requests import (
REQUIREMENTS_RESPONSE_SCHEMA BLOCK_RESPONSE_SCHEMA,
ERROR_RESPONSE_SCHEMA,
BLOCKS_RESPONSE_SCHEMA,
REQUIREMENTS_RESPONSE_SCHEMA,
)
from duniterpy.documents import Identity, BlockUID from duniterpy.documents import Identity, BlockUID
from duniterpy.documents.ws2p.messages import DocumentMessage from duniterpy.documents.ws2p.messages import DocumentMessage
from tests.api.webserver import WebFunctionalSetupMixin from tests.api.webserver import WebFunctionalSetupMixin
class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase): class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase):
def test_block(self): def test_block(self):
json_sample = { json_sample = {
"heads": [ "heads": [
...@@ -25,7 +28,7 @@ class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase): ...@@ -25,7 +28,7 @@ class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase):
102102-000002C0694C7D373A78B095419C86584B81804CFB9641B7EBC3A18040B6FEE6:e66254bf:\ 102102-000002C0694C7D373A78B095419C86584B81804CFB9641B7EBC3A18040B6FEE6:e66254bf:\
duniter:1.6.20:1:15:14", duniter:1.6.20:1:15:14",
"sigV2": "ReXzbgUya9jo4dL/R4g19Y+RE9BGB0xDkw7mrBWoldlRLkq3KFyRkAf9VthVx1UUb/AINr3nxImZKVQiVH9+DQ==", "sigV2": "ReXzbgUya9jo4dL/R4g19Y+RE9BGB0xDkw7mrBWoldlRLkq3KFyRkAf9VthVx1UUb/AINr3nxImZKVQiVH9+DQ==",
"step": 0 "step": 0,
}, },
{ {
"message": "WS2POCAIC:HEAD:1:2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ:\ "message": "WS2POCAIC:HEAD:1:2ny7YAdmzReQxAayyJZsyVYwYhVyax2thKcGknmQy5nQ:\
...@@ -35,7 +38,7 @@ class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase): ...@@ -35,7 +38,7 @@ class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase):
102102-000002C0694C7D373A78B095419C86584B81804CFB9641B7EBC3A18040B6FEE6:a0a45ed2:\ 102102-000002C0694C7D373A78B095419C86584B81804CFB9641B7EBC3A18040B6FEE6:a0a45ed2:\
duniter:1.6.21:1:34:28", duniter:1.6.21:1:34:28",
"sigV2": "p5f7/KfQqjTaCYSMUXpjUDH7uF2DafetHNgphGzkOXgxM+Upeii0Fz2RFBwnZvN+Gjp81hAqSuH48PJP6HJSAw==", "sigV2": "p5f7/KfQqjTaCYSMUXpjUDH7uF2DafetHNgphGzkOXgxM+Upeii0Fz2RFBwnZvN+Gjp81hAqSuH48PJP6HJSAw==",
"step": 1 "step": 1,
}, },
{ {
"message": "WS2POCA:HEAD:1:GRBPV3Y7PQnB9LaZhSGuS3BqBJbSHyibzYq65kTh1nQ4:\ "message": "WS2POCA:HEAD:1:GRBPV3Y7PQnB9LaZhSGuS3BqBJbSHyibzYq65kTh1nQ4:\
...@@ -45,7 +48,7 @@ class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase): ...@@ -45,7 +48,7 @@ class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase):
102102-000002C0694C7D373A78B095419C86584B81804CFB9641B7EBC3A18040B6FEE6:6d0e96f9:\ 102102-000002C0694C7D373A78B095419C86584B81804CFB9641B7EBC3A18040B6FEE6:6d0e96f9:\
duniter:1.6.21:1:20:20", duniter:1.6.21:1:20:20",
"sigV2": "VsyQmXOUYrfHWy0FeS4rJrIJCUBI+3BergbSYQ78icJWV6MQzZSw7Z+Yl7urujCYZriDQM76D6GW+6F0EELpBQ==", "sigV2": "VsyQmXOUYrfHWy0FeS4rJrIJCUBI+3BergbSYQ78icJWV6MQzZSw7Z+Yl7urujCYZriDQM76D6GW+6F0EELpBQ==",
"step": 2 "step": 2,
}, },
] ]
} }
...@@ -170,11 +173,17 @@ class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase): ...@@ -170,11 +173,17 @@ class TestWs2p(WebFunctionalSetupMixin, unittest.TestCase):
document_message = DocumentMessage() document_message = DocumentMessage()
# prepare document # prepare document
identity_document = Identity( identity_document = Identity(
10, "beta_brousouf", "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd", "lolcat", 10,
"beta_brousouf",
"HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd",
"lolcat",
BlockUID(32, "DB30D958EE5CB75186972286ED3F4686B8A1C2CD"), BlockUID(32, "DB30D958EE5CB75186972286ED3F4686B8A1C2CD"),
"J3G9oM5AKYZNLAB5Wx499w61NuUoS57JVccTShUbGpCMjCqj9yXXqNq7dyZpDWA6BxipsiaMZhujMeBfCznzyci") "J3G9oM5AKYZNLAB5Wx499w61NuUoS57JVccTShUbGpCMjCqj9yXXqNq7dyZpDWA6BxipsiaMZhujMeBfCznzyci",
)
# get json string message # get json string message
json_document_message = document_message.get_json(DocumentMessage.IDENTITY_TYPE_ID, identity_document.inline()) json_document_message = document_message.get_json(
DocumentMessage.IDENTITY_TYPE_ID, identity_document.inline()
)
# convert to dict to verify # convert to dict to verify
dict_document_message = json.loads(json_document_message) dict_document_message = json.loads(json_document_message)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment