-
Notifications
You must be signed in to change notification settings - Fork 421
feat(event_source): support custom json_deserializer; add json_body in SQSEvent #2200
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
Changes from 3 commits
534d45e
4ae10ef
501454f
fbbae51
f7b166f
708a7e6
ec681ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,16 @@ def body(self) -> str: | |
"""The message's contents (not URL-encoded).""" | ||
return self["body"] | ||
|
||
@property | ||
def json_body(self) -> Dict: | ||
"""Parses the submitted body as json""" | ||
try: | ||
if self._json_data is None: | ||
self._json_data = self._json_deserializer(self["body"]) | ||
return self._json_data | ||
except Exception: | ||
return self["body"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should fail hard here to give the caller a chance to handle this error correctly. Reasoning in a mental model fashion:
As a rule of thumb, you'd want to propagate the error as close to the actual problem so they instinctively know how to handle it. If we catch all, we're masking the actual error and leading them to an unknown error later on.
|
||
|
||
@property | ||
def attributes(self) -> SQSRecordAttributes: | ||
"""A map of the attributes requested in ReceiveMessage to their respective values.""" | ||
|
@@ -157,4 +167,4 @@ class SQSEvent(DictWrapper): | |
@property | ||
def records(self) -> Iterator[SQSRecord]: | ||
for record in self["Records"]: | ||
yield SQSRecord(record) | ||
yield SQSRecord(data=record, json_deserializer=self._json_deserializer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docstring ;) we apparently missed back in the days for DictWrapper, but given the json_deserializer will be added to all data classes, we can propagate the docstring to everyone benefits from it.
pushing that change