Skip to content

Commit d056426

Browse files
author
Jeeven Dhanoa
committed
Apply aggregation pattern to app/data
1 parent 1c7e4ae commit d056426

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

app/data/__init__.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,31 @@
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 DataSources:
7+
# Mapping of services to data-sources.
8+
__DATA_SOURCES_MAP = {
9+
"jhu": JhuLocationService(),
10+
"csbs": CSBSLocationService(),
11+
"nyt": NYTLocationService(),
12+
}
1213

14+
def __init__(self):
15+
pass
1316

14-
def data_source(source):
15-
"""
16-
Retrieves the provided data-source service.
17+
def get_data_source(self, source):
18+
"""
19+
Retrieves the provided data-source service.
1720
18-
:returns: The service.
19-
:rtype: LocationService
20-
"""
21-
return DATA_SOURCES.get(source.lower())
21+
:returns: The service.
22+
:rtype: LocationService
23+
"""
24+
return self.__DATA_SOURCES_MAP.get(source.lower())
25+
26+
def get_data_sources(self):
27+
"""
28+
Retrieves a dict of all data sources.
29+
30+
:returns: The dictionary of data sources.
31+
:rtype: dict
32+
"""
33+
return self.__DATA_SOURCES_MAP

app/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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 DataSources
1818
from .routers import V1, V2
1919
from .utils.httputils import setup_client_session, teardown_client_session
2020

@@ -41,6 +41,8 @@
4141
on_shutdown=[teardown_client_session],
4242
)
4343

44+
DATA_SOURCES = DataSources()
45+
4446
# #####################
4547
# Middleware
4648
#######################
@@ -73,8 +75,8 @@ async def add_datasource(request: Request, call_next):
7375
"""
7476
Attach the data source to the request.state.
7577
"""
76-
# Retrieve the datas ource from query param.
77-
source = data_source(request.query_params.get("source", default="jhu"))
78+
# Retrieve the data source from query param.
79+
source = DATA_SOURCES.get_data_source(request.query_params.get("source", default="jhu"))
7880

7981
# Abort with 404 if source cannot be found.
8082
if not source:

app/routers/v2.py

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

44
from fastapi import APIRouter, HTTPException, Request
55

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

99
V2 = APIRouter()
10+
DATA_SOURCES = DataSources()
1011

1112

1213
class Sources(str, enum.Enum):
@@ -107,4 +108,4 @@ async def sources():
107108
"""
108109
Retrieves a list of data-sources that are availble to use.
109110
"""
110-
return {"sources": list(DATA_SOURCES.keys())}
111+
return {"sources": list(DATA_SOURCES.get_data_sources().keys())}

0 commit comments

Comments
 (0)