-
Notifications
You must be signed in to change notification settings - Fork 2
feat: supporting EventHub trigger SDK bindings #101
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 12 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
4b4f59d
Chng
14a277c
Merge remote-tracking branch 'origin/dev' into evanroman/ehub-bind
6ad56e0
Rm leftovers
7ebe2d2
Fix docs
90d8fae
Fix docs
205c16a
Add to CI
48af64c
Address
3d186b9
Fix docs
ef85906
Fix docs
0c03860
Address
26b891a
Address
239505b
Rm
e5620bf
Add
5cc2b74
Add
1a2910b
Fix
46eb74e
Lint
555df8f
Fix
4ef5019
Fix
f4577f5
Fix
3f73d14
Refactor
bddbc9a
Fix
ab3a7d7
Fix version
c883fe7
More tests
521872e
More tests
c04e320
Add batch sample
d3ec5f3
Merge branch 'dev' into evanroman/ehub-bind
EvanR-Dev 5a7c4b2
Rm 39
76c1950
Lint
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
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
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
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
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
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
2 changes: 0 additions & 2 deletions
2
azurefunctions-extensions-bindings-blob/samples/blob_samples_blobclient/function_app.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
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
2 changes: 0 additions & 2 deletions
2
azurefunctions-extensions-bindings-blob/samples/blob_samples_containerclient/function_app.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
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
2 changes: 0 additions & 2 deletions
2
...ons-extensions-bindings-blob/samples/blob_samples_storagestreamdownloader/function_app.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
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
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
Empty file.
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 |
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,92 @@ | ||
# Azure Functions Extensions Bindings EventHub library for Python | ||
This library allows an EventHub Trigger binding in Python Function Apps to recognize and bind to the types from the | ||
Azure EventHub sdk (EventData). | ||
|
||
EventHub types can be generated from: | ||
|
||
[Source code](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-eventhub) | ||
[Package (PyPi)](https://pypi.org/project/azurefunctions-extensions-bindings-eventhub/) | ||
| API reference documentation | ||
| Product documentation | ||
| [Samples](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-eventhub/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 an | ||
[Azure storage account](https://docs.microsoft.com/azure/storage/common/storage-account-overview) to use this package. | ||
|
||
### Install the package | ||
Install the Azure Functions Extensions Bindings EventHub library for Python with pip: | ||
|
||
```bash | ||
pip install azurefunctions-extensions-bindings-eventhub | ||
``` | ||
|
||
### Create a storage account | ||
If you wish to create a new storage account, you can use the | ||
[Azure Portal](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-portal), | ||
[Azure PowerShell](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-powershell), | ||
or [Azure CLI](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-cli): | ||
|
||
```bash | ||
# Create a new resource group to hold the storage account - | ||
# if using an existing resource group, skip this step | ||
az group create --name my-resource-group --location westus2 | ||
|
||
# Create the storage account | ||
az storage account create -n my-storage-account-name -g my-resource-group | ||
``` | ||
|
||
### Bind to the SDK-type | ||
The Azure Functions Extensions Bindings EventHub library for Python allows you to create a function app with an EventHub Trigger | ||
and define the type as an EventData. Instead of receiving an EventHubEvent, when the function is executed, the type returned will be the defined SDK-type and have all of the properties and methods available as seen in the Azure EventHub library for Python. | ||
|
||
|
||
```python | ||
import logging | ||
import azure.functions as func | ||
import azurefunctions.extensions.bindings.eventhub as eh | ||
|
||
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS) | ||
|
||
@app.event_hub_message_trigger( | ||
arg_name="eh_data", event_hub_name="EVENTHUB_NAME", connection="AzureWebJobsStorage" | ||
) | ||
def eventhub_trigger(eh_data: eh.EventHubData): | ||
logging.info( | ||
"Python EventHub trigger processed an event %s", | ||
eh_data.body_as_str() | ||
) | ||
``` | ||
|
||
## 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 [EventHub samples](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-eventhub/samples). | ||
|
||
Several samples are available in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with EventHubs: | ||
|
||
* [eventhub_samples_eventhubdata](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-eventhub/samples/eventhub_samples_eventhubdata) - Examples for using the EventHubData type: | ||
* From EventHubTrigger | ||
|
||
### Additional documentation | ||
For more information on the Azure EventHub SDK, see the [Azure EventHub documentation](https://learn.microsoft.com/en-us/azure/event-hubs/) on learn.microsoft.com | ||
and the [Azure EventHub README](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/eventhub/azure-eventhub/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-eventhub/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-eventhub/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-eventhub/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
...ions-extensions-bindings-eventhub/azurefunctions/extensions/bindings/eventhub/__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 .eventHubData import EventHubData | ||
from .eventHubDataConverter import EventHubDataConverter | ||
|
||
__all__ = [ | ||
"EventHubData", | ||
"EventHubDataConverter", | ||
] | ||
|
||
__version__ = "1.0.0b1" |
52 changes: 52 additions & 0 deletions
52
...-extensions-bindings-eventhub/azurefunctions/extensions/bindings/eventhub/eventHubData.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,52 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from typing import Optional | ||
import uamqp | ||
|
||
from azure.eventhub import EventData | ||
from azurefunctions.extensions.base import Datum, SdkType | ||
|
||
|
||
class EventHubData(SdkType): | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__(self, *, data: Datum) -> None: | ||
# 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: | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
self._content = data.content | ||
self.decoded_message = self.__get_eventhub_content(self._content) | ||
|
||
def __get_eventhub_content(self, content): | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
When receiving the EventBindingData, the content field is in the form of bytes. | ||
This content must be decoded in order to construct an EventData object from the azure.eventhub SDK. | ||
The .NET worker uses the Azure.Core.Amqp library to do this: | ||
https://github.com/Azure/azure-functions-dotnet-worker/blob/main/extensions/Worker.Extensions.EventHubs/src/EventDataConverter.cs#L45 | ||
""" | ||
if content: | ||
try: | ||
return uamqp.Message().decode_from_bytes(content) | ||
except Exception as e: | ||
raise ValueError(f"Failed to decode EventHub content: {e}") from e | ||
|
||
return None | ||
|
||
def get_sdk_type(self) -> Optional[EventData]: | ||
""" | ||
When receiving an EventHub message, the content portion after being decoded | ||
is used in the constructor to create an EventData object. This will contain | ||
fields such as message, enqueue_time, and more. | ||
""" | ||
# https://github.com/Azure/azure-sdk-for-python/issues/39711 | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if self.decoded_message: | ||
return EventData._from_message(self.decoded_message) | ||
|
||
return None |
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.