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
19 changes: 11 additions & 8 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
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(),
}

DATA_SOURCES = ['jhu', 'csbs', 'nyt']

def data_source(source):
"""
Expand All @@ -18,4 +12,13 @@ def data_source(source):
:returns: The service.
:rtype: LocationService
"""
return DATA_SOURCES.get(source.lower())
s = source.lower()
if s not in DATA_SOURCES:
return None
# return the singleton instance of the required service
if s == 'jhu':
return JhuLocationService.getInstance()
elif s == 'csbs':
return CSBSLocationService.getInstance()
elif s == 'nyt':
return NYTLocationService.getInstance()
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)}
14 changes: 14 additions & 0 deletions app/services/location/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class LocationService(ABC):
Service for retrieving locations.
"""

# this class variable will store the singleton instance for
# the LocationService instance from the child class
__instance__ = None

@abstractmethod
async def get_all(self):
"""
Expand All @@ -26,3 +30,13 @@ async def get(self, id): # pylint: disable=redefined-builtin,invalid-name
:rtype: Location
"""
raise NotImplementedError

@classmethod
def getInstance(cls):
# singleton pattern
if cls.__instance__ is None:
# instantiate new object from the class (note that we are
# expecting 0 parameters for the constructor. if parameters
# needed this method should be overridden by the subclass)
cls.__instance__ = cls()
return cls.__instance__