Skip to content

Commit 209f36f

Browse files
committed
EECS 3311: Creational Design Pattern - Singleton
1 parent c89c46a commit 209f36f

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

app/data/__init__.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,34 @@
33
from ..services.location.jhu import JhuLocationService
44
from ..services.location.nyt import NYTLocationService
55

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

12+
def __call__(cls, *args, **kwargs):
13+
"""
14+
Possible changes to the value of the `__init__` argument do not affect
15+
the returned instance.
16+
"""
17+
if cls not in cls._instances:
18+
instance = super().__call__(*args, **kwargs)
19+
cls._instances[cls] = instance
20+
return cls._instances[cls]
1321

14-
def data_source(source):
15-
"""
16-
Retrieves the provided data-source service.
1722

18-
:returns: The service.
19-
:rtype: LocationService
20-
"""
21-
return DATA_SOURCES.get(source.lower())
23+
class DataSourceSingleton(metaclass=DataSourceSingletonMeta):
24+
DATA_SOURCES = {
25+
"jhu": JhuLocationService(),
26+
"csbs": CSBSLocationService(),
27+
"nyt": NYTLocationService(),
28+
}
29+
def get_data_source(self, dataSource):
30+
return self.DATA_SOURCES.get(dataSource.lower())
31+
...
32+
33+
def get_data_source_list(self):
34+
return self.DATA_SOURCES
35+
36+

app/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1515

1616
from .config import get_settings
17-
from .data import data_source
17+
# from .data import data_source
18+
from .data import DataSourceSingleton
1819
from .routers import V1, V2
1920
from .utils.httputils import setup_client_session, teardown_client_session
2021

@@ -73,8 +74,12 @@ async def add_datasource(request: Request, call_next):
7374
"""
7475
Attach the data source to the request.state.
7576
"""
77+
78+
79+
7680
# Retrieve the datas ource from query param.
77-
source = data_source(request.query_params.get("source", default="jhu"))
81+
source = DataSourceSingleton().get_data_source(request.query_params.get("source", default="jhu"))
82+
# data_source(request.query_params.get("source", default="jhu"))
7883

7984
# Abort with 404 if source cannot be found.
8085
if not source:

app/routers/v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from fastapi import APIRouter, HTTPException, Request
55

6-
from ..data import DATA_SOURCES
6+
from ..data import DataSourceSingleton
77
from ..models import LatestResponse, LocationResponse, LocationsResponse
88

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

0 commit comments

Comments
 (0)