File tree Expand file tree Collapse file tree 3 files changed +38
-18
lines changed
Expand file tree Collapse file tree 3 files changed +38
-18
lines changed Original file line number Diff line number Diff line change 33from ..services .location .jhu import JhuLocationService
44from ..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
13-
14- def data_source (source ):
7+ class DataSources :
158 """
16- Retrieves the provided data-source service.
17-
18- :returns: The service.
19- :rtype: LocationService
9+ Class to represent the root of the aggregate containing the location services.
2010 """
21- return DATA_SOURCES .get (source .lower ())
11+
12+ # Mapping of services to data-sources.
13+ __DATA_SOURCES_MAP = {
14+ "jhu" : JhuLocationService (),
15+ "csbs" : CSBSLocationService (),
16+ "nyt" : NYTLocationService (),
17+ }
18+
19+ def __init__ (self ):
20+ pass
21+
22+ def get_data_source (self , source ):
23+ """
24+ Retrieves the provided data-source service.
25+
26+ :returns: The service.
27+ :rtype: LocationService
28+ """
29+ return self .__DATA_SOURCES_MAP .get (source .lower ())
30+
31+ def get_data_sources (self ):
32+ """
33+ Retrieves a dict of all data sources.
34+
35+ :returns: The dictionary of data sources.
36+ :rtype: dict
37+ """
38+ return self .__DATA_SOURCES_MAP
Original file line number Diff line number Diff line change 1414from sentry_sdk .integrations .asgi import SentryAsgiMiddleware
1515
1616from .config import get_settings
17- from .data import data_source
17+ from .data import DataSources
1818from .routers import V1 , V2
1919from .utils .httputils import setup_client_session , teardown_client_session
2020
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 :
Original file line number Diff line number Diff line change 33
44from fastapi import APIRouter , HTTPException , Request
55
6- from ..data import DATA_SOURCES
6+ from ..data import DataSources
77from ..models import LatestResponse , LocationResponse , LocationsResponse
88
99V2 = APIRouter ()
10+ DATA_SOURCES = DataSources ()
1011
1112
1213class 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 ())}
You can’t perform that action at this time.
0 commit comments