Commit d39d1f77 authored by Federico Sismondi's avatar Federico Sismondi

added Message.load_from_pika which will save a lot of lines of code for TT developpers

parent 8b3a3bfa
Pipeline #1767 passed with stage
in 0 seconds
......@@ -192,25 +192,8 @@ class Message(object):
:param properties: Used for building more complete complex representation (e.g. for reply_to corre_id params)
:return: The python Message object or subclass (e.g. MsgPacketSniffedRaw)
Properties argument can be any of the following
1. dict of all the properties, example for creating the dict from pika props:
2. properties pika object itself
def on_request(self, ch, method, props, body):
props_dict = {
'content_type': props.content_type,
'delivery_mode': props.delivery_mode,
'correlation_id': props.correlation_id,
'reply_to': props.reply_to,
'message_id': props.message_id,
'timestamp': props.timestamp,
'user_id': props.user_id,
'app_id': props.app_id,
}
example fromAgent.coap_client.packet.raw -> matches fromAgent.*.packet.raw, hence returns MsgPacketSniffedRaw
about r_key matching mechanism:
fromAgent.coap_client.packet.raw -> matches fromAgent.*.packet.raw -> returns MsgPacketSniffedRaw
# We can use the Message class to build Message objects from json + rkey:
>>> m=MsgSniffingGetCapture()
......@@ -242,8 +225,7 @@ class Message(object):
elif type(properties) is dict:
props_dict.update(properties)
else:
pass
# TODO parse pika and kombu type of properties
raise NotImplementedError('Incompatible properties input or not yet supported')
# let's update the messages properties
if properties:
......@@ -251,6 +233,30 @@ class Message(object):
return built_message
@classmethod
def load_from_pika(cls, method, props, body):
"""
Builds a python object representation of the AMQP message based on the ones defined by the event bus API.
Takes as arguments pika objects method, properties and body returned by channel.basic_consume method
"""
global rk_pattern_to_message_type_map
props_dict = {
'content_type': props.content_type,
'delivery_mode': props.delivery_mode,
'correlation_id': props.correlation_id,
'reply_to': props.reply_to,
'message_id': props.message_id,
'timestamp': props.timestamp,
'user_id': props.user_id,
'app_id': props.app_id,
}
routing_key = method.routing_key
json_body = body.decode('utf-8')
return Message.load(json_body, routing_key, props_dict)
@classmethod
def from_json(cls, body):
"""
......@@ -1147,6 +1153,7 @@ class MsgTestingToolConfigured(Message):
"testing_tools": "f-interop/interoperability-coap",
}
# TODO deprecate this message
class MsgSessionCreated(Message):
"""
......@@ -2508,7 +2515,7 @@ rk_pattern_to_message_type_map = RoutingKeyToMessageMap(
"orchestrator.tests.get_contributor_name.request": MsgOrchestratorTestsGetContributorName, # any -> SO
# TODO deprecate this
"orchestrator.session.created":MsgSessionCreated, # SO -> any
"orchestrator.session.created": MsgSessionCreated, # SO -> any
# CORE API: TT <-> GUI
"ui.core.session.get.request": MsgUiRequestSessionConfiguration, # TT -> GUI
......
......@@ -72,11 +72,11 @@ class MessagesLibraryTests(unittest.TestCase):
method, props, body = self.channel.basic_get(self.queue_for_post_validation)
while method:
logging.info('got a message')
message_count += 1
logging.info('parsing message %s, number: %s' % (method.routing_key,message_count))
self.channel.basic_ack(method.delivery_tag)
# load w/o properties
# api call 1 - load w/o properties
message = Message.load(
json_body=body.decode('utf-8'),
routing_key=method.routing_key,
......@@ -84,7 +84,9 @@ class MessagesLibraryTests(unittest.TestCase):
)
# load w/ properties
assert message, 'load w/o properties didnt work for %s' % (body.decode('utf-8'), method.routing_key)
# api call 2 - load w/ properties
props_dict = {
'content_type': props.content_type,
'delivery_mode': props.delivery_mode,
......@@ -95,17 +97,25 @@ class MessagesLibraryTests(unittest.TestCase):
'user_id': props.user_id,
'app_id': props.app_id,
}
# load w/o properties
message = Message.load(
json_body=body.decode('utf-8'),
routing_key=method.routing_key,
properties=props_dict,
)
assert message, 'load WITH properties didnt work for %s' % (body.decode('utf-8'), method.routing_key)
# api call 3 - load using pika dependent api call
message = Message.load_from_pika(
method,
props,
body,
)
#print(repr(message))
assert message, 'load pika properties didnt work for %s' % (body.decode('utf-8'), method.routing_key)
logging.info(repr(message))
method, props, body = self.channel.basic_get(self.queue_for_post_validation)
if len(self.serializable_messages_types) != message_count:
self.fail('expecting %s messages, but got %s' % (len(self.serializable_messages_types), message_count))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment