From 9e44f6de36d3374f15186f059c068415b168756c Mon Sep 17 00:00:00 2001 From: emlee518 Date: Fri, 23 Jul 2021 21:52:46 -0400 Subject: [PATCH] Applied aggregation to the data sources and updated how they can be called in main.py. --- app/data/__init__.py | 17 ++++++++++------- app/main.py | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/data/__init__.py b/app/data/__init__.py index 60a75dac..8fb02b4f 100644 --- a/app/data/__init__.py +++ b/app/data/__init__.py @@ -4,18 +4,21 @@ from ..services.location.nyt import NYTLocationService # Mapping of services to data-sources. -DATA_SOURCES = { - "jhu": JhuLocationService(), - "csbs": CSBSLocationService(), - "nyt": NYTLocationService(), -} +class DATASOURCES: + __data_sources = {} + def __init__(self): + self.__date_sources = { + "jhu": JhuLocationService(), + "csbs": CSBSLocationService(), + "nyt": NYTLocationService(), + } -def data_source(source): +def get_data_sources(self, source: str): """ Retrieves the provided data-source service. :returns: The service. :rtype: LocationService """ - return DATA_SOURCES.get(source.lower()) + return self.__data_sources.get(source.lower()) diff --git a/app/main.py b/app/main.py index b9aff949..9966cf71 100644 --- a/app/main.py +++ b/app/main.py @@ -14,7 +14,7 @@ from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from .config import get_settings -from .data import data_source +from .data import DATASOURCES from .routers import V1, V2 from .utils.httputils import setup_client_session, teardown_client_session @@ -74,7 +74,8 @@ async def add_datasource(request: Request, call_next): Attach the data source to the request.state. """ # Retrieve the datas ource from query param. - source = data_source(request.query_params.get("source", default="jhu")) + source = DATASOURCES() + source.get_data_sources(request.query_params.get("source", default="jhu")) # Abort with 404 if source cannot be found. if not source: