diff --git a/app/data/__init__.py b/app/data/__init__.py index 60a75dac..c637fe70 100644 --- a/app/data/__init__.py +++ b/app/data/__init__.py @@ -18,4 +18,4 @@ def data_source(source): :returns: The service. :rtype: LocationService """ - return DATA_SOURCES.get(source.lower()) + return LocationServiceAdaptor(DATA_SOURCES.get(source.lower())) diff --git a/app/services/location/__init__.py b/app/services/location/__init__.py index 6d292b54..404b4017 100644 --- a/app/services/location/__init__.py +++ b/app/services/location/__init__.py @@ -2,10 +2,12 @@ from abc import ABC, abstractmethod -class LocationService(ABC): +class LocationServiceAdaptor: """ Service for retrieving locations. """ + def __init__(self, source): + self.source = source @abstractmethod async def get_all(self): @@ -15,6 +17,7 @@ async def get_all(self): :returns: The locations. :rtype: List[Location] """ + return await self.source.get_locations() raise NotImplementedError @abstractmethod @@ -25,4 +28,5 @@ async def get(self, id): # pylint: disable=redefined-builtin,invalid-name :returns: The location. :rtype: Location """ - raise NotImplementedError + locations = await self.source() + return locations[id] diff --git a/app/services/location/csbs.py b/app/services/location/csbs.py index 444ebad6..d8987869 100644 --- a/app/services/location/csbs.py +++ b/app/services/location/csbs.py @@ -20,16 +20,6 @@ class CSBSLocationService(LocationService): Service for retrieving locations from csbs """ - async def get_all(self): - # Get the locations. - locations = await get_locations() - return locations - - async def get(self, loc_id): # pylint: disable=arguments-differ - # Get location at the index equal to the provided id. - locations = await self.get_all() - return locations[loc_id] - # Base URL for fetching data BASE_URL = "https://facts.csbs.org/covid-19/covid19_county.csv" diff --git a/app/services/location/jhu.py b/app/services/location/jhu.py index ebed3960..5ac0eca1 100644 --- a/app/services/location/jhu.py +++ b/app/services/location/jhu.py @@ -25,18 +25,6 @@ class JhuLocationService(LocationService): """ Service for retrieving locations from Johns Hopkins CSSE (https://github.com/CSSEGISandData/COVID-19). """ - - async def get_all(self): - # Get the locations. - locations = await get_locations() - return locations - - async def get(self, loc_id): # pylint: disable=arguments-differ - # Get location at the index equal to provided id. - locations = await self.get_all() - return locations[loc_id] - - # --------------------------------------------------------------- diff --git a/app/services/location/nyt.py b/app/services/location/nyt.py index 1f25ec34..75732d8f 100644 --- a/app/services/location/nyt.py +++ b/app/services/location/nyt.py @@ -20,18 +20,6 @@ class NYTLocationService(LocationService): """ Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data). """ - - async def get_all(self): - # Get the locations. - locations = await get_locations() - return locations - - async def get(self, loc_id): # pylint: disable=arguments-differ - # Get location at the index equal to provided id. - locations = await self.get_all() - return locations[loc_id] - - # ---------------------------------------------------------------