|
| 1 | +# Azure Functions Extensions Bindings ServiceBus library for Python |
| 2 | +This library allows ServiceBus Triggers in Python Function Apps to recognize and bind to client types from the |
| 3 | +Azure ServiceBus sdk. |
| 4 | + |
| 5 | +The SDK types can be generated from: |
| 6 | + |
| 7 | +* ServiceBus Triggers |
| 8 | + |
| 9 | +The supported ServiceBus SDK types include: |
| 10 | + |
| 11 | +* ServiceBusReceivedMessage |
| 12 | + |
| 13 | +[Source code](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-servicebus) |
| 14 | +| |
| 15 | +[Package (PyPi)](https://pypi.org/project/azurefunctions-extensions-bindings-servicebus/) |
| 16 | +| [Samples](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-servicebus/samples) |
| 17 | + |
| 18 | + |
| 19 | +## Getting started |
| 20 | + |
| 21 | +### Prerequisites |
| 22 | +* 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). |
| 23 | + |
| 24 | +* You must have an [Azure subscription](https://azure.microsoft.com/free/) and a |
| 25 | +[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. |
| 26 | + |
| 27 | +### Install the package |
| 28 | +Install the Azure Functions Extensions Bindings ServiceBus library for Python with pip: |
| 29 | + |
| 30 | +```bash |
| 31 | +pip install azurefunctions-extensions-bindings-servicebus |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +### Bind to the SDK-type |
| 36 | +The Azure Functions Extensions Bindings ServiceBus library for Python allows you to create a function app with a ServiceBus Trigger |
| 37 | +and define the type as a ServiceBusReceivedMessage. Instead of receiving |
| 38 | +a ServiceBusMessage, when the function is executed, the type returned will be the defined SDK-type and have all the |
| 39 | +properties and methods available as seen in the Azure ServiceBus library for Python. |
| 40 | + |
| 41 | + |
| 42 | +```python |
| 43 | +import logging |
| 44 | +import azure.functions as func |
| 45 | +import azurefunctions.extensions.bindings.servicebus as servicebus |
| 46 | + |
| 47 | +app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION) |
| 48 | + |
| 49 | +@app.service_bus_queue_trigger(arg_name="receivedmessage", |
| 50 | + queue_name="QUEUE_NAME", |
| 51 | + connection="SERVICEBUS_CONNECTION") |
| 52 | +def servicebus_queue_trigger(receivedmessage: servicebus.ServiceBusReceivedMessage): |
| 53 | + logging.info("Python ServiceBus queue trigger processed message.") |
| 54 | + logging.info("Receiving: %s\n" |
| 55 | + "Body: %s\n" |
| 56 | + "Enqueued time: %s\n" |
| 57 | + "Lock Token: %s\n" |
| 58 | + "Locked until : %s\n" |
| 59 | + "Message ID: %s\n" |
| 60 | + "Sequence number: %s\n", |
| 61 | + receivedmessage, |
| 62 | + receivedmessage.body, |
| 63 | + receivedmessage.enqueued_time_utc, |
| 64 | + receivedmessage.lock_token, |
| 65 | + receivedmessage.locked_until, |
| 66 | + receivedmessage.message_id, |
| 67 | + receivedmessage.sequence_number) |
| 68 | +``` |
| 69 | + |
| 70 | +## Troubleshooting |
| 71 | +### General |
| 72 | +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). |
| 73 | + |
| 74 | +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`. |
| 75 | + |
| 76 | +## Next steps |
| 77 | + |
| 78 | +### More sample code |
| 79 | + |
| 80 | +Get started with our [ServiceBus samples](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-servicebus/samples). |
| 81 | + |
| 82 | +Several samples are available in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Azure ServiceBus: |
| 83 | + |
| 84 | +* [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: |
| 85 | + * From ServiceBus Queue Trigger (Single Message) |
| 86 | + * From ServiceBus Topic Trigger (Single Message) |
| 87 | + |
| 88 | +* [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: |
| 89 | + * From ServiceBus Queue Trigger (Batch) |
| 90 | + * From ServiceBus Topic Trigger (Batch) |
| 91 | + |
| 92 | + |
| 93 | +### Additional documentation |
| 94 | +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 |
| 95 | +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). |
| 96 | + |
| 97 | +## Contributing |
| 98 | +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. |
| 99 | + |
| 100 | +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. |
| 101 | + |
| 102 | +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. |
0 commit comments