-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunction_app.py
53 lines (41 loc) · 1.58 KB
/
function_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
"""
import azure.functions as func
import logging
import requests
import os
app = func.FunctionApp()
@app.event_hub_message_trigger(arg_name="azeventhub", event_hub_name="ci360_events",
connection="CI360EventHubNM_RootManageSharedAccessKey_EVENTHUB")
def eventhub_to_CI(azeventhub: func.EventHubEvent):
logging.info('Python EventHub trigger processed an event: %s',
azeventhub.get_body().decode('utf-8'))
logging.info("Sending Event to CI360.")
# Retrieve URL and token from environment variables with error handling
try:
url = os.environ["ci360_url"]
token = os.environ["token"]
logging.info(f"url: {url}")
except KeyError as e:
logging.error(f"Environment variable {e} not found")
raise
eventbody = azeventhub.get_body().decode('utf-8')
payload = eventbody
headers = {
'Content-type': 'application/json',
'Authorization': 'Bearer ' + token
}
try:
# Make the POST request to the API endpoint
response = requests.post(url, headers=headers, data=payload)
# Log and print the response text
logging.info(f"Response: {response.text}")
print(response.text)
# Check if the request was successful
response.raise_for_status()
except requests.exceptions.RequestException as e:
# Log any errors that occur
logging.error(f"Request failed: {e}")
print(f"Request failed: {e}")