|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +from typing import Any, Dict, List |
| 4 | + |
| 5 | +from aws_lambda_powertools.utilities.data_classes.common import DictWrapper |
| 6 | + |
| 7 | + |
| 8 | +class BasicProperties(DictWrapper): |
| 9 | + @property |
| 10 | + def content_type(self) -> str: |
| 11 | + return self["contentType"] |
| 12 | + |
| 13 | + @property |
| 14 | + def content_encoding(self) -> str: |
| 15 | + return self["contentEncoding"] |
| 16 | + |
| 17 | + @property |
| 18 | + def headers(self) -> Dict[str, Any]: |
| 19 | + return self["headers"] |
| 20 | + |
| 21 | + @property |
| 22 | + def delivery_mode(self) -> int: |
| 23 | + return self["deliveryMode"] |
| 24 | + |
| 25 | + @property |
| 26 | + def priority(self) -> int: |
| 27 | + return self["priority"] |
| 28 | + |
| 29 | + @property |
| 30 | + def correlation_id(self) -> str: |
| 31 | + return self["correlationId"] |
| 32 | + |
| 33 | + @property |
| 34 | + def reply_to(self) -> str: |
| 35 | + return self["replyTo"] |
| 36 | + |
| 37 | + @property |
| 38 | + def expiration(self) -> int: |
| 39 | + return self["expiration"] |
| 40 | + |
| 41 | + @property |
| 42 | + def message_id(self) -> str: |
| 43 | + return self["messageId"] |
| 44 | + |
| 45 | + @property |
| 46 | + def timestamp(self) -> int: |
| 47 | + return self["timestamp"] |
| 48 | + |
| 49 | + @property |
| 50 | + def get_type(self) -> str: |
| 51 | + return self["type"] |
| 52 | + |
| 53 | + @property |
| 54 | + def user_id(self) -> str: |
| 55 | + return self["userId"] |
| 56 | + |
| 57 | + @property |
| 58 | + def app_id(self) -> str: |
| 59 | + return self["appId"] |
| 60 | + |
| 61 | + @property |
| 62 | + def cluster_id(self) -> str: |
| 63 | + return self["clusterId"] |
| 64 | + |
| 65 | + @property |
| 66 | + def body_size(self) -> int: |
| 67 | + return self["bodySize"] |
| 68 | + |
| 69 | + |
| 70 | +class RabbitMessage(DictWrapper): |
| 71 | + @property |
| 72 | + def basic_properties(self) -> BasicProperties: |
| 73 | + return BasicProperties(self["basicProperties"]) |
| 74 | + |
| 75 | + @property |
| 76 | + def redelivered(self) -> bool: |
| 77 | + return self["redelivered"] |
| 78 | + |
| 79 | + @property |
| 80 | + def data(self) -> str: |
| 81 | + return self["data"] |
| 82 | + |
| 83 | + @property |
| 84 | + def decoded_data(self) -> str: |
| 85 | + """Decodes the data as a str""" |
| 86 | + return base64.b64decode(self.data.encode()).decode() |
| 87 | + |
| 88 | + @property |
| 89 | + def json_data(self) -> Any: |
| 90 | + """Parses the data as json""" |
| 91 | + return json.loads(self.decoded_data) |
| 92 | + |
| 93 | + |
| 94 | +class RabbitMQEvent(DictWrapper): |
| 95 | + """Represents a Rabbit MQ event sent to Lambda |
| 96 | +
|
| 97 | + Documentation: |
| 98 | + -------------- |
| 99 | + - https://docs.aws.amazon.com/lambda/latest/dg/with-mq.html |
| 100 | + - https://aws.amazon.com/blogs/compute/using-amazon-mq-for-rabbitmq-as-an-event-source-for-lambda/ |
| 101 | + """ |
| 102 | + |
| 103 | + def __init__(self, data: Dict[str, Any]): |
| 104 | + super().__init__(data) |
| 105 | + self._rmq_messages_by_queue = { |
| 106 | + key: [RabbitMessage(message) for message in messages] |
| 107 | + for key, messages in self["rmqMessagesByQueue"].items() |
| 108 | + } |
| 109 | + |
| 110 | + @property |
| 111 | + def event_source(self) -> str: |
| 112 | + return self["eventSource"] |
| 113 | + |
| 114 | + @property |
| 115 | + def event_source_arn(self) -> str: |
| 116 | + return self["eventSourceArn"] |
| 117 | + |
| 118 | + @property |
| 119 | + def rmq_messages_by_queue(self) -> Dict[str, List[RabbitMessage]]: |
| 120 | + return self._rmq_messages_by_queue |
0 commit comments