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: class MessageSender:
def __init__(self, custom_anchor=None): def __init__(self, custom_anchor=None):
self.server_connector = ServerUplinkConnector() self.server_connector = ServerUplinkConnector()
if isinstance(custom_anchor, str): if isinstance(custom_anchor, bytes):
custom_anchor = custom_anchor.encode('utf-8') custom_anchor = custom_anchor.decode('utf-8')
self.anchor = custom_anchor or b'message' self.anchor = custom_anchor or 'message'
def send(self, originator, message): def send(self, originator, message):
response = { data = {
'originator': originator, 'originator': originator,
'timestamp': datetime.now().isoformat(), 'timestamp': datetime.now().isoformat(),
'message': message 'message': message
} }
encoded_response = json.dumps(response).encode('utf-8') response = {
self.server_connector.send(self.anchor, encoded_response) 'anchor': self.anchor,
'data': data
}
self.server_connector.send(*[frame.encode('utf-8') for frame in (self.anchor, json.dumps(response))])