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
35 changes: 23 additions & 12 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@
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.
class DataSources:

:returns: The service.
:rtype: LocationService
"""
return DATA_SOURCES.get(source.lower())
def __init__(self):
"""
Sets __data_source's key-value pairs to be the data-source (key) and data-source service (value) for the supported sources (jhu, csbs, nyt)
"""
self.__data_source = {
"jhu": JhuLocationService(),
"csbs": CSBSLocationService(),
"nyt": NYTLocationService(),
}

def get_data_sources(self):
"""Return the keys stored in __data_source"""
return self.__data_source.keys()

def get_data_source_service(self, source):
"""
Retrieves the provided data-source service.

:returns: The service.
:rtype: LocationService
"""
return self.__data_source.get(source.lower())
11 changes: 8 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

from .config import get_settings
from .data import data_source
from .data import DataSources
from .routers import V1, V2
from .utils.httputils import setup_client_session, teardown_client_session

# ############
# Constructing DataSources object for retriving data source services
# ############
dataSources = DataSources()

# ############
# FastAPI App
# ############
Expand Down Expand Up @@ -74,8 +79,8 @@ 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 = dataSources.get_data_source_service(
request.query_params.get("source", default="jhu"))
# Abort with 404 if source cannot be found.
if not source:
return Response("The provided data-source was not found.", status_code=404)
Expand Down
10 changes: 8 additions & 2 deletions app/routers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@

from fastapi import APIRouter, HTTPException, Request

from ..data import DATA_SOURCES
from ..data import DataSources
from ..models import LatestResponse, LocationResponse, LocationsResponse

# ############
# Constructing DataSources object for retriving data source services
# ############
dataSources = DataSources()


V2 = APIRouter()


Expand Down Expand Up @@ -107,4 +113,4 @@ async def sources():
"""
Retrieves a list of data-sources that are availble to use.
"""
return {"sources": list(DATA_SOURCES.keys())}
return {"sources": list(dataSources.get_data_sources)}