Update modules/db_connection.py
Browse files- modules/db_connection.py +15 -6
modules/db_connection.py
CHANGED
|
@@ -1,13 +1,18 @@
|
|
| 1 |
import requests
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
def call_azure_function(operation, data):
|
| 5 |
function_url = os.getenv('AZURE_FUNCTION_URL')
|
| 6 |
api_key = os.getenv('AZURE_FUNCTION_API_KEY')
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
headers = {
|
| 13 |
'Content-Type': 'application/json',
|
|
@@ -16,13 +21,17 @@ def call_azure_function(operation, data):
|
|
| 16 |
|
| 17 |
try:
|
| 18 |
response = requests.post(function_url, json={'operation': operation, 'data': data}, headers=headers)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
if response.status_code == 200:
|
| 23 |
return response.json()
|
|
|
|
|
|
|
|
|
|
| 24 |
else:
|
|
|
|
| 25 |
raise Exception(f"Error calling Azure Function: {response.text}")
|
| 26 |
except requests.exceptions.RequestException as e:
|
| 27 |
-
|
| 28 |
raise
|
|
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
+
import logging
|
| 4 |
|
| 5 |
def call_azure_function(operation, data):
|
| 6 |
function_url = os.getenv('AZURE_FUNCTION_URL')
|
| 7 |
api_key = os.getenv('AZURE_FUNCTION_API_KEY')
|
| 8 |
|
| 9 |
+
if not function_url or not api_key:
|
| 10 |
+
logging.error("AZURE_FUNCTION_URL or AZURE_FUNCTION_API_KEY is not set")
|
| 11 |
+
raise ValueError("Azure Function URL or API Key is not configured")
|
| 12 |
+
|
| 13 |
+
logging.info(f"Calling Azure Function: {function_url}")
|
| 14 |
+
logging.info(f"Operation: {operation}")
|
| 15 |
+
logging.info(f"Data: {data}")
|
| 16 |
|
| 17 |
headers = {
|
| 18 |
'Content-Type': 'application/json',
|
|
|
|
| 21 |
|
| 22 |
try:
|
| 23 |
response = requests.post(function_url, json={'operation': operation, 'data': data}, headers=headers)
|
| 24 |
+
logging.info(f"Response status code: {response.status_code}")
|
| 25 |
+
logging.info(f"Response content: {response.text}")
|
| 26 |
|
| 27 |
if response.status_code == 200:
|
| 28 |
return response.json()
|
| 29 |
+
elif response.status_code == 401:
|
| 30 |
+
logging.error("Authentication failed. Check if the API key is correct.")
|
| 31 |
+
raise Exception("Authentication failed. Check if the API key is correct.")
|
| 32 |
else:
|
| 33 |
+
logging.error(f"Error calling Azure Function: {response.text}")
|
| 34 |
raise Exception(f"Error calling Azure Function: {response.text}")
|
| 35 |
except requests.exceptions.RequestException as e:
|
| 36 |
+
logging.error(f"Request exception: {str(e)}")
|
| 37 |
raise
|