Skip to content

Commit e429095

Browse files
author
Vimarsh Patel
committed
First Aggregate (data/__init__.py)
1 parent 1c7e4ae commit e429095

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

app/data/__init__.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@
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-
}
126

137

14-
def data_source(source):
8+
class Source:
9+
10+
def __init__(self, source):
11+
# Mapping of services to data-sources.
12+
self.__DATA_SOURCES_LIST = {
13+
"jhu": JhuLocationService(),
14+
"csbs": CSBSLocationService(),
15+
"nyt": NYTLocationService(),
16+
}
17+
18+
def all_data_source(self):
19+
return self.__DATA_SOURCES_LIST
20+
21+
def single_data_source(self, source):
1522
"""
1623
Retrieves the provided data-source service.
1724
1825
:returns: The service.
1926
:rtype: LocationService
2027
"""
21-
return DATA_SOURCES.get(source.lower())
28+
return self.__DATA_SOURCES_LIST.get(source.lower())

app/main.py

Lines changed: 4 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

@@ -46,6 +46,7 @@
4646
#######################
4747

4848
# Scout APM
49+
4950
if SETTINGS.scout_name: # pragma: no cover
5051
LOGGER.info(f"Adding Scout APM middleware for `{SETTINGS.scout_name}`")
5152
APP.add_middleware(ScoutMiddleware)
@@ -67,14 +68,14 @@
6768
)
6869
APP.add_middleware(GZipMiddleware, minimum_size=1000)
6970

70-
71+
SOURCES = Source()
7172
@APP.middleware("http")
7273
async def add_datasource(request: Request, call_next):
7374
"""
7475
Attach the data source to the request.state.
7576
"""
7677
# Retrieve the datas ource from query param.
77-
source = data_source(request.query_params.get("source", default="jhu"))
78+
source = SOURCE.get_data_source(request.query_params.get("source", default="jhu"))
7879

7980
# Abort with 404 if source cannot be found.
8081
if not source:

app/routers/v2.py

Lines changed: 3 additions & 3 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 Source
77
from ..models import LatestResponse, LocationResponse, LocationsResponse
88

99
V2 = APIRouter()
@@ -101,10 +101,10 @@ async def get_location_by_id(
101101
location = await request.state.source.get(id)
102102
return {"location": location.serialize(timelines)}
103103

104-
104+
SOURCE = Source()
105105
@V2.get("/sources")
106106
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(SOURCE.all_data_source)}

0 commit comments

Comments
 (0)