From 7a6e84e90016b6402f370c645f971510351abe55 Mon Sep 17 00:00:00 2001 From: "Adam Ling (MSFT)" Date: Wed, 10 Feb 2021 07:18:14 -0800 Subject: [PATCH 1/4] align with event data in event hub to have body_as_str on service bus message --- sdk/servicebus/azure-servicebus/CHANGELOG.md | 3 +++ .../azure/servicebus/_common/message.py | 25 ++++++++++++++++++- .../azure-servicebus/tests/test_queues.py | 3 +++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/sdk/servicebus/azure-servicebus/CHANGELOG.md b/sdk/servicebus/azure-servicebus/CHANGELOG.md index e85ed6c56cd8..9bb518570c60 100644 --- a/sdk/servicebus/azure-servicebus/CHANGELOG.md +++ b/sdk/servicebus/azure-servicebus/CHANGELOG.md @@ -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. ## 7.0.1 (2021-01-12) diff --git a/sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py b/sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py index 8b41a9495157..b62467eaad2e 100644 --- a/sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py +++ b/sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py @@ -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 @@ -321,6 +321,29 @@ def body(self): """ return self.message.get_data() + @property + 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] diff --git a/sdk/servicebus/azure-servicebus/tests/test_queues.py b/sdk/servicebus/azure-servicebus/tests/test_queues.py index 5229dd26c0f2..b9de283369ed 100644 --- a/sdk/servicebus/azure-servicebus/tests/test_queues.py +++ b/sdk/servicebus/azure-servicebus/tests/test_queues.py @@ -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 @@ -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 From 2ee83870a15845b55a9227de8bf20e7834a6d04d Mon Sep 17 00:00:00 2001 From: "Adam Ling (MSFT)" Date: Wed, 10 Feb 2021 09:04:52 -0800 Subject: [PATCH 2/4] remove property annotation --- .../azure-servicebus/azure/servicebus/_common/message.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py b/sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py index b62467eaad2e..9c1464d3e925 100644 --- a/sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py +++ b/sdk/servicebus/azure-servicebus/azure/servicebus/_common/message.py @@ -321,7 +321,6 @@ def body(self): """ return self.message.get_data() - @property 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. From 81954eb7a60f8f4e125d8b3c704fdc71ff44651d Mon Sep 17 00:00:00 2001 From: "Adam Ling (MSFT)" Date: Wed, 10 Feb 2021 09:06:20 -0800 Subject: [PATCH 3/4] update test --- sdk/servicebus/azure-servicebus/tests/test_queues.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/servicebus/azure-servicebus/tests/test_queues.py b/sdk/servicebus/azure-servicebus/tests/test_queues.py index b9de283369ed..50b38a9983aa 100644 --- a/sdk/servicebus/azure-servicebus/tests/test_queues.py +++ b/sdk/servicebus/azure-servicebus/tests/test_queues.py @@ -1913,8 +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" + 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 @@ -1932,7 +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 message.body_as_str() in ["ServiceBusMessage", "Message2"] assert len(messages) == 2 From 302755fef8e0b32cd773769ee928484d0d405413 Mon Sep 17 00:00:00 2001 From: "Adam Ling (MSFT)" Date: Thu, 11 Feb 2021 04:13:28 -0800 Subject: [PATCH 4/4] Update sdk/servicebus/azure-servicebus/CHANGELOG.md --- sdk/servicebus/azure-servicebus/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/servicebus/azure-servicebus/CHANGELOG.md b/sdk/servicebus/azure-servicebus/CHANGELOG.md index 9bb518570c60..db151cf33129 100644 --- a/sdk/servicebus/azure-servicebus/CHANGELOG.md +++ b/sdk/servicebus/azure-servicebus/CHANGELOG.md @@ -4,7 +4,7 @@ **New Features** -* Added a `body_as_str` method on `ServiceBusMessage` which returns the content of the message as a string. +* Added a `body_as_str` method on `ServiceBusMessage` and `ServiceBusReceivedMessage` which returns the content of the message as a string. ## 7.0.1 (2021-01-12)