Skip to content

Commit 0c03860

Browse files
author
Evan Roman
committed
Address
1 parent ef85906 commit 0c03860

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

azurefunctions-extensions-bindings-blob/azurefunctions/extensions/bindings/blob/blobClient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
class BlobClient(SdkType):
14-
def __init__(self, *, data: Union[bytes, Datum]) -> None:
14+
def __init__(self, *, data: Datum) -> None:
1515
# model_binding_data properties
1616
self._data = data
1717
self._using_managed_identity = False
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
from typing import Union
4+
from typing import Optional, Union
55
import uamqp
66

77
from azure.eventhub import EventData
88
from azurefunctions.extensions.base import Datum, SdkType
99

1010

1111
class EventHubData(SdkType):
12-
def __init__(self, *, data: Union[bytes, Datum]) -> None:
12+
def __init__(self, *, data: Datum) -> None:
1313
# model_binding_data properties
1414
self._data = data
1515
self._version = None
@@ -25,14 +25,28 @@ def __init__(self, *, data: Union[bytes, Datum]) -> None:
2525
self.decoded_message = self.__get_eventhub_content(self._content)
2626

2727
def __get_eventhub_content(self, content):
28+
"""
29+
When receiving the EventBindingData, the content field is in the form of bytes.
30+
This content must be decoded in order to construct an EventData object from the azure.eventhub SDK.
31+
The .NET worker uses the Azure.Core.Amqp library to do this:
32+
https://github.com/Azure/azure-functions-dotnet-worker/blob/main/extensions/Worker.Extensions.EventHubs/src/EventDataConverter.cs#L45
33+
"""
2834
if content:
29-
return uamqp.Message().decode_from_bytes(content)
30-
else:
31-
return None
35+
try:
36+
return uamqp.Message().decode_from_bytes(content)
37+
except Exception as e:
38+
raise ValueError(f"Failed to decode EventHub content: {e}") from e
39+
40+
return None
3241

33-
def get_sdk_type(self):
42+
def get_sdk_type(self) -> Optional[EventData]:
43+
"""
44+
When receiving an EventHub message, the content portion after being decoded
45+
is used in the constructor to create an EventData object. This will contains
46+
fields such as message, enqueue_time, and more.
47+
"""
3448
# https://github.com/Azure/azure-sdk-for-python/issues/39711
3549
if self.decoded_message:
3650
return EventData._from_message(self.decoded_message)
37-
else:
38-
return None
51+
52+
return None
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
from typing import Any
4+
from typing import Any, Optional
55

66
from azurefunctions.extensions.base import Datum, InConverter, OutConverter
77

@@ -21,22 +21,20 @@ def check_input_type_annotation(cls, pytype: type) -> bool:
2121
)
2222

2323
@classmethod
24-
def decode(cls, data: Datum, *, trigger_metadata, pytype) -> Any:
24+
def decode(cls, data: Datum, *, trigger_metadata, pytype) -> Optional[Any]:
2525
if data is None or data.type is None:
2626
return None
2727

28-
data_type = data.type
29-
30-
if data_type == "model_binding_data":
31-
data = data.value
32-
else:
28+
if data.type != "model_binding_data":
3329
raise ValueError(
34-
f'unexpected type of data received for the "eventhub" binding '
35-
f": {data_type!r}"
30+
"Unexpected type of data received for the 'eventhub' binding: "
31+
+ repr(data.type)
3632
)
33+
34+
content = data.value
3735

3836
# Determines which sdk type to return based on pytype
3937
if pytype == EventHubData:
40-
return EventHubData(data=data).get_sdk_type()
41-
else:
42-
return None
38+
return EventHubData(data=content).get_sdk_type()
39+
40+
return None

azurefunctions-extensions-bindings-eventhub/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ classifiers= [
1717
'Programming Language :: Python :: 3.9',
1818
'Programming Language :: Python :: 3.10',
1919
'Programming Language :: Python :: 3.11',
20+
'Programming Language :: Python :: 3.12',
2021
'Operating System :: Microsoft :: Windows',
2122
'Operating System :: POSIX',
2223
'Operating System :: MacOS :: MacOS X',

0 commit comments

Comments
 (0)