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 support for arbitrary deserializers #230

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions rele/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

logger = logging.getLogger(__name__)

def json_deserializer(message):
return json.loads(message.data.decode("utf-8"))

class Subscription:
"""The Subscription class
Expand Down Expand Up @@ -35,14 +37,22 @@ def callback_func(self, data, **kwargs):
"""

def __init__(
self, func, topic, prefix="", suffix="", filter_by=None, backend_filter_by=None
self,
func,
topic,
prefix="",
suffix="",
filter_by=None,
backend_filter_by=None,
deserializer=json_deserializer,
):
self._func = func
self.topic = topic
self._prefix = prefix
self._suffix = suffix
self._filters = self._init_filters(filter_by)
self.backend_filter_by = backend_filter_by
self._deserializer = self._init_deserializer(deserializer)

def _init_filters(self, filter_by):
if filter_by and not (
Expand All @@ -61,6 +71,11 @@ def _init_filters(self, filter_by):

return None

def _init_deserializer(self, deserializer):
if not callable(deserializer):
raise ValueError("deserializer must be a callable or None.")
return deserializer

@property
def name(self):
name_parts = [self._prefix, self.topic, self._suffix]
Expand Down Expand Up @@ -109,8 +124,8 @@ def __call__(self, message):
start_time = time.time()

try:
data = json.loads(message.data.decode("utf-8"))
except json.JSONDecodeError as e:
data = self._subscription._deserializer(message)
except Exception as e:
message.ack()
run_middleware_hook(
"post_process_message_failure",
Expand Down Expand Up @@ -145,7 +160,14 @@ def __call__(self, message):
run_middleware_hook("post_process_message")


def sub(topic, prefix=None, suffix=None, filter_by=None, backend_filter_by=None):
def sub(
topic,
prefix=None,
suffix=None,
filter_by=None,
backend_filter_by=None,
deserializer=json_deserializer,
):
"""Decorator function that makes declaring a PubSub Subscription simple.

The Subscriber returned will automatically create and name
Expand Down Expand Up @@ -181,6 +203,11 @@ def purpose_2(data, **kwargs):
def sub_process_landscape_photos(data, **kwargs):
pass

@sub(topic='string-messages', deserializer=lambda x: x.data.decode('utf-8'))
def sub_process_non_json(data, **kwargs):
pass


:param topic: string The topic that is being subscribed to.
:param prefix: string An optional prefix to the subscription name.
Useful to namespace your subscription with your project name
Expand All @@ -190,6 +217,8 @@ def sub_process_landscape_photos(data, **kwargs):
:param filter_by: Union[function, list] An optional function or tuple of
functions that filters the messages to be processed by
the sub regarding their attributes.
:param deserializer: function An optional deserialization function that
replaces the default json deserialization.
:return: :class:`~rele.subscription.Subscription`
"""

Expand All @@ -214,6 +243,7 @@ def decorator(func):
suffix=suffix,
filter_by=filter_by,
backend_filter_by=backend_filter_by,
deserializer=deserializer,
)

return decorator
15 changes: 15 additions & 0 deletions tests/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ def sub_process_landscape_gif_photos(data, **kwargs):
return f'Received a {kwargs.get("format")} photo of type {kwargs.get("type")}'


def deserializer_handler(message):
return message.data.decode("utf-8")


@sub(topic="string-data", deserializer=deserializer_handler)
def sub_string_deserializer(data, **kwargs):
return data


class TestSubscription:
def test_subs_return_subscription_objects(self):
assert isinstance(sub_stub, Subscription)
Expand Down Expand Up @@ -294,6 +303,12 @@ def crashy_sub_stub(data, **kwargs):
}
assert failed_log.subscription_message == str(message_wrapper)

def test_deserializer_handler(self, message_wrapper_invalid_json):
callback = Callback(sub_string_deserializer)
res = callback(message_wrapper_invalid_json)

assert res == "foobar"

def test_log_acks_called_message_when_not_json_serializable(
self, caplog, message_wrapper_invalid_json, published_at
):
Expand Down