Skip to content
Prev Previous commit
Next Next commit
EECS 3311: Creational Design Pattern - Singleton
  • Loading branch information
DukeM23 committed Aug 16, 2021
commit 209f36f682e9a61aa510ad1f07b956c9bb4840b1
41 changes: 28 additions & 13 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@
from ..services.location.jhu import JhuLocationService
from ..services.location.nyt import NYTLocationService

# Mapping of services to data-sources.
DATA_SOURCES = {
"jhu": JhuLocationService(),
"csbs": CSBSLocationService(),
"nyt": NYTLocationService(),
}
class DataSourceSingletonMeta(type):
"""
access point to the DataSourceSingleton
"""
_instances = {}

def __call__(cls, *args, **kwargs):
"""
Possible changes to the value of the `__init__` argument do not affect
the returned instance.
"""
if cls not in cls._instances:
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]

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

:returns: The service.
:rtype: LocationService
"""
return DATA_SOURCES.get(source.lower())
class DataSourceSingleton(metaclass=DataSourceSingletonMeta):
DATA_SOURCES = {
"jhu": JhuLocationService(),
"csbs": CSBSLocationService(),
"nyt": NYTLocationService(),
}
def get_data_source(self, dataSource):
return self.DATA_SOURCES.get(dataSource.lower())
...

def get_data_source_list(self):
return self.DATA_SOURCES


9 changes: 7 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

from .config import get_settings
from .data import data_source
# from .data import data_source
from .data import DataSourceSingleton
from .routers import V1, V2
from .utils.httputils import setup_client_session, teardown_client_session

Expand Down Expand Up @@ -73,8 +74,12 @@ 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 = DataSourceSingleton().get_data_source(request.query_params.get("source", default="jhu"))
# data_source(request.query_params.get("source", default="jhu"))

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

from fastapi import APIRouter, HTTPException, Request

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

V2 = APIRouter()
Expand Down Expand Up @@ -107,4 +107,4 @@ async def sources():
"""
Retrieves a list of data-sources that are availble to use.
"""
return {"sources": list(DATA_SOURCES.keys())}
return {"sources": list(DataSourceSingleton().get_data_source_list().keys())}