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

[ServiceBus] Add body_as_str helper method on ServicBusMessage #16663

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions sdk/servicebus/azure-servicebus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 7.0.2 (Unreleased)

**New Features**

* Added a `body_as_str` method on `ServiceBusMessage` which returns the content of the message as a string.
yunhaoling marked this conversation as resolved.
Show resolved Hide resolved

## 7.0.1 (2021-01-12)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import uuid
import logging
import copy
from typing import Optional, List, Union, Iterable, TYPE_CHECKING, Any
from typing import Optional, List, Union, Iterable, TYPE_CHECKING, Any, cast

import six

Expand Down Expand Up @@ -321,6 +321,28 @@ def body(self):
"""
return self.message.get_data()

def body_as_str(self, encoding="UTF-8"):
# type: (str) -> str
"""The content of the message as a string, if the data is of a compatible type.

:param encoding: The encoding to use for decoding message.
Default is 'UTF-8'
:rtype: str
"""
data = self.body
try:
return "".join(b.decode(encoding) for b in cast(Iterable[bytes], data))
except TypeError:
return six.text_type(data)
except: # pylint: disable=bare-except
pass
try:
return cast(bytes, data).decode(encoding)
except Exception as e:
raise TypeError(
"Message data is not compatible with string type: {}".format(e)
)

@property
def content_type(self):
# type: () -> Optional[str]
Expand Down
3 changes: 3 additions & 0 deletions sdk/servicebus/azure-servicebus/tests/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,8 @@ def test_queue_send_twice(self, servicebus_namespace_connection_string, serviceb
with sb_client.get_queue_sender(servicebus_queue.name) as sender:
message = ServiceBusMessage("ServiceBusMessage")
message2 = ServiceBusMessage("Message2")
assert message.body_as_str() == "ServiceBusMessage"
assert message2.body_as_str() == "Message2"
# first test batch message resending.
batch_message = sender.create_message_batch()
batch_message._from_list([message, message2]) # pylint: disable=protected-access
Expand All @@ -1930,6 +1932,7 @@ def test_queue_send_twice(self, servicebus_namespace_connection_string, serviceb
with sb_client.get_queue_receiver(servicebus_queue.name, max_wait_time=5) as receiver:
for message in receiver:
messages.append(message)
assert message.body_as_str() in ["ServiceBusMessage", "Message2"]
assert len(messages) == 2


Expand Down