Skip to content

Commit

Permalink
Make the message body a public property
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
  • Loading branch information
abompard committed Jan 23, 2019
1 parent 67fb323 commit 7c8f522
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
16 changes: 8 additions & 8 deletions docs/sample_schema_package/mailman_schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,21 @@ class MessageV1(BaseMessage):
@property
def subject(self):
"""The email's subject."""
return self._body["msg"]["subject"]
return self.body["msg"]["subject"]

@property
def body(self):
"""The email message body."""
return self._body["msg"]["body"]
return self.body["msg"]["body"]

@property
def agent_avatar(self):
"""An URL to the avatar of the user who caused the action."""
from_header = self._body["msg"]["from"]
from_header = self.body["msg"]["from"]
return get_avatar(from_header)

def _get_archived_at(self):
return self._body["msg"]["archived-at"]
return self.body["msg"]["archived-at"]


class MessageV2(BaseMessage):
Expand Down Expand Up @@ -184,18 +184,18 @@ class MessageV2(BaseMessage):
@property
def subject(self):
"""The email's subject."""
return self._body["subject"]
return self.body["subject"]

@property
def body(self):
"""The email message body."""
return self._body["body"]
return self.body["body"]

@property
def agent_avatar(self):
"""An URL to the avatar of the user who caused the action."""
from_header = self._body["from"]
from_header = self.body["from"]
return get_avatar(from_header)

def _get_archived_at(self):
return self._body["archived-at"]
return self.body["archived-at"]
2 changes: 1 addition & 1 deletion docs/tutorial/schemas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ from the message, or to create a human-readable representation.
Change the ``__str__()`` method to use the expected items from the message body. For example::

return '{owner} did something to the {package} package'.format(
owner=self._body['owner'], package=self._body['package']['name'])
owner=self.body['owner'], package=self.body['package']['name'])

Also edit the ``summary`` property to return something relevant.

Expand Down
16 changes: 8 additions & 8 deletions fedora_messaging/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class Message(object):
def __init__(
self, body=None, headers=None, topic=None, properties=None, severity=None
):
self._body = body or {}
self.body = body or {}
if topic:
self.topic = topic
headers = headers or {}
Expand Down Expand Up @@ -459,14 +459,14 @@ def _encoded_routing_key(self):
@property
def _encoded_body(self):
"""The encoded body used to publish the message."""
return json.dumps(self._body).encode("utf-8")
return json.dumps(self.body).encode("utf-8")

def __repr__(self):
"""
Provide a printable representation of the object that can be passed to func:`eval`.
"""
return "{}(id={}, topic={}, body={})".format(
self.__class__.__name__, repr(self.id), repr(self.topic), repr(self._body)
self.__class__.__name__, repr(self.id), repr(self.topic), repr(self.body)
)

def __eq__(self, other):
Expand Down Expand Up @@ -498,7 +498,7 @@ def __eq__(self, other):

return (
self.topic == other.topic
and self._body == other._body
and self.body == other.body
and headers == other_headers
)

Expand Down Expand Up @@ -527,9 +527,9 @@ def validate(self):
jsonschema.validate(self._headers, schema)
for schema in (self.body_schema, Message.body_schema):
_log.debug(
'Validating message body "%r" with schema "%r"', self._body, schema
'Validating message body "%r" with schema "%r"', self.body, schema
)
jsonschema.validate(self._body, schema)
jsonschema.validate(self.body, schema)

@property
def summary(self):
Expand Down Expand Up @@ -559,7 +559,7 @@ def __str__(self):
h=json.dumps(
self._headers, sort_keys=True, indent=4, separators=(",", ": ")
),
b=json.dumps(self._body, sort_keys=True, indent=4, separators=(",", ": ")),
b=json.dumps(self.body, sort_keys=True, indent=4, separators=(",", ": ")),
)

