Rework MessageSender to conform defined API

This commit is contained in:
Bálint Bokros 2018-02-02 16:49:15 +01:00
parent c99630172b
commit 91d5e560eb

View File

@ -7,15 +7,18 @@ from .networking.server_connector import ServerUplinkConnector
class MessageSender:
def __init__(self, custom_anchor=None):
self.server_connector = ServerUplinkConnector()
if isinstance(custom_anchor, str):
custom_anchor = custom_anchor.encode('utf-8')
self.anchor = custom_anchor or b'message'
if isinstance(custom_anchor, bytes):
custom_anchor = custom_anchor.decode('utf-8')
self.anchor = custom_anchor or 'message'
def send(self, originator, message):
response = {
data = {
'originator': originator,
'timestamp': datetime.now().isoformat(),
'message': message
}
encoded_response = json.dumps(response).encode('utf-8')
self.server_connector.send(self.anchor, encoded_response)
response = {
'anchor': self.anchor,
'data': data
}
self.server_connector.send(*[frame.encode('utf-8') for frame in (self.anchor, json.dumps(response))])