in setup.py [0:0]
def call_search_api(search_service, search_api_version, resource_type, resource_name, method, credential, body=None):
"""
Calls the Azure Search API with the specified parameters.
"""
token = credential.get_token("https://search.azure.com/.default").token
headers = {
"Authorization": f"Bearer {token}",
'Content-Type': 'application/json'
}
search_endpoint = f"https://{search_service}.search.windows.net/{resource_type}/{resource_name}?api-version={search_api_version}"
response = None
try:
if method not in ["get", "put", "delete"]:
logging.warning(f"[call_search_api] Invalid method {method} ")
if method == "get":
response = requests.get(search_endpoint, headers=headers)
elif method == "put":
response = requests.put(search_endpoint, headers=headers, json=body)
if method == "delete":
response = requests.delete(search_endpoint, headers=headers)
status_code = response.status_code
logging.info(f"[call_search_api] Successfully called search API {method} {resource_type} {resource_name}. Code: {status_code}.")
if response is not None:
status_code = response.status_code
if status_code >= 400:
logging.warning(f"[call_search_api] {status_code} code when calling search API {method} {resource_type} {resource_name}. Reason: {response.reason}.")
try:
response_text_dict = json.loads(response.text)
logging.warning(f"[call_search_api] {status_code} code when calling search API {method} {resource_type} {resource_name}. Message: {response_text_dict['error']['message']}")
except json.JSONDecodeError:
logging.warning(f"[call_search_api] {status_code} Response is not valid JSON. Raw response:\n{response.text}")
else:
logging.info(f"[call_search_api] Successfully called search API {method} {resource_type} {resource_name}. Code: {status_code}.")
except Exception as e:
error_message = str(e)
logging.error(f"Error when calling search API {method} {resource_type} {resource_name}. Error: {error_message}")