Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Apply aggregation pattern to app/data
  • Loading branch information
Jeeven Dhanoa committed Jul 23, 2021
commit d05642664e578812a6035de47feb583ea1b349a8
38 changes: 25 additions & 13 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
from ..services.location.jhu import JhuLocationService
from ..services.location.nyt import NYTLocationService

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

def __init__(self):
pass

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

:returns: The service.
:rtype: LocationService
"""
return DATA_SOURCES.get(source.lower())
:returns: The service.
:rtype: LocationService
"""
return self.__DATA_SOURCES_MAP.get(source.lower())

def get_data_sources(self):
"""
Retrieves a dict of all data sources.

:returns: The dictionary of data sources.
:rtype: dict
"""
return self.__DATA_SOURCES_MAP
8 changes: 5 additions & 3 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 .data import DataSources
from .routers import V1, V2
from .utils.httputils import setup_client_session, teardown_client_session

Expand All @@ -41,6 +41,8 @@
on_shutdown=[teardown_client_session],
)

DATA_SOURCES = DataSources()

# #####################
# Middleware
#######################
Expand Down Expand Up @@ -73,8 +75,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"))
# Retrieve the data source from query param.
source = DATA_SOURCES.get_data_source(request.query_params.get("source", default="jhu"))

# Abort with 404 if source cannot be found.
if not source:
Expand Down
5 changes: 3 additions & 2 deletions app/routers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

from fastapi import APIRouter, HTTPException, Request

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

V2 = APIRouter()
DATA_SOURCES = DataSources()


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