Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@
from ..services.location.nyt import NYTLocationService

# Mapping of services to data-sources.
DATA_SOURCES = {
"jhu": JhuLocationService(),
"csbs": CSBSLocationService(),
"nyt": NYTLocationService(),
}


def data_source(source):
"""
Retrieves the provided data-source service.

:returns: The service.
:rtype: LocationService
"""
return DATA_SOURCES.get(source.lower())
DATA_SOURCES = [
"jhu",
"csbs",
"nyt",
]
6 changes: 4 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

from .config import get_settings
from .data import data_source
from .services.location import LocationServiceFactory
from .routers import V1, V2
from .utils.httputils import setup_client_session, teardown_client_session

Expand Down Expand Up @@ -74,7 +74,9 @@ async def add_datasource(request: Request, call_next):
Attach the data source to the request.state.
"""
# Retrieve the datas ource from query param.
source = data_source(request.query_params.get("source", default="jhu"))
source_name = request.query_params.get("source", default="jhu")
location_service_factory = LocationServiceFactory()
source = location_service_factory.create(source_name)

# Abort with 404 if source cannot be found.
if not source:
Expand Down
2 changes: 1 addition & 1 deletion app/routers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@ async def sources():
"""
Retrieves a list of data-sources that are availble to use.
"""
return {"sources": list(DATA_SOURCES.keys())}
return {"sources": DATA_SOURCES}
21 changes: 21 additions & 0 deletions app/services/location/locationservicefactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


from ..location.csbs import CSBSLocationService
from ..location.jhu import JhuLocationService
from ..location.nyt import NYTLocationService


NEW_YORK_TIMES = 'nyt'
JOHNS_HOPKINS_UNIVERSITY = 'jhu'
CSBS = 'csbs'


class LocationServiceFactory:
def create(location_service_name):
lowercase_service_name = location_service_name.lower()
if lowercase_service_name == CSBS:
return CSBSLocationService()
elif lowercase_service_name == JOHNS_HOPKINS_UNIVERSITY:
return JhuLocationService()
elif lowercase_service_name == NEW_YORK_TIMES:
return NYTLocationService()