Skip to content

Commit

Permalink
refactor: small refactors based on PR review comments #260
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Jan 5, 2022
1 parent 3afccd2 commit d368778
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
18 changes: 10 additions & 8 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Only attempt to create a DataGateway API backend if the datagateway_api object
# is present in the config. This ensures that the API does not error on startup
# due to an AttributeError exception being thrown if the object is missing.
if Config.config.datagateway_api is not None:
if Config.config.datagateway_api:
from datagateway_api.src.datagateway_api.backends import create_backend
from datagateway_api.src.datagateway_api.database.helpers import db # noqa: I202
from datagateway_api.src.datagateway_api.icat.icat_client_pool import create_client_pool
Expand All @@ -27,13 +27,15 @@
from datagateway_api.src.resources.non_entities.sessions_endpoints import (
session_endpoints,
)
from datagateway_api.src.resources.search_api_endpoints import (
get_files_endpoint,
get_number_count_endpoint,
get_number_count_files_endpoint,
get_search_endpoint,
get_single_endpoint,
)

if Config.config.search_api:
from datagateway_api.src.resources.search_api_endpoints import (
get_files_endpoint,
get_number_count_endpoint,
get_number_count_files_endpoint,
get_search_endpoint,
get_single_endpoint,
)
from datagateway_api.src.resources.table_endpoints.table_endpoints import (
count_instrument_facility_cycles_endpoint,
count_instrument_investigation_endpoint,
Expand Down
9 changes: 8 additions & 1 deletion datagateway_api/src/search_api/panosc_mappings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import logging
from pathlib import Path
import sys

from datagateway_api.src.common.config import Config
from datagateway_api.src.common.exceptions import SearchAPIError

log = logging.getLogger()
Expand All @@ -17,7 +19,12 @@ def __init__(
log.info("Loading PaNOSC to ICAT mappings from %s", path)
self.mappings = json.load(target)
except IOError as e:
raise SearchAPIError(e)
# The API shouldn't exit if there's an exception (e.g. file not found) if
# the user is only using DataGateway API and not the search API
if Config.config.search_api:
sys.exit(
f"An error occurred while trying to load the PaNOSC mappings: {e}",
)

def get_panosc_related_entity_name(
self, panosc_entity_name, panosc_related_field_name,
Expand Down
34 changes: 15 additions & 19 deletions datagateway_api/src/search_api/query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,22 @@ def get_where_filter(where_filter_input, entity_name):
f", {e.args}",
)

try:
or_conditional_filters = []
field_names = entity_class._text_operator_fields
log.debug(
"Text operators found for PaNOSC %s: %s", entity_name, field_names,
or_conditional_filters = []
field_names = entity_class._text_operator_fields
log.debug(
"Text operators found for PaNOSC %s: %s", entity_name, field_names,
)
if not field_names:
# No text operator fields present, simply log and move on, we should
# ignore text operator queries on entities where `_text_operator_fields`
# is empty (meaning they are not present in the origina PaNOSC data
# model)
log.info(
"No text operator fields found for PaNOSC entity %s, will"
" ignore",
entity_name,
)
if not field_names:
# No text operator fields present, raise KeyError to be caught in
# this try/except block
log.warning(
"No text operator fields found for PaNOSC entity %s, will"
" ignore",
entity_name,
)
raise KeyError()
else:
for field_name in field_names:
or_conditional_filters.append(
{field_name: {"like": where_filter_input["text"]}},
Expand All @@ -169,11 +170,6 @@ def get_where_filter(where_filter_input, entity_name):
where_filter, entity_name,
),
)
except KeyError:
# Do not raise FilterError nor attempt to create filters. Simply
# ignore text operator queries on fields that are not part of the
# text_operator_fields dict.
pass
else:
log.info("Basic where filter found, extracting field, value and operation")
filter_data = SearchAPIQueryFilterFactory.get_condition_values(
Expand Down

0 comments on commit d368778

Please sign in to comment.