I created event hub and did message routing from my iothub. Now using azure function Event hub trigger is happening and able to monitor the data in monitor section. I want this data to send to flutter for that I need an API endpoint to call from webapp so that the data can be transmitted to, but I got URL only for http trigger. Not for event hub trigger do I need to invoke URL or I need to use http trigger? What would be the best option? If any alternate option please suggest.
import azure.functions as func
import logging
import json
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
#@app.route(route="http_trigger")
#def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
# logging.info('Python HTTP trigger function processed a request.')
# name = req.params.get('name')
# if not name:
# try:
# req_body = req.get_json()
# except ValueError:
# pass
# else:
# name = req_body.get('name')
# if name:
# return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
# else:
# return func.HttpResponse(
# "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
# status_code=200
# )
@app.event_hub_message_trigger(arg_name="azeventhub", event_hub_name="eventhubiot",
connection="AZHUB_events_IOTHUB")
def eventhub_trigger(azeventhub: func.EventHubEvent)-> func.HttpResponse:
logging.info('Python EventHub trigger processed an event: %s',
azeventhub.get_body().decode('utf-8'))
device_id = None
temperature = None
for azeventhub in azeventhub.get_body().decode('utf-8').split('\n'):
if azeventhub.strip():
key, value = azeventhub.split(':')
if key.strip() == 'device_id':
device_id = value.strip()
elif key.strip() == 'temperature':
temperature = value.strip()
response_body = json.dumps(response_body)
return func.HttpResponse(response_body, status_code=200, mimetype='application/json')
This is the code I'm using, above commented is previously created http trigger. I used builtin endpoint here to read data It is working fine.
The below Http Azure Function code that consumes events from an Azure Event Hub and then processes them.
Output: