mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-23 03:31:56 +00:00
Rework MessageSender to conform defined API
This commit is contained in:
parent
c99630172b
commit
91d5e560eb
@ -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))])
|
||||
|
Loading…
Reference in New Issue
Block a user