diff --git a/app/data/__init__.py b/app/data/__init__.py index 60a75dac..f115d4e1 100644 --- a/app/data/__init__.py +++ b/app/data/__init__.py @@ -4,18 +4,29 @@ from ..services.location.nyt import NYTLocationService # Mapping of services to data-sources. -DATA_SOURCES = { - "jhu": JhuLocationService(), - "csbs": CSBSLocationService(), - "nyt": NYTLocationService(), -} -def data_source(source): - """ - Retrieves the provided data-source service. +class DataSources: - :returns: The service. - :rtype: LocationService - """ - return DATA_SOURCES.get(source.lower()) + def __init__(self): + """ + Sets __data_source's key-value pairs to be the data-source (key) and data-source service (value) for the supported sources (jhu, csbs, nyt) + """ + self.__data_source = { + "jhu": JhuLocationService(), + "csbs": CSBSLocationService(), + "nyt": NYTLocationService(), + } + + def get_data_sources(self): + """Return the keys stored in __data_source""" + return self.__data_source.keys() + + def get_data_source_service(self, source): + """ + Retrieves the provided data-source service. + + :returns: The service. + :rtype: LocationService + """ + return self.__data_source.get(source.lower()) diff --git a/app/main.py b/app/main.py index b9aff949..363be959 100644 --- a/app/main.py +++ b/app/main.py @@ -14,10 +14,15 @@ 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 +# ############ +# Constructing DataSources object for retriving data source services +# ############ +dataSources = DataSources() + # ############ # FastAPI App # ############ @@ -74,8 +79,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.get_data_source_service( + request.query_params.get("source", default="jhu")) # Abort with 404 if source cannot be found. if not source: return Response("The provided data-source was not found.", status_code=404) diff --git a/app/routers/v2.py b/app/routers/v2.py index 31eb408c..baf6062c 100644 --- a/app/routers/v2.py +++ b/app/routers/v2.py @@ -3,9 +3,15 @@ from fastapi import APIRouter, HTTPException, Request -from ..data import DATA_SOURCES +from ..data import DataSources from ..models import LatestResponse, LocationResponse, LocationsResponse +# ############ +# Constructing DataSources object for retriving data source services +# ############ +dataSources = DataSources() + + V2 = APIRouter() @@ -107,4 +113,4 @@ async def sources(): """ Retrieves a list of data-sources that are availble to use. """ - return {"sources": list(DATA_SOURCES.keys())} + return {"sources": list(dataSources.get_data_sources)}