mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 11:41:31 +00:00
Refactor API message format
This commit is contained in:
parent
996e8e2af7
commit
25f54a71b5
@ -13,7 +13,7 @@ class ConsoleLogsHandler:
|
|||||||
try:
|
try:
|
||||||
connector.send_message({
|
connector.send_message({
|
||||||
'key': 'console.write',
|
'key': 'console.write',
|
||||||
'value': message[self.stream]
|
'content': message[self.stream]
|
||||||
})
|
})
|
||||||
except KeyError:
|
except KeyError:
|
||||||
LOG.error('Invalid %s message received: %s', self.keys, message)
|
LOG.error('Invalid %s message received: %s', self.keys, message)
|
||||||
|
@ -26,8 +26,8 @@ class FrontendReadyHandler:
|
|||||||
|
|
||||||
def handle_ready(self):
|
def handle_ready(self):
|
||||||
trigger = {
|
trigger = {
|
||||||
'key': 'fsm.step',
|
'key': 'fsm.trigger',
|
||||||
'trigger': self.initial_trigger
|
'transition': self.initial_trigger
|
||||||
}
|
}
|
||||||
sign_message(self._auth_key, trigger)
|
sign_message(self._auth_key, trigger)
|
||||||
self.connector.send_message(trigger)
|
self.connector.send_message(trigger)
|
||||||
|
@ -25,8 +25,8 @@ class MessageQueueHandler:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _generate_messages_from_queue(queue_message):
|
def _generate_messages_from_queue(queue_message):
|
||||||
last = queue_message['value'][-1]
|
last = queue_message['messages'][-1]
|
||||||
for message in queue_message['value']:
|
for message in queue_message['messages']:
|
||||||
yield {
|
yield {
|
||||||
'key': 'message.send',
|
'key': 'message.send',
|
||||||
'typing': message is not last,
|
'typing': message is not last,
|
||||||
|
@ -37,7 +37,7 @@ def handler():
|
|||||||
def queue():
|
def queue():
|
||||||
yield {
|
yield {
|
||||||
'key': 'message.queue',
|
'key': 'message.queue',
|
||||||
'value': [
|
'messages': [
|
||||||
{'originator': urandom(4).hex(), 'message': urandom(16).hex()}
|
{'originator': urandom(4).hex(), 'message': urandom(16).hex()}
|
||||||
for _ in range(randint(5, 10))
|
for _ in range(randint(5, 10))
|
||||||
]
|
]
|
||||||
@ -46,7 +46,7 @@ def queue():
|
|||||||
|
|
||||||
def test_message_order(handler, queue):
|
def test_message_order(handler, queue):
|
||||||
handler.connector.raise_event(queue)
|
handler.connector.raise_event(queue)
|
||||||
old_list = queue['value']
|
old_list = queue['messages']
|
||||||
new_list = handler.connector.messages
|
new_list = handler.connector.messages
|
||||||
length = len(old_list)
|
length = len(old_list)
|
||||||
assert len(new_list) == length
|
assert len(new_list) == length
|
||||||
@ -64,4 +64,4 @@ def test_wpm(handler, queue):
|
|||||||
handler.wpm = 100000000
|
handler.wpm = 100000000
|
||||||
handler.connector.raise_event(queue)
|
handler.connector.raise_event(queue)
|
||||||
sleep(0.25)
|
sleep(0.25)
|
||||||
assert len(handler.connector.messages) == 2*len(queue['value'])
|
assert len(handler.connector.messages) == 2*len(queue['messages'])
|
||||||
|
@ -14,13 +14,13 @@ class MessageSender:
|
|||||||
def queue_messages(self, messages, originator=None):
|
def queue_messages(self, messages, originator=None):
|
||||||
message_queue = {
|
message_queue = {
|
||||||
'key': 'message.queue',
|
'key': 'message.queue',
|
||||||
'value': []
|
'messages': []
|
||||||
}
|
}
|
||||||
for message in messages:
|
for message in messages:
|
||||||
next_message = {'message': message}
|
next_message = {'message': message}
|
||||||
if originator:
|
if originator:
|
||||||
next_message['originator'] = originator
|
next_message['originator'] = originator
|
||||||
message_queue['value'].append(next_message)
|
message_queue['messages'].append(next_message)
|
||||||
self.uplink.send_message(message_queue)
|
self.uplink.send_message(message_queue)
|
||||||
|
|
||||||
def set_originator(self, originator):
|
def set_originator(self, originator):
|
||||||
|
@ -18,8 +18,8 @@ class FSMHandler:
|
|||||||
self.auth_key = KeyManager().auth_key
|
self.auth_key = KeyManager().auth_key
|
||||||
|
|
||||||
self.command_handlers = {
|
self.command_handlers = {
|
||||||
'fsm.step' : self.handle_step,
|
'fsm.trigger': self.handle_step,
|
||||||
'fsm.update' : self.handle_update
|
'fsm.update' : self.handle_update
|
||||||
}
|
}
|
||||||
|
|
||||||
def handle_event(self, message, _):
|
def handle_event(self, message, _):
|
||||||
@ -32,7 +32,7 @@ class FSMHandler:
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||||
|
|
||||||
def handle_step(self, message): # pylint: disable=inconsistent-return-statements
|
def handle_trigger(self, message): # pylint: disable=inconsistent-return-statements
|
||||||
if self.fsm.step(message['trigger']):
|
if self.fsm.step(message['trigger']):
|
||||||
return message
|
return message
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ class HistoryMonitor(ABC, InotifyObserver):
|
|||||||
def send_message(self, command):
|
def send_message(self, command):
|
||||||
self.uplink.send_message({
|
self.uplink.send_message({
|
||||||
'key': f'history.{self.domain}',
|
'key': f'history.{self.domain}',
|
||||||
'value': command
|
'command': command
|
||||||
}, intent=Intent.EVENT)
|
}, intent=Intent.EVENT)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user