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
Aggregate Pattern for DataSource functionallity implemented. Allows f…
…or a deeper level of abstraction while encapsulating data sources in such a way that the data sources and the boundary can only be accessed through the root
  • Loading branch information
MikePresman committed Jul 15, 2021
commit 841d83b91e29f39fe3a2375b4a7f32b0a8964b7c
31 changes: 18 additions & 13 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
from ..services.location.csbs import CSBSLocationService
from ..services.location.jhu import JhuLocationService
from ..services.location.nyt import NYTLocationService
from ..services.location import LocationService

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

def __init__(self):
self.__DATA_SOURCES['jhu'] = JhuLocationService()
self.__DATA_SOURCES['csbs'] = CSBSLocationService()
self.__DATA_SOURCES['nyt'] = NYTLocationService()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate that you took the time to explain what you were doing and why.
But I still don't really see the value of this refactor.

For much the same reason as I said in your other PR.
#349 (comment)

DataSource doesn't need to be a class. What is the point of creating a DataSource instance considering all the instances have the exact same sources?

There are ways to make the __init__ method return the same instance singleton, but this won't do that.
Although I'm not actually recommending that (just an FYI).


def data_source(source):
"""
Retrieves the provided data-source service.
# Mapping of services to data-sources.
@classmethod
def get_data_source(self, source: str) -> LocationService:
return self.__DATA_SOURCES.get(source.lower())

:returns: The service.
:rtype: LocationService
"""
return DATA_SOURCES.get(source.lower())
@classmethod
def add_data_source(self, source: str, reference_to_source: LocationService) -> None:
self.__DATA_SOURCES[source] = reference_to_source

@classmethod
def get_all_sources(self) -> dict:
return self.__DATA_SOURCES
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 .data import DataSource
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 = DataSource()
source.get_data_source(request.query_params.get("source", default = "jhu"))


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

from fastapi import APIRouter, HTTPException, Request

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

V2 = APIRouter()
Expand All @@ -26,7 +26,7 @@ async def get_latest(
"""
Getting latest amount of total confirmed cases, deaths, and recoveries.
"""
locations = await request.state.source.get_all()
locations = await request.state.source.get_all_sources()[source].get_all()
return {
"latest": {
"confirmed": sum(map(lambda location: location.confirmed, locations)),
Expand Down Expand Up @@ -57,7 +57,7 @@ async def get_locations(
params.pop("timelines", None)

# Retrieve all the locations.
locations = await request.state.source.get_all()
locations = await request.state.source.get_all_sources()[source].get_all()

# Attempt to filter out locations with properties matching the provided query params.
for key, value in params.items():
Expand Down Expand Up @@ -98,7 +98,7 @@ async def get_location_by_id(
"""
Getting specific location by id.
"""
location = await request.state.source.get(id)
location = await request.state.source.get_all_sources()[source].get(id)
return {"location": location.serialize(timelines)}


Expand All @@ -107,4 +107,5 @@ async def sources():
"""
Retrieves a list of data-sources that are availble to use.
"""
return {"sources": list(DATA_SOURCES.keys())}
data_source = DataSource()
return {"sources": list(data_source.get_all_sources().keys())}