wcag_AI_validation/utils.py

125 lines
3.4 KiB
Python

import json
import time
import urllib.request
import urllib.parse
import logging
import os
exception_msg = "Exception: %s"
def call_API_urlibrequest(
data={},
verbose=False,
url="",
headers=[],
method="post",
base=2, # number of seconds to wait
max_tries=3,
):
if verbose:
logging.info("input_data:%s", data)
# Allow multiple attempts to call the API incase of downtime.
# Return provided response to user after 3 failed attempts.
wait_seconds = [base**i for i in range(max_tries)]
for num_tries in range(max_tries):
try:
if method == "get":
# Encode the parameters and append them to the URL
query_string = urllib.parse.urlencode(data)
url_with_params = f"{url}?{query_string}"
request = urllib.request.Request(url_with_params, method="GET")
for ele in headers:
request.add_header(ele[0], ele[1])
elif method == "post":
# Convert the dictionary to a JSON formatted string and encode it to bytes
data_to_send = json.dumps(data).encode("utf-8")
request = urllib.request.Request(url, data=data_to_send, method="POST")
for ele in headers:
request.add_header(ele[0], ele[1])
else:
return {"error_message": "method_not_allowed"}
# Send the request and capture the response
with urllib.request.urlopen(request) as response:
# Read and decode the response
response_json = json.loads(response.read().decode("utf-8"))
logging.info("response_json:%s", response_json)
logging.info("response.status_code:%s", response.getcode())
return response_json
except Exception as e:
logging.error("error message:%s", e)
response_json = {"error": e}
logging.info("num_tries:%s", num_tries)
logging.info(
"Waiting %s seconds before automatically trying again.",
str(wait_seconds[num_tries]),
)
time.sleep(wait_seconds[num_tries])
logging.info(
"Tried %s times to make API call to get a valid response object", max_tries
)
logging.info("Returning provided response")
return response_json
def disclaim_bool_string(value):
if isinstance(value, str):
if value == "True":
return True
else:
return False
elif isinstance(value, bool):
return value
def prepare_output_folder(file, now_str):
output_dir = ""
try:
output_dir = create_folder(
root_path=os.getcwd(),
directory_separator="/",
next_path="outputs",
)
output_dir = create_folder(
root_path=output_dir,
directory_separator="/",
next_path=file + "_" + now_str,
)
except Exception as e:
logging.error("error prepare output folder:%s", e)
return output_dir
def create_folder(root_path, directory_separator, next_path):
output_dir = root_path + directory_separator + next_path
try:
if not os.path.exists(output_dir):
os.mkdir(output_dir)
except Exception as e:
logging.error(exception_msg, e)
exit(1)
return output_dir