Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Basis for providing multiple data-sources.
  • Loading branch information
ExpDev07 committed Mar 21, 2020
commit e2d4634d96648f37ed3708f5a295ff5587b3e8ed
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@

All requests must be made to the base url: ``https://coronavirus-tracker-api.herokuapp.com/v2/`` (e.g: https://coronavirus-tracker-api.herokuapp.com/v2/locations). You can try them out in your browser to further inspect responses.

### Chosing data source

We provide multiple data-sources you can pick from, simply add the query paramater ``?source=your_source_of_choice`` to your requests. JHU will be used as a default if you don't provide one.

#### Available sources:

* **jhu** - https://github.com/CSSEGISandData/COVID-19 - Data repository operated by the Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE). Also, Supported by ESRI Living Atlas Team and the Johns Hopkins University Applied Physics Lab (JHU APL).

* **... more to come later**.

### Getting latest amount of total confirmed cases, deaths, and recoveries.
```http
GET /v2/latest
Expand Down
15 changes: 15 additions & 0 deletions app/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from ..services.location.jhu import JhuLocationService

# Mapping of services to data-sources.
data_sources = {
'jhu': JhuLocationService(),
}

def data_source(source: str):
"""
Retrieves the provided data-source service.

:returns: The service.
:rtype: LocationService
"""
return data_sources.get(source.lower())
3 changes: 1 addition & 2 deletions app/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def __init__(self, id, country, province, coordinates, confirmed, deaths, recove
self.confirmed = confirmed
self.deaths = deaths
self.recovered = recovered



@property
def country_code(self):
"""
Expand Down
16 changes: 15 additions & 1 deletion app/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Blueprint, redirect, current_app as app
from flask import Blueprint, redirect, request, current_app as app
from ..data import data_source

# Follow the import order to avoid circular dependency
api_v1 = Blueprint('api_v1', __name__, url_prefix='')
Expand All @@ -14,3 +15,16 @@
@app.route('/')
def index():
return redirect('https://github.com/ExpDev07/coronavirus-tracker-api', 302)

# Middleware for picking data source.
@api_v2.before_request
def datasource():
"""
Attaches the datasource to the request.
"""
# Retrieve the datas ource from query param.
source = request.args.get('source', type=str, default='jhu')

# Attach source to request and return it.
request.source = data_source(source)
pass
1 change: 0 additions & 1 deletion app/routes/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

6 changes: 3 additions & 3 deletions app/routes/v2/latest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from flask import jsonify
from flask import request, jsonify
from ...routes import api_v2 as api
from ...services import jhu
from ...data import data_source

@api.route('/latest')
def latest():
# Get the serialized version of all the locations.
locations = [ location.serialize() for location in jhu.get_all() ]
locations = [ location.serialize() for location in request.source.get_all() ]

# All the latest information.
latest = list(map(lambda location: location['latest'], locations))
Expand Down
6 changes: 3 additions & 3 deletions app/routes/v2/locations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import jsonify, request
from distutils.util import strtobool
from ...routes import api_v2 as api
from ...services import jhu
from ...data import data_source

@api.route('/locations')
def locations():
Expand All @@ -10,7 +10,7 @@ def locations():
country_code = request.args.get('country_code', type=str)

# Retrieve all the locations.
locations = jhu.get_all()
locations = request.source.get_all()

# Filtering my country code if provided.
if not country_code is None:
Expand All @@ -30,5 +30,5 @@ def location(id):

# Return serialized location.
return jsonify({
'location': jhu.get(id).serialize(timelines)
'location': request.source.get(id).serialize(timelines)
})