diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4ecdd0b6..91b53c02 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,6 +15,10 @@ Please write new test cases for new code you create. * We will love you forever if you include unit tests. We can always use more test coverage * If you have updated [Pipefile](./Pipfile), you have to update `Pipfile.lock`, `requirements.txt` and `requirements-dev.txt`. See section [Update requirements files](./README.md#update-requirements-files). +## Some about the database + +* There is gonna be something that must be added into the database because blah blah this is a test. This is something in consideration about the database wanting to be added to add stability blah + ## Your First Code Contribution Unsure where to begin contributing to coronavirus-tracker-api ? You can start by looking through these issues labels: diff --git a/README.md b/README.md index 7355d0af..84ce0d6c 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ Provides up-to-date data about Coronavirus outbreak. Includes numbers about confirmed cases, deaths and recovered. Support multiple data-sources. +Test + ![Travis build](https://api.travis-ci.com/ExpDev07/coronavirus-tracker-api.svg?branch=master) [![Coverage Status](https://coveralls.io/repos/github/ExpDev07/coronavirus-tracker-api/badge.svg?branch=master)](https://coveralls.io/github/ExpDev07/coronavirus-tracker-api?branch=master) [![License](https://img.shields.io/github/license/ExpDev07/coronavirus-tracker-api)](LICENSE.md) diff --git a/app/data/__init__.py b/app/data/__init__.py index 60a75dac..18a8dcdf 100644 --- a/app/data/__init__.py +++ b/app/data/__init__.py @@ -3,19 +3,34 @@ 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 DataSourceSingletonMeta(type): + """ + access point to the DataSourceSingleton + """ + _instances = {} + def __call__(cls, *args, **kwargs): + """ + Possible changes to the value of the `__init__` argument do not affect + the returned instance. + """ + if cls not in cls._instances: + instance = super().__call__(*args, **kwargs) + cls._instances[cls] = instance + return cls._instances[cls] -def data_source(source): - """ - Retrieves the provided data-source service. - :returns: The service. - :rtype: LocationService - """ - return DATA_SOURCES.get(source.lower()) +class DataSourceSingleton(metaclass=DataSourceSingletonMeta): + DATA_SOURCES = { + "jhu": JhuLocationService(), + "csbs": CSBSLocationService(), + "nyt": NYTLocationService(), + } + def get_data_source(self, dataSource): + return self.DATA_SOURCES.get(dataSource.lower()) + ... + + def get_data_source_list(self): + return self.DATA_SOURCES + + diff --git a/app/main.py b/app/main.py index b9aff949..0f6ee03a 100644 --- a/app/main.py +++ b/app/main.py @@ -14,7 +14,8 @@ from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from .config import get_settings -from .data import data_source +# from .data import data_source +from .data import DataSourceSingleton from .routers import V1, V2 from .utils.httputils import setup_client_session, teardown_client_session @@ -73,8 +74,12 @@ 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 = DataSourceSingleton().get_data_source(request.query_params.get("source", default="jhu")) + # data_source(request.query_params.get("source", default="jhu")) # Abort with 404 if source cannot be found. if not source: diff --git a/app/routers/v2.py b/app/routers/v2.py index 31eb408c..0961d1bc 100644 --- a/app/routers/v2.py +++ b/app/routers/v2.py @@ -3,7 +3,7 @@ from fastapi import APIRouter, HTTPException, Request -from ..data import DATA_SOURCES +from ..data import DataSourceSingleton from ..models import LatestResponse, LocationResponse, LocationsResponse V2 = APIRouter() @@ -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": list(DataSourceSingleton().get_data_source_list().keys())}