Skip to content

Commit 074f36d

Browse files
authored
[EventHubs] Get IoT Hub Name from Redirect Address in sample (Azure#19314)
Fixes: Azure#19087 Follows[ C# sample,](https://github.com/Azure/azure-sdk-for-net/blob/350c23ea19ed76d74f7d97dfe3aec82671fdcc9d/samples/iothub-connect-to-eventhubs/IotHubConnection.cs#L97) which also gets IoT Hub name from redirect address.
1 parent 991c638 commit 074f36d

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

sdk/eventhub/azure-eventhub/samples/async_samples/iot_hub_connection_string_receive_async.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""
1616

1717
import os
18+
import re
1819
import time
1920
from base64 import b64encode, b64decode
2021
from hashlib import sha256
@@ -80,6 +81,13 @@ def convert_iothub_to_eventhub_conn_str(iothub_conn_str):
8081
# Once a redirect error is received, close the original client and recreate a new one to the re-directed address
8182
receive_client.close()
8283
fully_qualified_name = redirect.hostname.decode("utf-8")
84+
# Use regular expression to parse the Event Hub name from the IoT Hub redirection address
85+
if redirect.address:
86+
# The regex searches for the Event Hub compatible name in the redirection address. The name is nested in
87+
# between the port and 'ConsumerGroups'.
88+
# (ex. "...servicebus.windows.net:12345/<Event Hub name>/ConsumerGroups/...").
89+
# The regex matches string ':<digits>/', then any characters, then the string '/ConsumerGroups'.
90+
iot_hub_name = re.search(":\d+\/.*/ConsumerGroups", str(redirect.address)).group(0).split("/")[1]
8391
return "Endpoint=sb://{}/;SharedAccessKeyName={};SharedAccessKey={};EntityPath={}".format(
8492
fully_qualified_name,
8593
shared_access_key_name,

0 commit comments

Comments
 (0)