Skip to content

Commit

Permalink
Add Helpers build_messages_attributes (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
khayomacedo authored Sep 26, 2024
1 parent 6433c48 commit cbcd45f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
8 changes: 7 additions & 1 deletion faster_sam/dependencies/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Callable, Dict, Type
from uuid import uuid4
import uuid
from faster_sam import helpers

from fastapi import Request
from pydantic import BaseModel
Expand Down Expand Up @@ -44,6 +45,11 @@ def dep(message: schema) -> Dict[str, Any]:
assert isinstance(message, IntoSQSInfo)

info = message.into()

message_attributes = {}
if info.message_attributes:
message_attributes = helpers.build_message_attributes(info.message_attributes)

event = {
"Records": [
{
Expand All @@ -56,7 +62,7 @@ def dep(message: schema) -> Dict[str, Any]:
"SenderId": str(uuid.uuid4()),
"ApproximateFirstReceiveTimestamp": info.sent_timestamp,
},
"messageAttributes": info.message_attributes,
"messageAttributes": message_attributes,
"md5OfBody": hashlib.md5(info.body.encode()).hexdigest(),
"eventSource": "aws:sqs",
"eventSourceARN": info.source_arn,
Expand Down
17 changes: 17 additions & 0 deletions faster_sam/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Dict


def build_message_attributes(attributes: Dict[str, str]) -> Dict[str, Dict[str, str]]:
attrs = {}

for key, value in attributes.items():
if isinstance(value, str):
attrs[key] = {"stringValue": value, "dataType": "String"}
elif isinstance(value, (int, float)):
attrs[key] = {"stringValue": str(value), "dataType": "Number"}
elif isinstance(value, bytes):
attrs[key] = {"BinaryValue": str(value), "dataType": "Binary"}
else:
raise TypeError(f"{value} of type {type(value).__name__} is not supported")

return attrs
47 changes: 47 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import unittest

from faster_sam import helpers


class TestHelpers(unittest.TestCase):
def teste_empty_attributes(self):
result = helpers.build_message_attributes({})
self.assertEqual(result, {})

def test_string_attributes(self):
attributes = {"key1": "value1", "key2": "value2"}
expected_result = {
"key1": {"stringValue": "value1", "dataType": "String"},
"key2": {"stringValue": "value2", "dataType": "String"},
}
result = helpers.build_message_attributes(attributes)
self.assertEqual(result, expected_result)

def test_number_attributes(self):
attributes = {"key1": 123, "key2": 12.3}
expected_result = {
"key1": {"stringValue": "123", "dataType": "Number"},
"key2": {"stringValue": "12.3", "dataType": "Number"},
}
result = helpers.build_message_attributes(attributes)
self.assertEqual(result, expected_result)

def test_bytes_attributes(self):
attributes = {"key1": b"test_bytes"}
expected_result = {"key1": {"BinaryValue": str(b"test_bytes"), "dataType": "Binary"}}
result = helpers.build_message_attributes(attributes)
self.assertEqual(result, expected_result)

def test_multi_attributes(self):
attributes = {"key1": 123, "key2": "value1"}
expected_result = {
"key1": {"stringValue": "123", "dataType": "Number"},
"key2": {"stringValue": "value1", "dataType": "String"},
}
result = helpers.build_message_attributes(attributes)
self.assertEqual(result, expected_result)

def test_unsupported_type(self):
attributes = {"key1": "value1", "key2": [1, 2, 3]}
with self.assertRaises(TypeError):
helpers.build_message_attributes(attributes)

0 comments on commit cbcd45f

Please sign in to comment.