Skip to content

Commit e528159

Browse files
author
Jeeven Dhanoa
committed
Seperate DataSources to their own class
1 parent 1c7e4ae commit e528159

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

app/data/__init__.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,36 @@
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

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

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)