2017-12-13 03:37:28 +01:00
|
|
|
import json
|
2022-12-11 11:39:38 +01:00
|
|
|
import logging
|
2018-10-08 12:35:56 +02:00
|
|
|
import time
|
2017-12-13 03:37:28 +01:00
|
|
|
|
2019-12-27 23:26:39 +01:00
|
|
|
from platypush.message import Message
|
2017-12-17 16:15:44 +01:00
|
|
|
|
2019-12-11 18:05:17 +01:00
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
class Response(Message):
|
2022-12-11 11:39:38 +01:00
|
|
|
"""Response message class"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
target=None,
|
|
|
|
origin=None,
|
|
|
|
id=None,
|
|
|
|
output=None,
|
|
|
|
errors=None,
|
|
|
|
timestamp=None,
|
|
|
|
logging_level=logging.INFO,
|
|
|
|
):
|
2017-12-17 16:15:44 +01:00
|
|
|
"""
|
2020-08-24 01:14:40 +02:00
|
|
|
:param target: Target
|
|
|
|
:type target: str
|
|
|
|
:param origin: Origin
|
|
|
|
:type origin: str
|
|
|
|
:param output: Output
|
|
|
|
:param errors: Errors
|
|
|
|
:param id: Message ID this response refers to
|
|
|
|
:type id: str
|
|
|
|
:param timestamp: Message timestamp
|
|
|
|
:type timestamp: float
|
2017-12-17 16:15:44 +01:00
|
|
|
"""
|
|
|
|
|
2022-12-11 11:39:38 +01:00
|
|
|
super().__init__(timestamp=timestamp, logging_level=logging_level)
|
2017-12-17 16:15:44 +01:00
|
|
|
self.target = target
|
2018-01-05 23:20:39 +01:00
|
|
|
self.output = self._parse_msg(output)
|
2019-12-11 18:05:17 +01:00
|
|
|
self.errors = self._parse_msg(errors or [])
|
2017-12-17 16:15:44 +01:00
|
|
|
self.origin = origin
|
2017-12-18 03:09:38 +01:00
|
|
|
self.id = id
|
2017-12-13 03:37:28 +01:00
|
|
|
|
|
|
|
def is_error(self):
|
2022-12-11 11:39:38 +01:00
|
|
|
"""Returns True if the response has errors"""
|
2017-12-13 03:37:28 +01:00
|
|
|
return len(self.errors) != 0
|
|
|
|
|
2018-01-05 23:20:39 +01:00
|
|
|
@classmethod
|
|
|
|
def _parse_msg(cls, msg):
|
2022-12-11 11:39:38 +01:00
|
|
|
if isinstance(msg, (bytes, bytearray)):
|
2018-01-05 23:20:39 +01:00
|
|
|
msg = msg.decode('utf-8')
|
|
|
|
if isinstance(msg, str):
|
2019-12-11 18:05:17 +01:00
|
|
|
try:
|
|
|
|
msg = json.loads(msg.strip())
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2018-01-05 23:20:39 +01:00
|
|
|
|
|
|
|
return msg
|
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
@classmethod
|
|
|
|
def build(cls, msg):
|
|
|
|
msg = super().parse(msg)
|
|
|
|
args = {
|
2019-12-11 18:05:17 +01:00
|
|
|
'target': msg['target'],
|
|
|
|
'output': msg['response']['output'],
|
|
|
|
'errors': msg['response']['errors'],
|
|
|
|
'timestamp': msg['_timestamp'] if '_timestamp' in msg else time.time(),
|
2022-12-11 11:39:38 +01:00
|
|
|
'logging_level': msg.get('_logging_level', logging.INFO),
|
2017-12-17 16:15:44 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 18:05:17 +01:00
|
|
|
if 'id' in msg:
|
|
|
|
args['id'] = msg['id']
|
|
|
|
if 'origin' in msg:
|
|
|
|
args['origin'] = msg['origin']
|
2017-12-17 16:15:44 +01:00
|
|
|
|
2019-12-11 18:05:17 +01:00
|
|
|
return cls(**args)
|
2017-12-17 16:15:44 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
2020-08-24 01:14:40 +02:00
|
|
|
Overrides the ``str()`` operator and converts
|
2017-12-17 16:15:44 +01:00
|
|
|
the message into a UTF-8 JSON string
|
|
|
|
"""
|
2022-12-11 11:39:38 +01:00
|
|
|
output = (
|
|
|
|
self.output if self.output is not None else {'success': bool(self.errors)}
|
|
|
|
)
|
2019-12-27 15:55:56 +01:00
|
|
|
|
2019-03-06 02:23:01 +01:00
|
|
|
response_dict = {
|
2019-12-11 18:05:17 +01:00
|
|
|
'id': self.id,
|
|
|
|
'type': 'response',
|
|
|
|
'target': self.target if hasattr(self, 'target') else None,
|
|
|
|
'origin': self.origin if hasattr(self, 'origin') else None,
|
|
|
|
'_timestamp': self.timestamp,
|
|
|
|
'response': {
|
2019-12-27 15:55:56 +01:00
|
|
|
'output': output,
|
2019-12-11 18:05:17 +01:00
|
|
|
'errors': self.errors,
|
2017-12-17 16:15:44 +01:00
|
|
|
},
|
2019-03-06 02:23:01 +01:00
|
|
|
}
|
|
|
|
|
2022-12-11 11:39:38 +01:00
|
|
|
if self.logging_level:
|
|
|
|
response_dict['_logging_level'] = self.logging_level
|
2019-03-06 02:23:01 +01:00
|
|
|
|
2019-12-27 23:26:39 +01:00
|
|
|
return json.dumps(response_dict, cls=self.Encoder)
|
2017-12-13 03:37:28 +01:00
|
|
|
|
2022-12-11 11:39:38 +01:00
|
|
|
def log(self, action=None):
|
|
|
|
prefix = (
|
|
|
|
f'Processed response from action {action}: '
|
|
|
|
if action
|
|
|
|
else 'Received response: '
|
|
|
|
)
|
|
|
|
|
|
|
|
super().log(prefix)
|
|
|
|
|
2019-12-25 20:32:54 +01:00
|
|
|
|
2019-03-06 02:01:17 +01:00
|
|
|
# vim:sw=4:ts=4:et:
|