def handle_message( self, message: SmartAppFromMessage ) -> typing.Tuple[int, str, SmartAppToMessage]: if not message.validate(): result = 400, "BAD REQUEST", SmartAppToMessage( self.BAD_REQUEST_COMMAND, message=message, request=None, ) try: result[2].as_dict except (json.JSONDecodeError, KeyError): result = 400, "BAD REQUEST", SmartAppToMessage( self.BAD_REQUEST_COMMAND, message=basic_error_message, request=None, ) finally: return result answer, stats = self.process_message(message) if not answer: return 204, "NO CONTENT", SmartAppToMessage(self.NO_ANSWER_COMMAND, message=message, request=None) answer_message = SmartAppToMessage(answer, message, request=None, validators=self.to_msg_validators) if answer_message.validate(): return 200, "OK", answer_message else: return 500, "BAD ANSWER", SmartAppToMessage( self.BAD_ANSWER_COMMAND, message=message, request=None)
def _generate_answers(self, user, commands, message, **kwargs): topic_key = kwargs["topic_key"] kafka_key = kwargs["kafka_key"] answers = [] commands = commands or [] commands = combine_commands(commands, user) for command in commands: request = SmartKitKafkaRequest(id=None, items=command.request_data) request.update_empty_items({ "topic_key": topic_key, "kafka_key": kafka_key }) answer = SmartAppToMessage(command=command, message=message, request=request, masking_fields=self.masking_fields, validators=self.to_msg_validators) if answer.validate(): answers.append(answer) else: answers.append( SmartAppToMessage(self.BAD_ANSWER_COMMAND, message=message, request=request)) smart_kit_metrics.counter_outgoing(self.app_name, command.name, answer, user) return answers
async def handle_message(self, message: SmartAppFromMessage) -> typing.Tuple[int, str, SmartAppToMessage]: if not message.validate(): return 400, "BAD REQUEST", SmartAppToMessage(self.BAD_REQUEST_COMMAND, message=message, request=None) answer, stats = await self.process_message(message) if not answer: return 204, "NO CONTENT", SmartAppToMessage(self.NO_ANSWER_COMMAND, message=message, request=None) answer_message = SmartAppToMessage( answer, message, request=None, validators=self.to_msg_validators) if answer_message.validate(): return 200, "OK", answer_message else: return 500, "BAD ANSWER", SmartAppToMessage(self.BAD_ANSWER_COMMAND, message=message, request=None)
def test_validation(self): command_ = PicklableMock() request_ = PicklableMock() message_ = PicklableMock() command_.payload = {"pi": 3.14159265358979} command_.name = "AnyName" request_.header = "json" message_.payload = {"q": 0} message_.incremental_id = 111 message_.session_id = 11 message_.uuid = '1234-5678-9012' message = SmartAppToMessage(command_, message_, request_, validators=(PieMessageValidator(), )) self.assertTrue(message.validate()) command_.payload["pi"] = 2.7182818284 message = SmartAppToMessage(command_, message_, request_, validators=(PieMessageValidator(), )) self.assertFalse(message.validate())