diff --git a/caimira/apps/calculator/__init__.py b/caimira/apps/calculator/__init__.py index 7dc383a7..80476955 100644 --- a/caimira/apps/calculator/__init__.py +++ b/caimira/apps/calculator/__init__.py @@ -28,7 +28,6 @@ import tornado.log from . import markdown_tools from . import model_generator from .report_generator import ReportGenerator, calculate_report_data -from .data_service import DataService from .user import AuthenticatedUser, AnonymousUser # The calculator version is based on a combination of the model version and the @@ -105,17 +104,6 @@ class ConcentrationModel(BaseRequestHandler): from pprint import pprint pprint(requested_model_config) start = datetime.datetime.now() - - # Data Service API Integration - fetched_service_data = None - data_service: DataService = self.settings["data_service"] - if self.settings["data_service"]: - try: - fetched_service_data = await data_service.fetch() - except Exception as err: - error_message = f"Something went wrong with the data service: {str(err)}" - LOG.error(error_message, exc_info=True) - self.send_error(500, reason=error_message) try: form = model_generator.FormData.from_dict(requested_model_config) @@ -429,15 +417,6 @@ def make_app( ) template_environment.globals['get_url']=get_root_url template_environment.globals['get_calculator_url']=get_root_calculator_url - - data_service_credentials = { - 'data_service_client_email': os.environ.get('DATA_SERVICE_CLIENT_EMAIL', None), - 'data_service_client_password': os.environ.get('DATA_SERVICE_CLIENT_PASSWORD', None), - } - data_service = None - data_service_enabled = os.environ.get('DATA_SERVICE_ENABLED', 'False').lower() == 'true' - if data_service_enabled: - data_service = DataService(data_service_credentials) if debug: tornado.log.enable_pretty_logging() @@ -456,9 +435,6 @@ def make_app( arve_client_secret=os.environ.get('ARVE_CLIENT_SECRET', None), arve_api_key=os.environ.get('ARVE_API_KEY', None), - # Data Service Integration - data_service=data_service, - # Process parallelism controls. There is a balance between serving a single report # requests quickly or serving multiple requests concurrently. # The defaults are: handle one report at a time, and allow parallelism diff --git a/caimira/apps/calculator/data_service.py b/caimira/store/data_service.py similarity index 92% rename from caimira/apps/calculator/data_service.py rename to caimira/store/data_service.py index f3705970..703884b8 100644 --- a/caimira/apps/calculator/data_service.py +++ b/caimira/store/data_service.py @@ -41,10 +41,11 @@ class DataService(): if (client_email == None or client_password == None): # If the credentials are not defined, an exception is raised. raise Exception("DataService credentials not set") - + http_client = AsyncHTTPClient() headers = {'Content-type': 'application/json'} - json_body = { "email": f"{client_email}", "password": f"{client_password}"} + json_body = {"email": f"{client_email}", + "password": f"{client_password}"} response = await http_client.fetch(HTTPRequest( url=self.host + '/login', @@ -52,7 +53,7 @@ class DataService(): headers=headers, body=json.dumps(json_body), ), - raise_error=True) + raise_error=True) self._access_token = json.loads(response.body)['access_token'] return self._access_token @@ -68,7 +69,6 @@ class DataService(): method='GET', headers=headers, ), - raise_error=True) + raise_error=True) return json.loads(response.body) - \ No newline at end of file diff --git a/caimira/store/global_store.py b/caimira/store/global_store.py index 5d30a021..164af849 100644 --- a/caimira/store/global_store.py +++ b/caimira/store/global_store.py @@ -1,14 +1,17 @@ import os +import logging from caimira.store.data_service import DataService +LOG = logging.getLogger(__name__) + class GlobalStore: ''' Singleton pattern - ensure that there's only one instance of GlobalStore throughout the application ''' - + _instance = None def __new__(self): @@ -28,10 +31,12 @@ class GlobalStore: data_service_enabled = os.environ.get( 'DATA_SERVICE_ENABLED', 'False').lower() == 'true' if data_service_enabled: - data_service = DataService(data_service_credentials) - self._instance = await data_service.fetch() - else: - print('Data service not enabled.') + try: + data_service = DataService(data_service_credentials) + self._instance = await data_service.fetch() + except Exception as err: + error_message = f"Something went wrong with the data service: {str(err)}" + LOG.error(error_message, exc_info=True) @classmethod def get_data(self): diff --git a/caimira/tests/test_data_service.py b/caimira/tests/test_data_service.py index 01cf176c..29c8b9e6 100644 --- a/caimira/tests/test_data_service.py +++ b/caimira/tests/test_data_service.py @@ -4,7 +4,7 @@ import unittest from unittest.mock import patch, MagicMock from tornado.httpclient import HTTPError -from caimira.apps.calculator.data_service import DataService +from caimira.store.data_service import DataService @dataclass class MockResponse: