From f9bb6deb3ffdb6d87d940a35a3455ef5a1518379 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Thu, 9 Dec 2021 10:18:47 +0000 Subject: [PATCH] add class to load PaNOSC mappings into the code #265 --- datagateway_api/src/common/exceptions.py | 6 ++++ .../src/search_api/panosc_mappings.py | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 datagateway_api/src/search_api/panosc_mappings.py diff --git a/datagateway_api/src/common/exceptions.py b/datagateway_api/src/common/exceptions.py index 1d82ef6a..87498ef6 100644 --- a/datagateway_api/src/common/exceptions.py +++ b/datagateway_api/src/common/exceptions.py @@ -53,3 +53,9 @@ class PythonICATError(ApiError): def __init__(self, msg="Python ICAT error", *args, **kwargs): super().__init__(msg, *args, **kwargs) self.status_code = 500 + + +class SearchAPIError(ApiError): + def __init__(self, msg="Search API error", *args, **kwargs): + super().__init__(msg, *args, **kwargs) + self.status_code = 500 diff --git a/datagateway_api/src/search_api/panosc_mappings.py b/datagateway_api/src/search_api/panosc_mappings.py new file mode 100644 index 00000000..5d3fcd79 --- /dev/null +++ b/datagateway_api/src/search_api/panosc_mappings.py @@ -0,0 +1,29 @@ +import json +from pathlib import Path + +from datagateway_api.src.common.exceptions import SearchAPIError + + +class PaNOSCMappings: + def __init__( + self, path=Path(__file__).parent.parent.parent / "search_api_mapping.json", + ): + try: + with open(path, encoding="utf-8") as target: + self.mappings = json.load(target) + except IOError as e: + raise SearchAPIError(e) + + +# TODO - don't think I'll need this +def load_mappings(path=Path(__file__).parent.parent.parent / "search_api_mapping.json"): + try: + with open(path, encoding="utf-8") as target: + data = json.load(target) + except IOError as e: + raise SearchAPIError(e) + + return data + + +mappings = PaNOSCMappings()