test2 / modules /db_connection.py
AIdeaText's picture
Update modules/db_connection.py
6734a9e verified
raw
history blame
No virus
1.45 kB
import requests
import os
import logging
def call_azure_function(operation, data):
function_url = os.getenv('AZURE_FUNCTION_URL')
api_key = os.getenv('AZURE_FUNCTION_API_KEY')
if not function_url or not api_key:
logging.error("AZURE_FUNCTION_URL or AZURE_FUNCTION_API_KEY is not set")
raise ValueError("Azure Function URL or API Key is not configured")
logging.info(f"Calling Azure Function: {function_url}")
logging.info(f"Operation: {operation}")
logging.info(f"Data: {data}")
headers = {
'Content-Type': 'application/json',
'x-functions-key': api_key
}
try:
response = requests.post(function_url, json={'operation': operation, 'data': data}, headers=headers)
logging.info(f"Response status code: {response.status_code}")
logging.info(f"Response content: {response.text}")
if response.status_code == 200:
return response.json()
elif response.status_code == 401:
logging.error("Authentication failed. Check if the API key is correct.")
raise Exception("Authentication failed. Check if the API key is correct.")
else:
logging.error(f"Error calling Azure Function: {response.text}")
raise Exception(f"Error calling Azure Function: {response.text}")
except requests.exceptions.RequestException as e:
logging.error(f"Request exception: {str(e)}")
raise