Skip to content

Commit e2d4634

Browse files
author
ExpDev07
committed
Basis for providing multiple data-sources.
1 parent 48d4ac7 commit e2d4634

File tree

7 files changed

+47
-10
lines changed

7 files changed

+47
-10
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515

1616
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.
1717

18+
### Chosing data source
19+
20+
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.
21+
22+
#### Available sources:
23+
24+
* **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).
25+
26+
* **... more to come later**.
27+
1828
### Getting latest amount of total confirmed cases, deaths, and recoveries.
1929
```http
2030
GET /v2/latest

app/data/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from ..services.location.jhu import JhuLocationService
2+
3+
# Mapping of services to data-sources.
4+
data_sources = {
5+
'jhu': JhuLocationService(),
6+
}
7+
8+
def data_source(source: str):
9+
"""
10+
Retrieves the provided data-source service.
11+
12+
:returns: The service.
13+
:rtype: LocationService
14+
"""
15+
return data_sources.get(source.lower())

app/location.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def __init__(self, id, country, province, coordinates, confirmed, deaths, recove
1717
self.confirmed = confirmed
1818
self.deaths = deaths
1919
self.recovered = recovered
20-
21-
20+
2221
@property
2322
def country_code(self):
2423
"""

app/routes/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from flask import Blueprint, redirect, current_app as app
1+
from flask import Blueprint, redirect, request, current_app as app
2+
from ..data import data_source
23

34
# Follow the import order to avoid circular dependency
45
api_v1 = Blueprint('api_v1', __name__, url_prefix='')
@@ -14,3 +15,16 @@
1415
@app.route('/')
1516
def index():
1617
return redirect('https://github.com/ExpDev07/coronavirus-tracker-api', 302)
18+
19+
# Middleware for picking data source.
20+
@api_v2.before_request
21+
def datasource():
22+
"""
23+
Attaches the datasource to the request.
24+
"""
25+
# Retrieve the datas ource from query param.
26+
source = request.args.get('source', type=str, default='jhu')
27+
28+
# Attach source to request and return it.
29+
request.source = data_source(source)
30+
pass

app/routes/v2/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

app/routes/v2/latest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from flask import jsonify
1+
from flask import request, jsonify
22
from ...routes import api_v2 as api
3-
from ...services import jhu
3+
from ...data import data_source
44

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

1010
# All the latest information.
1111
latest = list(map(lambda location: location['latest'], locations))

app/routes/v2/locations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask import jsonify, request
22
from distutils.util import strtobool
33
from ...routes import api_v2 as api
4-
from ...services import jhu
4+
from ...data import data_source
55

66
@api.route('/locations')
77
def locations():
@@ -10,7 +10,7 @@ def locations():
1010
country_code = request.args.get('country_code', type=str)
1111

1212
# Retrieve all the locations.
13-
locations = jhu.get_all()
13+
locations = request.source.get_all()
1414

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

3131
# Return serialized location.
3232
return jsonify({
33-
'location': jhu.get(id).serialize(timelines)
33+
'location': request.source.get(id).serialize(timelines)
3434
})

0 commit comments

Comments
 (0)