-
Notifications
You must be signed in to change notification settings - Fork 2
feat: support ServiceBus SDK-type bindings #107
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cf26e82
Initial changes to support sbrm
037100e
missed return
94b4fd0
add test pipeline, start readme
hallvictoria 9a37705
Merge branch 'dev' of https://github.com/Azure/azure-functions-python…
hallvictoria 866c74e
add to pipelines, update tests, lint
hallvictoria 64b0cfb
lint, samples, fix pyproject
2d3b737
merge
69ee203
Merge branch 'dev' into hallvictoria/servicebus
hallvictoria a458552
move to pydocs
d55a0ea
Update readme, final samples
hallvictoria d92acce
lower version
hallvictoria 6dad9b2
feedback + run all tests as part of public build
75c0b68
feedback
hallvictoria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Copyright (c) Microsoft Corporation. | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
recursive-include azure *.py *.pyi | ||
recursive-include tests *.py | ||
include LICENSE README.md |
102 changes: 102 additions & 0 deletions
102
azurefunctions-extensions-bindings-servicebus/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Azure Functions Extensions Bindings ServiceBus library for Python | ||
This library allows ServiceBus Triggers in Python Function Apps to recognize and bind to client types from the | ||
Azure ServiceBus sdk. | ||
|
||
The SDK types can be generated from: | ||
|
||
* ServiceBus Triggers | ||
|
||
The supported ServiceBus SDK types include: | ||
|
||
* ServiceBusReceivedMessage | ||
|
||
[Source code](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-servicebus) | ||
| | ||
[Package (PyPi)](https://pypi.org/project/azurefunctions-extensions-bindings-servicebus/) | ||
| [Samples](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-servicebus/samples) | ||
|
||
|
||
## Getting started | ||
|
||
### Prerequisites | ||
* Python 3.9 or later is required to use this package. For more details, please read our page on [Python Functions version support policy](https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=isolated-process%2Cv4&pivots=programming-language-python#languages). | ||
|
||
* You must have an [Azure subscription](https://azure.microsoft.com/free/) and a | ||
[ServiceBus Resource](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus?tabs=isolated-process%2Cextensionv5%2Cextensionv3&pivots=programming-language-python) to use this package. | ||
|
||
### Install the package | ||
Install the Azure Functions Extensions Bindings ServiceBus library for Python with pip: | ||
|
||
```bash | ||
pip install azurefunctions-extensions-bindings-servicebus | ||
``` | ||
|
||
|
||
### Bind to the SDK-type | ||
The Azure Functions Extensions Bindings ServiceBus library for Python allows you to create a function app with a ServiceBus Trigger | ||
and define the type as a ServiceBusReceivedMessage. Instead of receiving | ||
a ServiceBusMessage, when the function is executed, the type returned will be the defined SDK-type and have all the | ||
properties and methods available as seen in the Azure ServiceBus library for Python. | ||
|
||
|
||
```python | ||
import logging | ||
import azure.functions as func | ||
import azurefunctions.extensions.bindings.servicebus as servicebus | ||
|
||
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION) | ||
|
||
@app.service_bus_queue_trigger(arg_name="receivedmessage", | ||
queue_name="QUEUE_NAME", | ||
connection="SERVICEBUS_CONNECTION") | ||
def servicebus_queue_trigger(receivedmessage: servicebus.ServiceBusReceivedMessage): | ||
logging.info("Python ServiceBus queue trigger processed message.") | ||
logging.info("Receiving: %s\n" | ||
"Body: %s\n" | ||
"Enqueued time: %s\n" | ||
"Lock Token: %s\n" | ||
"Locked until : %s\n" | ||
"Message ID: %s\n" | ||
"Sequence number: %s\n", | ||
receivedmessage, | ||
receivedmessage.body, | ||
receivedmessage.enqueued_time_utc, | ||
receivedmessage.lock_token, | ||
receivedmessage.locked_until, | ||
receivedmessage.message_id, | ||
receivedmessage.sequence_number) | ||
``` | ||
|
||
## Troubleshooting | ||
### General | ||
The SDK-types raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/README.md). | ||
|
||
This list can be used for reference to catch thrown exceptions. To get the specific error code of the exception, use the `error_code` attribute, i.e, `exception.error_code`. | ||
|
||
## Next steps | ||
|
||
### More sample code | ||
|
||
Get started with our [ServiceBus samples](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-servicebus/samples). | ||
|
||
Several samples are available in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Azure ServiceBus: | ||
|
||
* [servicebus_samples_single](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-servicebus/samples/servicebus_samples_single) - Examples for using the ServiceBusReceivedMessage type: | ||
* From ServiceBus Queue Trigger (Single Message) | ||
* From ServiceBus Topic Trigger (Single Message) | ||
|
||
* [servicebus_samples_batch](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-servicebus/samples/service_samples_batch) - Examples for interacting with batches: | ||
* From ServiceBus Queue Trigger (Batch) | ||
* From ServiceBus Topic Trigger (Batch) | ||
|
||
|
||
### Additional documentation | ||
For more information on the Azure ServiceBus SDK, see the [Azure ServiceBus SDK documentation](https://learn.microsoft.com/en-us/python/api/overview/azure/servicebus-readme?view=azure-python) on docs.microsoft.com | ||
and the [Azure ServiceBus README](https://github.com/Azure/azure-sdk-for-python/blob/azure-servicebus_7.14.1/sdk/servicebus/azure-servicebus/README.md). | ||
|
||
## Contributing | ||
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com. | ||
|
||
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA. | ||
|
||
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
1 change: 1 addition & 0 deletions
1
azurefunctions-extensions-bindings-servicebus/azurefunctions/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__path__ = __import__("pkgutil").extend_path(__path__, __name__) |
1 change: 1 addition & 0 deletions
1
azurefunctions-extensions-bindings-servicebus/azurefunctions/extensions/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__path__ = __import__("pkgutil").extend_path(__path__, __name__) |
1 change: 1 addition & 0 deletions
1
azurefunctions-extensions-bindings-servicebus/azurefunctions/extensions/bindings/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__path__ = __import__("pkgutil").extend_path(__path__, __name__) |
12 changes: 12 additions & 0 deletions
12
...-extensions-bindings-servicebus/azurefunctions/extensions/bindings/servicebus/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from .serviceBusReceivedMessage import ServiceBusReceivedMessage | ||
from .serviceBusConverter import ServiceBusConverter | ||
|
||
__all__ = [ | ||
"ServiceBusReceivedMessage", | ||
"ServiceBusConverter", | ||
] | ||
|
||
__version__ = '1.0.0a1' |
74 changes: 74 additions & 0 deletions
74
...-bindings-servicebus/azurefunctions/extensions/bindings/servicebus/serviceBusConverter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import collections.abc | ||
from typing import Any, get_args, get_origin | ||
|
||
from azurefunctions.extensions.base import Datum, InConverter | ||
from .serviceBusReceivedMessage import ServiceBusReceivedMessage | ||
|
||
|
||
class ServiceBusConverter( | ||
InConverter, | ||
binding='serviceBusTrigger', trigger=True | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
@classmethod | ||
def check_input_type_annotation(cls, pytype: type) -> bool: | ||
if pytype is None: | ||
return False | ||
|
||
# The annotation is a class/type (not an object) - not iterable | ||
if (isinstance(pytype, type) | ||
and issubclass(pytype, ServiceBusReceivedMessage)): | ||
return True | ||
|
||
# An iterable who only has one inner type and is a subclass of SdkType | ||
return cls._is_iterable_supported_type(pytype) | ||
|
||
@classmethod | ||
def _is_iterable_supported_type(cls, annotation: type) -> bool: | ||
# Check base type from type hint. Ex: List from List[SdkType] | ||
base_type = get_origin(annotation) | ||
if (base_type is None | ||
or not issubclass(base_type, collections.abc.Iterable)): | ||
return False | ||
|
||
inner_types = get_args(annotation) | ||
if inner_types is None or len(inner_types) != 1: | ||
return False | ||
|
||
inner_type = inner_types[0] | ||
|
||
return (isinstance(inner_type, type) | ||
and issubclass(inner_type, ServiceBusReceivedMessage)) | ||
|
||
@classmethod | ||
def decode(cls, data: Datum, *, trigger_metadata, pytype) -> Any: | ||
""" | ||
ServiceBus allows for batches to be sent. The cardinality can be one or many. | ||
When the cardinality is one: | ||
- The data is of type "model_binding_data" - each event is an independent | ||
function invocation | ||
When the cardinality is many: | ||
- The data is of type "collection_model_binding_data" - all events are sent | ||
in a single function invocation | ||
- collection_model_binding_data has 1 or more model_binding_data objects | ||
""" | ||
if data is None or data.type is None: | ||
return None | ||
|
||
data_type = data.type | ||
|
||
if data_type == "model_binding_data": | ||
return ServiceBusReceivedMessage(data=data.value).get_sdk_type() | ||
elif data_type == "collection_model_binding_data": | ||
try: | ||
return [ServiceBusReceivedMessage(data=mbd).get_sdk_type() | ||
for mbd in data.value.model_binding_data] | ||
except Exception as e: | ||
raise ValueError("Failed to decode incoming ServiceBus batch: " | ||
+ repr(e)) from e | ||
else: | ||
raise ValueError( | ||
"Unexpected type of data received for the 'servicebus' binding: " | ||
+ repr(data.type)) |
33 changes: 33 additions & 0 deletions
33
...ngs-servicebus/azurefunctions/extensions/bindings/servicebus/serviceBusReceivedMessage.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from azure.servicebus import ServiceBusReceivedMessage as ServiceBusReceivedMessageSdk | ||
from azurefunctions.extensions.base import Datum, SdkType | ||
from .utils import get_decoded_message | ||
|
||
|
||
class ServiceBusReceivedMessage(SdkType): | ||
def __init__(self, *, data: Datum) -> None: | ||
gavin-aguiar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# model_binding_data properties | ||
self._data = data | ||
self._version = None | ||
self._source = None | ||
self._content_type = None | ||
self._content = None | ||
self._decoded_message = None | ||
if self._data: | ||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
self._content = data.content | ||
self._decoded_message = get_decoded_message(self._content) | ||
|
||
def get_sdk_type(self): | ||
""" | ||
Returns a ServiceBusReceivedMessage. | ||
Message settling is not yet supported. | ||
""" | ||
if self._decoded_message: | ||
hallvictoria marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ServiceBusReceivedMessageSdk(self._decoded_message, receiver=None) | ||
hallvictoria marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
return None | ||
hallvictoria marked this conversation as resolved.
Show resolved
Hide resolved
|
53 changes: 53 additions & 0 deletions
53
...ons-extensions-bindings-servicebus/azurefunctions/extensions/bindings/servicebus/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import uamqp | ||
import uuid | ||
|
||
|
||
_X_OPT_LOCK_TOKEN = b"x-opt-lock-token" | ||
|
||
|
||
def get_lock_token(message: bytes, index: int) -> str: | ||
hallvictoria marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Get the lock token from the message | ||
lock_token_encoded = message[:index] | ||
|
||
# Convert the lock token to a UUID using the first 16 bytes | ||
lock_token_uuid = uuid.UUID(bytes=lock_token_encoded[:16]) | ||
|
||
return lock_token_uuid | ||
|
||
|
||
def get_amqp_message(message: bytes, index: int): | ||
""" | ||
Get the amqp message from the model_binding_data content | ||
and create the message. | ||
""" | ||
amqp_message = message[index + len(_X_OPT_LOCK_TOKEN):] | ||
decoded_message = uamqp.Message().decode_from_bytes(amqp_message) | ||
|
||
return decoded_message | ||
|
||
|
||
def get_decoded_message(content: bytes): | ||
hallvictoria marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
First, find the end of the lock token. Then, | ||
get the lock token UUID and create the delivery | ||
annotations dictionary. Finally, get the amqp message | ||
and set the delivery annotations. Once the delivery | ||
annotations have been set, the amqp message is ready to | ||
return. | ||
""" | ||
if content: | ||
try: | ||
index = content.find(_X_OPT_LOCK_TOKEN) | ||
|
||
lock_token = get_lock_token(content, index) | ||
delivery_anno_dict = {_X_OPT_LOCK_TOKEN: lock_token} | ||
|
||
decoded_message = get_amqp_message(content, index) | ||
decoded_message.delivery_annotations = delivery_anno_dict | ||
return decoded_message | ||
except Exception as e: | ||
raise ValueError(f"Failed to decode ServiceBus content: {e}") from e | ||
return None |
52 changes: 52 additions & 0 deletions
52
azurefunctions-extensions-bindings-servicebus/pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
[build-system] | ||
requires = ["setuptools >= 61.0"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "azurefunctions-extensions-bindings-servicebus" | ||
dynamic = ["version"] | ||
requires-python = ">=3.9" | ||
authors = [{ name = "Azure Functions team at Microsoft Corp.", email = "[email protected]"}] | ||
description = "ServiceBus Python worker extension for Azure Functions." | ||
readme = "README.md" | ||
license = {text = "MIT License"} | ||
classifiers= [ | ||
'License :: OSI Approved :: MIT License', | ||
'Intended Audience :: Developers', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: 3.9', | ||
'Programming Language :: Python :: 3.10', | ||
'Programming Language :: Python :: 3.11', | ||
'Programming Language :: Python :: 3.12', | ||
'Programming Language :: Python :: 3.13', | ||
'Operating System :: Microsoft :: Windows', | ||
'Operating System :: POSIX', | ||
'Operating System :: MacOS :: MacOS X', | ||
'Environment :: Web Environment', | ||
'Development Status :: 5 - Production/Stable', | ||
] | ||
dependencies = [ | ||
'azurefunctions-extensions-base', | ||
'azure-servicebus~=7.14.2', | ||
'uamqp~=1.6.11' | ||
] | ||
|
||
[project.optional-dependencies] | ||
dev = [ | ||
'flake8', | ||
'mypy', | ||
'pytest', | ||
'pytest-cov', | ||
'coverage', | ||
'pytest-instafail', | ||
'pre-commit' | ||
] | ||
|
||
[tool.setuptools.dynamic] | ||
version = {attr = "azurefunctions.extensions.bindings.servicebus.__version__"} | ||
|
||
[tool.setuptools.packages.find] | ||
exclude = [ | ||
'azurefunctions.extensions.bindings','azurefunctions.extensions', | ||
'azurefunctions', 'tests', 'samples' | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.