in setup.py [0:0]
def get_function_key(subscription_id, resource_group, function_app_name, credential):
"""
Returns an API key for the given function.
"""
logging.info(f"Obtaining function key after creating or updating its value.")
accessToken = f"Bearer {credential.get_token('https://management.azure.com/.default').token}"
requestUrl = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.Web/sites/{function_app_name}/functions/document_chunking/keys/mykey?api-version=2022-03-01"
requestHeaders = {
"Authorization": accessToken,
"Content-Type": "application/json"
}
data = {
"properties": {
"name": "mykey"
}
}
max_attempts = 4
for attempt in range(1, max_attempts + 1):
logging.info(f"Attempt {attempt}/{max_attempts} to retrieve function key...")
try:
response = requests.put(requestUrl, headers=requestHeaders, json=data)
response_json = response.json()
except Exception as e:
logging.error(f"Attempt {attempt}: Failed to get a valid JSON response. Error: {str(e)}")
response_json = {}
if "properties" in response_json and "value" in response_json["properties"]:
function_key = response_json["properties"]["value"]
logging.info("Function key retrieved successfully.")
return function_key
else:
logging.error(f"Attempt {attempt}: Function key not found in response. Response: {response_json}")
if attempt < max_attempts:
logging.info("Retrying in 30 seconds...")
time.sleep(30)
logging.error("Failed to retrieve function key after maximum attempts.")
return None