diff --git a/src/hs_ontology_api/routes/fieldassays/fieldassays_controller.py b/src/hs_ontology_api/routes/fieldassays/fieldassays_controller.py index 3d85d29..38c55ac 100644 --- a/src/hs_ontology_api/routes/fieldassays/fieldassays_controller.py +++ b/src/hs_ontology_api/routes/fieldassays/fieldassays_controller.py @@ -2,7 +2,7 @@ # JAS December 2023 from flask import Blueprint, jsonify, current_app, request, make_response from hs_ontology_api.utils.neo4j_logic import field_assays_get_logic -from hs_ontology_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names +from ubkg_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names field_assays_blueprint = Blueprint('field-assays', __name__, url_prefix='/field-assays') diff --git a/src/hs_ontology_api/routes/fielddescriptions/fielddescriptions_controller.py b/src/hs_ontology_api/routes/fielddescriptions/fielddescriptions_controller.py index 497a023..b7e5791 100644 --- a/src/hs_ontology_api/routes/fielddescriptions/fielddescriptions_controller.py +++ b/src/hs_ontology_api/routes/fielddescriptions/fielddescriptions_controller.py @@ -2,7 +2,7 @@ # JAS December 2023 from flask import Blueprint, jsonify, current_app, request, make_response from hs_ontology_api.utils.neo4j_logic import field_descriptions_get_logic -from hs_ontology_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names, \ +from ubkg_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names, \ validate_parameter_value_in_enum field_descriptions_blueprint = Blueprint('field-descriptions', __name__, url_prefix='/field-descriptions') diff --git a/src/hs_ontology_api/routes/fieldentities/fieldentities_controller.py b/src/hs_ontology_api/routes/fieldentities/fieldentities_controller.py index 5eccc1d..6525806 100644 --- a/src/hs_ontology_api/routes/fieldentities/fieldentities_controller.py +++ b/src/hs_ontology_api/routes/fieldentities/fieldentities_controller.py @@ -2,7 +2,7 @@ # JAS January 2024 from flask import Blueprint, jsonify, current_app, request, make_response from hs_ontology_api.utils.neo4j_logic import field_entities_get_logic -from hs_ontology_api.utils.http_error_string import get_404_error_string,validate_query_parameter_names, \ +from ubkg_api.utils.http_error_string import get_404_error_string,validate_query_parameter_names, \ validate_parameter_value_in_enum diff --git a/src/hs_ontology_api/routes/fieldschemas/fieldschemas_controller.py b/src/hs_ontology_api/routes/fieldschemas/fieldschemas_controller.py index 38562b6..e255b2a 100644 --- a/src/hs_ontology_api/routes/fieldschemas/fieldschemas_controller.py +++ b/src/hs_ontology_api/routes/fieldschemas/fieldschemas_controller.py @@ -2,7 +2,7 @@ # JAS January 2024 from flask import Blueprint, jsonify, current_app, request, make_response from hs_ontology_api.utils.neo4j_logic import field_schemas_get_logic -from hs_ontology_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names, \ +from ubkg_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names, \ validate_parameter_value_in_enum field_schemas_blueprint = Blueprint('field-schemas', __name__, url_prefix='/field-schemas') diff --git a/src/hs_ontology_api/routes/fieldtypes/fieldtypes_controller.py b/src/hs_ontology_api/routes/fieldtypes/fieldtypes_controller.py index 68cabd9..9501bc8 100644 --- a/src/hs_ontology_api/routes/fieldtypes/fieldtypes_controller.py +++ b/src/hs_ontology_api/routes/fieldtypes/fieldtypes_controller.py @@ -2,7 +2,7 @@ # JAS December 2023 from flask import Blueprint, jsonify, current_app, request, make_response from hs_ontology_api.utils.neo4j_logic import field_types_get_logic -from hs_ontology_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names, \ +from ubkg_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names, \ validate_parameter_value_in_enum diff --git a/src/hs_ontology_api/routes/fieldtypesinfo/fieldtypesinfo_controller.py b/src/hs_ontology_api/routes/fieldtypesinfo/fieldtypesinfo_controller.py index 5d06935..95d376e 100644 --- a/src/hs_ontology_api/routes/fieldtypesinfo/fieldtypesinfo_controller.py +++ b/src/hs_ontology_api/routes/fieldtypesinfo/fieldtypesinfo_controller.py @@ -2,7 +2,7 @@ # JAS January 2024 from flask import Blueprint, jsonify, current_app, request, make_response from hs_ontology_api.utils.neo4j_logic import field_types_info_get_logic -from hs_ontology_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names, \ +from ubkg_api.utils.http_error_string import get_404_error_string, validate_query_parameter_names, \ validate_parameter_value_in_enum field_types_info_blueprint = Blueprint('field-types-info', __name__, url_prefix='/field-types-info') diff --git a/src/hs_ontology_api/utils/http_error_string.py b/src/hs_ontology_api/utils/http_error_string.py deleted file mode 100644 index 886d9b0..0000000 --- a/src/hs_ontology_api/utils/http_error_string.py +++ /dev/null @@ -1,123 +0,0 @@ -# coding: utf-8 -# Common functions used for format HTTP error messages (404, 400) for endpoints. - -from flask import request - -def format_request_path(): - """ - Formats the request path for an error sgring. - :return: - """ - # The request path will be one of three types: - # 1. The final element will correspond to the endpoint (e.g., /field-descriptions) - # 2. The penultimate element will correspond to the endpoint, and the final element will be a filter. - pathsplit = request.path.split('/') - err = f"{pathsplit[0]} " - if len(pathsplit) > 3: - err = err + f" for path {request.path}" - elif len(pathsplit) == 3: - err = err + f" for '{pathsplit[2]}'" - else: - err = err + f" for '{pathsplit[1]}'" - - return err - -def format_request_query_string(): - """ - Formats teh request query string for error messages. - - :return: - """ - err = '' - - listerr = [] - for req in request.args: - listerr.append(f"'{req}'='{request.args[req]}'") - - if len(listerr) > 0: - err = ' and query parameter' - if len(listerr) > 1: - err = err + 's' - err = err + ' ' + ' ; '.join(listerr) + '.' - - return err - -def get_404_error_string(prompt_string=None): - """ - Formats an error string for a 404 response, accounting for optional parameters. - :param: prompt_string - optional description of error - :return: string - """ - if prompt_string is None: - err = "No values for " - else: - err = prompt_string - - err = err + format_request_path() + format_request_query_string() - - return err - -def get_number_agreement(list_items=None): - """ - Builds a clause with correct number agreement - :param list_items: list of items - :return: - """ - if len(list_items) < 2: - return ' is' - else: - return 's are' - - -def list_as_single_quoted_string(delim: str = ';', list_elements=None): - """Converts the list of elements in list_elements into a string formatted with single quotes-- - e.g., ['a','b','c'] -> "'a'; 'b'; 'c'" - - """ - return f'{delim} '.join(f"'{x}'" for x in list_elements) - - -def validate_query_parameter_names(parameter_name_list=None): - """ - Validates query parameter names in the querystring. Prepares the content of a 404 message if the - querystring includes an unexpected parameter. - :param parameter_name_list: - :return: - - "ok" - - error string for a 400 error - """ - - for req in request.args: - if req not in parameter_name_list: - namelist = list_as_single_quoted_string(list_elements=parameter_name_list) - prompt = get_number_agreement(parameter_name_list) - return f"Invalid query parameter: '{req}'. The possible parameter name{prompt}: {namelist}. " \ - f"Refer to the SmartAPI documentation for this endpoint for more information." - - return "ok" - - -def validate_parameter_value_in_enum(param_name=None, param_value=None, enum_list=None): - """ - Verifies that a parameter's value is a member of a defined set--i.e., the equivalent of in an enumeration. - :param enum_list: list of enum values - :param param_value: value to validate - :param param_name: parameter name - :return: - --"ok" - --error string suitable for a 400 message - """ - - if param_value is None: - return "ok" - - if param_name is None: - return "ok" - - if param_value not in enum_list: - namelist = list_as_single_quoted_string(list_elements=enum_list) - prompt = get_number_agreement(enum_list) - return f"Invalid value for parameter: '{param_name}'. The possible parameter value{prompt}: {namelist}. " \ - f"Refer to the SmartAPI documentation for this endpoint for more information." - - return "ok" diff --git a/test/test_api.sh b/test/test_api.sh index adc2701..89869ea 100755 --- a/test/test_api.sh +++ b/test/test_api.sh @@ -169,8 +169,6 @@ echo echo | tee -a test.out echo | tee -a test.out -exit; - echo "TESTS FOR: dataset-types GET" | tee -a test.out echo "SIGNATURE: /dataset-types?application_context" | tee -a test.out echo | tee -a test.out