Skip to content
Open
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
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 28 additions & 13 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


9 changes: 7 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 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 DataSourceSingleton
from ..models import LatestResponse, LocationResponse, LocationsResponse

V2 = APIRouter()
Expand Down 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": list(DataSourceSingleton().get_data_source_list().keys())}