Skip to content

Commit

Permalink
Sns compression
Browse files Browse the repository at this point in the history
* add compression in code and tests

* remvoed wrapper and some refactor

* Lint

* removes unused import

* fixes type hinting

* tests for compressioN

* renamed fixture

* minor version bump
  • Loading branch information
Angelo authored Oct 28, 2019
1 parent cc0b179 commit a747089
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bluejay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Stdout (via Logging)
"""

__version__ = "0.1.3"
__version__ = "0.2.0"

from . import backend, client, event

Expand Down
15 changes: 12 additions & 3 deletions bluejay/backend/sns.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gzip
from base64 import b64encode

from .command import SendEvent, SendResponse
from .encode import JSONEncoder

Expand All @@ -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)
26 changes: 26 additions & 0 deletions tests/backend/sns/test_compression.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions tests/backend/sns/test_sending.py
Original file line number Diff line number Diff line change
@@ -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: <message>} wrapper
# This is also encoded.
return JSONEncoder().encode({"default": encoded})
return SNSBackend.compress(JSONEncoder().encode(obj))


@pytest.fixture
Expand Down

0 comments on commit a747089

Please sign in to comment.