File size: 1,451 Bytes
84ec5fc
28aefca
6734a9e
74ac7c0
84ec5fc
 
0d3433c
28aefca
6734a9e
 
 
 
 
 
 
b122c14
84ec5fc
 
0d3433c
84ec5fc
28aefca
b122c14
 
6734a9e
 
84ec5fc
b122c14
 
6734a9e
 
 
b122c14
6734a9e
b122c14
 
6734a9e
b122c14
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
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