Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,45 @@
from ..services.location.csbs import CSBSLocationService
from ..services.location.jhu import JhuLocationService
from ..services.location.nyt import NYTLocationService
from ..utils.singleton import Singleton

# Mapping of services to data-sources.
DATA_SOURCES = {
"jhu": JhuLocationService(),
"csbs": CSBSLocationService(),
"nyt": NYTLocationService(),
}


def data_source(source):
class DataSources(Singleton):
"""
Retrieves the provided data-source service.

:returns: The service.
:rtype: LocationService
Class to represent the root of the aggregate containing the location services.
"""
return DATA_SOURCES.get(source.lower())

# Mapping of services to data-sources.
__DATA_SOURCES_MAP = {
"jhu": JhuLocationService(),
"csbs": CSBSLocationService(),
"nyt": NYTLocationService(),
}

__instance = None

def __init__(self):
pass

def get_instance():
if DataSources.__instance is None:
DataSources.__instance = DataSources()
return DataSources.__instance

def get_data_source(self, source):
"""
Retrieves the provided data-source service.

:returns: The service.
:rtype: LocationService
"""
return self.__DATA_SOURCES_MAP.get(source.lower())

def get_data_sources(self):
"""
Retrieves a dict of all data sources.

:returns: The dictionary of data sources.
:rtype: dict
"""
return self.__DATA_SOURCES_MAP
8 changes: 5 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -41,6 +41,8 @@
on_shutdown=[teardown_client_session],
)

DATA_SOURCES = DataSources.get_instance()

# #####################
# Middleware
#######################
Expand Down Expand Up @@ -73,8 +75,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"))
# Retrieve the data source from query param.
source = DATA_SOURCES.get_data_source(request.query_params.get("source", default="jhu"))

# Abort with 404 if source cannot be found.
if not source:
Expand Down
5 changes: 3 additions & 2 deletions app/routers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

from fastapi import APIRouter, HTTPException, Request

from ..data import DATA_SOURCES
from ..data import DataSources
from ..models import LatestResponse, LocationResponse, LocationsResponse

V2 = APIRouter()
DATA_SOURCES = DataSources.get_instance()


class Sources(str, enum.Enum):
Expand Down Expand Up @@ -107,4 +108,4 @@ async def sources():
"""
Retrieves a list of data-sources that are availble to use.
"""
return {"sources": list(DATA_SOURCES.keys())}
return {"sources": list(DATA_SOURCES.get_data_sources().keys())}
11 changes: 11 additions & 0 deletions app/utils/singleton.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Singleton(object):
def __new__(cls, *args, **kwds):
it = cls.__dict__.get("__it__")
if it is not None:
return it
cls.__it__ = it = object.__new__(cls)
it.init(*args, **kwds)
return it

def init(self, *args, **kwds):
pass