Files
amq-adapter-python/amqp/adapter/amq_route_factory.py
T
2025-04-06 01:24:51 +01:00

76 lines
2.2 KiB
Python

import logging
from amqp.model.model import AMQRoute
# record separator
RS = ":"
# null route, in lieu of None value
NULL_ROUTE = {
"componentName": "",
"exchange": None,
"queue": None,
"routingKey": "#",
"timeout": 1,
"validUntil": 0
}
class AMQRouteFactory:
"""
Define the methods to create and validate AMQRoute objects.
"""
@staticmethod
def from_string(data):
"""
Builds the AMQRoute from its string form.
:param data: route string
:return: AMQRoute object
"""
try:
keys = data.split(RS)
if len(keys) >= 5:
i = 0
component_name = keys[i]
i += 1
exchange = keys[i]
i += 1
queue = keys[i] if i < len(keys) else ""
i += 1
routing_key = keys[i] if i < len(keys) else ""
i += 1
timeout = int(keys[i]) if i < len(keys) else 0
i += 1
valid_until = int(keys[i]) if i < len(keys) else 0
return AMQRoute(
component_name=component_name,
exchange=exchange,
queue=queue,
key=routing_key,
timeout=timeout,
valid_until=valid_until
)
logging.error(
" [E] AMQRoute incorrect format. AMQRoute expects input as: "
"'Component-Name:AMQExchange-Name:Queue-Name:Routing-Key:Timeout:Valid-Until', "
"got: ['%s']", data
)
except Exception as err:
logging.error(
" [E] %s: AMQRoute incorrect format. AMQRoute expects input as: "
"'Component-Name:AMQExchange-Name:Queue-Name:Routing-Key:Timeout:Valid-Until', "
"got: ['%s']", err, data
)
return NULL_ROUTE
@staticmethod
def validate(route_str):
"""
Validates the route string.
:param route_str: route string
:return: boolean validity indicator
"""
if route_str is not None:
return route_str.count(":") > 4
return False