@property
Expand Down Expand Up @@ -646,6 +646,6 @@ def _dump(self):
"topic": self.topic,
"headers": self._headers,
"id": self.id,
"body": self._body,
"body": self.body,
"queue": self.queue,
}
2 changes: 1 addition & 1 deletion fedora_messaging/tests/integration/test_publish_consume.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def callback(message):
assert len(messages_received) == 3
for m in messages_received:
assert u"nice.message" == m.topic
assert {u"encouragement": u"You're doing great!"} == m._body
assert {u"encouragement": u"You're doing great!"} == m.body
assert "sent-at" in m._headers
del m._headers["sent-at"]
assert expected_headers == m._headers
Expand Down
14 changes: 7 additions & 7 deletions fedora_messaging/tests/unit/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_proper_json(self):
self.assertIsInstance(test_message, message.Message)
self.assertEqual("test topic", test_message.topic)
self.assertEqual("test id", test_message.id)
self.assertEqual({"test_key": "test_value"}, test_message._body)
self.assertEqual({"test_key": "test_value"}, test_message.body)
self.assertEqual("test queue", test_message.queue)
self.assertEqual(
message.WARNING, test_message._headers["fedora_messaging_severity"]
Expand Down Expand Up @@ -190,7 +190,7 @@ def test_missing_queue(self):
self.assertEqual(len(messages), 1)
self.assertEqual("test topic", test_message.topic)
self.assertEqual("test id", test_message.id)
self.assertEqual({"test_key": "test_value"}, test_message._body)
self.assertEqual({"test_key": "test_value"}, test_message.body)
self.assertEqual(None, test_message.queue)
self.assertEqual(
message.WARNING, test_message._headers["fedora_messaging_severity"]
Expand Down Expand Up @@ -430,36 +430,36 @@ class CustomMessage(message.Message):
@property
def usernames(self):
try:
return self._body["users"]
return self.body["users"]
except KeyError:
return []

@property
def packages(self):
try:
return self._body["packages"]
return self.body["packages"]
except KeyError:
return []

@property
def containers(self):
try:
return self._body["containers"]
return self.body["containers"]
except KeyError:
return []
pass

@property
def modules(self):
try:
return self._body["modules"]
return self.body["modules"]
except KeyError:
return []

@property
def flatpaks(self):
try:
return self._body["flatpaks"]
return self.body["flatpaks"]
except KeyError:
return []

Expand Down
6 changes: 3 additions & 3 deletions fedora_messaging/tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setUp(self):
self.message._headers = {}
self.message.topic = "test.topic"
self.message._encoded_routing_key = b"test.topic"
self.message._body = "test body"
self.message.body = "test body"
self.message._encoded_body = b'"test body"'
self.tls_conf = {
"keyfile": None,
Expand Down Expand Up @@ -777,7 +777,7 @@ def test_message(self):
msg = self.consumer._consumer_callback.call_args_list[0][0][0]
msg.validate.assert_called_once()
self.channel.basic_ack.assert_called_with(delivery_tag="testtag")
self.assertEqual(msg._body, "test body")
self.assertEqual(msg.body, "test body")

def test_message_queue_set(self):
"""Assert the queue attribute is set on messages."""
Expand All @@ -791,7 +791,7 @@ def test_message_encoding(self):
self.consumer._on_message(self.channel, self.frame, self.properties, body)
self.consumer._consumer_callback.assert_called_once()
msg = self.consumer._consumer_callback.call_args_list[0][0][0]
self.assertEqual(msg._body, "test body unicode é à ç")
self.assertEqual(msg.body, "test body unicode é à ç")

def test_message_wrong_encoding(self):
body = '"test body unicode é à ç"'.encode("utf-8")
Expand Down
1 change: 1 addition & 0 deletions news/PR119.api
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :py:attr:`Message._body` attribute is renamed to ``body``, and is now part of the public API.

0 comments on commit 7c8f522

Please sign in to comment.