Skip to content

Commit 38a3fff

Browse files
committed
Apply the aggregate pattern to data sources
1 parent 1c7e4ae commit 38a3fff

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

app/data/__init__.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@
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 Source:
7+
def __init__(self, source) -> None:
8+
self._sources = {"jhu","csbs","nyt"}
129

10+
if source == "csbs":
11+
self._service = CSBSLocationService()
12+
elif source == "nyt":
13+
self._service = NYTLocationService()
14+
else:
15+
self._service = JhuLocationService()
1316

14-
def data_source(source):
15-
"""
16-
Retrieves the provided data-source service.
17+
if source not in self._sources:
18+
self._service = None
19+
20+
def get_sources(self):
21+
"""
22+
Return the list of available sources.
23+
"""
24+
return self._sources
1725

18-
:returns: The service.
19-
:rtype: LocationService
20-
"""
21-
return DATA_SOURCES.get(source.lower())
26+
def get_service(self):
27+
"""
28+
Retrieves the provided data-source service.
29+
30+
:returns: The service.
31+
:rtype: LocationService
32+
"""
33+
return self._service
34+
35+

app/main.py

Lines changed: 3 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 Source
1818
from .routers import V1, V2
1919
from .utils.httputils import setup_client_session, teardown_client_session
2020

@@ -74,10 +74,10 @@ async def add_datasource(request: Request, call_next):
7474
Attach the data source to the request.state.
7575
"""
7676
# Retrieve the datas ource from query param.
77-
source = data_source(request.query_params.get("source", default="jhu"))
77+
source = Source(request.query_params.get("source", default="jhu"))
7878

7979
# Abort with 404 if source cannot be found.
80-
if not source:
80+
if not source.get_service():
8181
return Response("The provided data-source was not found.", status_code=404)
8282

8383
# Attach source to request.

app/routers/v2.py

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

44
from fastapi import APIRouter, HTTPException, Request
55

6-
from ..data import DATA_SOURCES
76
from ..models import LatestResponse, LocationResponse, LocationsResponse
87

98
V2 = APIRouter()
@@ -26,7 +25,7 @@ async def get_latest(
2625
"""
2726
Getting latest amount of total confirmed cases, deaths, and recoveries.
2827
"""
29-
locations = await request.state.source.get_all()
28+
locations = await request.state.source.get_service().get_all()
3029
return {
3130
"latest": {
3231
"confirmed": sum(map(lambda location: location.confirmed, locations)),
@@ -57,7 +56,7 @@ async def get_locations(
5756
params.pop("timelines", None)
5857

5958
# Retrieve all the locations.
60-
locations = await request.state.source.get_all()
59+
locations = await request.state.source.get_service().get_all()
6160

6261
# Attempt to filter out locations with properties matching the provided query params.
6362
for key, value in params.items():
@@ -98,7 +97,7 @@ async def get_location_by_id(
9897
"""
9998
Getting specific location by id.
10099
"""
101-
location = await request.state.source.get(id)
100+
location = await request.state.source.get_service().get(id)
102101
return {"location": location.serialize(timelines)}
103102

104103

@@ -107,4 +106,4 @@ async def sources():
107106
"""
108107
Retrieves a list of data-sources that are availble to use.
109108
"""
110-
return {"sources": list(DATA_SOURCES.keys())}
109+
return {"sources": [source.value for source in Sources]}

0 commit comments

Comments
 (0)