import os from flask import request, has_request_context localData = {} DEFAULT_FILES_SOURCE = "creditfairadmin" PRE_ELIGIBILITY_FILES_SOURCE = "pre_eligibility" def is_pre_eligibility_request(): """ Checks the incoming request payload for the pre_eligibility flag. :return: True when the request is a pre-eligibility call, else False """ if has_request_context() and request.is_json and request.json: pre_elig = request.json.get("pre_eligibility") return pre_elig is True or str(pre_elig).lower() == "true" return False def autoload(data): """ This function reads and parses the config.json at the start of the API call. autoload sets all the respective connection configurations mentioned in the config.json. eg: for s3 connection for bureau and finbit files. :param data: json with all configurations :return: no return statement """ _autoload = data.get("autoload") config = _autoload.get("config") from helper_methods import load_from_S3 if config is not None: for config_name in config.keys(): if config_name in ["s3"]: for c_config_name in config.get(config_name).keys(): if config.get(config_name)[c_config_name] != "::getENV": os.environ[c_config_name] = config.get(config_name)[c_config_name] if config_name in ["files"]: files_config = config.get(config_name) source = PRE_ELIGIBILITY_FILES_SOURCE if is_pre_eligibility_request() else DEFAULT_FILES_SOURCE source_config = files_config.get(source) or files_config.get(DEFAULT_FILES_SOURCE) or {} # drop file configs loaded by a previous call so that a source # without a given file type does not fall back on stale data for other_source in files_config.values(): for c_config_name in other_source.keys(): localData.pop(c_config_name, None) for c_config_name in source_config.keys(): if source_config[c_config_name]["type"] == "s3": localData[c_config_name] = load_from_S3(source_config[c_config_name]) if config_name in ["dataConfig"]: for c_config_name in config.get(config_name).keys(): if config_name not in localData: localData[config_name] = {} localData[config_name][c_config_name] = config.get(config_name)[c_config_name] return localData