moved data service to separate module and removed logic from caimira calculator module
This commit is contained in:
parent
b1aa3af332
commit
ef212f9455
4 changed files with 16 additions and 35 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue