diff --git a/bluejay/__init__.py b/bluejay/__init__.py index e7adb5c..d400ce2 100644 --- a/bluejay/__init__.py +++ b/bluejay/__init__.py @@ -7,7 +7,7 @@ - Stdout (via Logging) """ -__version__ = "0.1.3" +__version__ = "0.2.0" from . import backend, client, event diff --git a/bluejay/backend/sns.py b/bluejay/backend/sns.py index 2d1425c..10e8eb9 100644 --- a/bluejay/backend/sns.py +++ b/bluejay/backend/sns.py @@ -1,3 +1,6 @@ +import gzip +from base64 import b64encode + from .command import SendEvent, SendResponse from .encode import JSONEncoder @@ -16,13 +19,19 @@ def build(cls, topic_arn: str) -> "SNSBackend": client = boto3.client("sns") return cls(client, topic_arn) + @classmethod + def compress(cls, payload: str) -> str: + compressed_payload = gzip.compress(payload.encode()) + b64_payload = b64encode(compressed_payload) + return b64_payload.decode() + def send(self, message: SendEvent) -> SendResponse: + payload = JSONEncoder().encode(message.payload) + compressed_payload = self.compress(payload) self.client.publish( TopicArn=self.topic_arn, MessageStructure="json", Subject=message.event_name, - Message=JSONEncoder().encode( - {"default": JSONEncoder().encode(message.payload)} - ), + Message=compressed_payload, ) return SendResponse(success=True) diff --git a/tests/backend/sns/test_compression.py b/tests/backend/sns/test_compression.py new file mode 100644 index 0000000..4b12f37 --- /dev/null +++ b/tests/backend/sns/test_compression.py @@ -0,0 +1,26 @@ +import gzip +from base64 import b64encode + +import pytest + +from bluejay.backend import SNSBackend +from bluejay.backend.encode import JSONEncoder + + +@pytest.fixture +def expected_payload(send_event_command): + obj = send_event_command.payload + + return SNSBackend.compress(JSONEncoder().encode(obj)) + + +def test_compression_duplicate_code(send_event_command, expected_payload): + payload = send_event_command.payload + + json_payload = JSONEncoder().encode(payload) + + compressed_string = gzip.compress(json_payload.encode()) + b64_payload = b64encode(compressed_string) + compressed_payload = b64_payload.decode() + + assert compressed_payload == expected_payload diff --git a/tests/backend/sns/test_sending.py b/tests/backend/sns/test_sending.py index 83e9fc3..5259bb7 100644 --- a/tests/backend/sns/test_sending.py +++ b/tests/backend/sns/test_sending.py @@ -1,16 +1,14 @@ import pytest +from bluejay.backend import SNSBackend from bluejay.backend.encode import JSONEncoder @pytest.fixture def expected_json(send_event_command): obj = send_event_command.payload - encoded = JSONEncoder().encode(obj) - # The message should be wrapped up in a {default: } wrapper - # This is also encoded. - return JSONEncoder().encode({"default": encoded}) + return SNSBackend.compress(JSONEncoder().encode(obj)) @pytest.fixture