Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Helpers build_messages_attributes #243

Merged
merged 8 commits into from
Sep 26, 2024
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


khayomacedo marked this conversation as resolved.
Show resolved Hide resolved
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)