File tree Expand file tree Collapse file tree 3 files changed +22
-14
lines changed
Expand file tree Collapse file tree 3 files changed +22
-14
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
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 ())
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 Source
1818from .routers import V1 , V2
1919from .utils .httputils import setup_client_session , teardown_client_session
2020
4646#######################
4747
4848# Scout APM
49+
4950if SETTINGS .scout_name : # pragma: no cover
5051 LOGGER .info (f"Adding Scout APM middleware for `{ SETTINGS .scout_name } `" )
5152 APP .add_middleware (ScoutMiddleware )
6768)
6869APP .add_middleware (GZipMiddleware , minimum_size = 1000 )
6970
70-
71+ SOURCES = Source ()
7172@APP .middleware ("http" )
7273async 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 :
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 Source
77from ..models import LatestResponse , LocationResponse , LocationsResponse
88
99V2 = 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" )
106106async 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 )}
You can’t perform that action at this time.
0 commit comments