def _validate_required_keys(keys, required_keys, scope): """Private function that checks if there is any required key missing. Args: keys [list of strings]: the specification keys required_keys [tuple of string]: the required keys for that scope scope [string]: the scope of the current node: 'root', 'endpoint', 'request' or 'test' """ if not set(required_keys) <= set(keys): missing_keys = set(required_keys) - set(keys) raise MissingMandatoryKeyError(missing_keys, scope)
def scan(): spec_path = settings["spec_path"] try: api_spec = load_config_file(spec_path) except FileNotFoundError as e: error_message = f"Could not find API spec file: {spec_path}. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except EmptyConfigFileError as e: error_message = f"API spec file is empty. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except (yaml.YAMLError, FileFormatNotSupportedError) as e: logger.error(e) raise SystemExit(ExitCode.USAGE_ERROR) try: if API_KEY not in api_spec: raise MissingMandatoryKeyError({API_KEY}, ROOT_SCOPE) root_node = EndpointNode(api_spec[API_KEY]) results = root_node.run() except ( InvalidKeyError, MissingMandatoryKeyError, KeyError, InvalidPythonCodeError, ) as e: error_message = "Error loading API spec." error_message = "{} {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) try: write_report(results) except (BadConfigurationError, InvalidPythonCodeError) as e: logger.error(e) raise SystemExit(ExitCode.USAGE_ERROR) session.exit()
def scan(spec_path, output_path, config_path, reporter, template, log_level): """Automated Testing and Documentation for your REST API.""" logging.basicConfig(level=log_level, format="%(message)s") logger = logging.getLogger(__name__) settings.save_preferences( **{ "spec_path": spec_path, "output_path": output_path, "config_path": config_path, "reporter": reporter, "template": template, }) spec_path = settings["spec_path"] try: api_spec = load_config_file(spec_path) except FileNotFoundError as e: error_message = f"Could not find API spec file: {spec_path}. {str(e)}" logger.error(error_message) return except EmptyConfigFileError as e: error_message = f"API spec file is empty. {str(e)}" logger.error(error_message) return except (yaml.YAMLError, FileFormatNotSupportedError) as e: logger.error(e) return try: if API_KEY not in api_spec: raise MissingMandatoryKeyError({API_KEY}, ROOT_SCOPE) root_node = EndpointNode(api_spec[API_KEY]) Reporter(settings["output_path"], settings["reporter"], settings["template"]).write(root_node.run()) except (InvalidKeyError, MissingMandatoryKeyError, KeyError) as e: error_message = "Error loading API spec." error_message = "{} {}".format(error_message, str(e)) logger.error(error_message) return
def _validate_required_keys(keys, required_keys, scope): """ Private function that checks validation of required keys. """ if not set(required_keys) <= set(keys): missing_keys = set(required_keys) - set(keys) raise MissingMandatoryKeyError(missing_keys, scope)
def missing_mandatory_key(*args, **kwargs): raise MissingMandatoryKeyError(["foo", "bar"], "endpoint")
def _validate_required_keys(keys, required_keys, scope): if not set(required_keys) <= set(keys): missing_keys = set(required_keys) - set(keys) raise MissingMandatoryKeyError(missing_keys, scope)