Skip to content

Commit 85e493f

Browse files
authored
Merge pull request #3 from Kilo59/master
Merge master with fast-api conversion.
2 parents 8a19959 + 3009428 commit 85e493f

File tree

7 files changed

+274
-102
lines changed

7 files changed

+274
-102
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@
110110
"contributions": [
111111
"doc"
112112
]
113+
},
114+
{
115+
"login": "carmelag",
116+
"name": "carmelag",
117+
"avatar_url": "https://avatars0.githubusercontent.com/u/5394906?v=4",
118+
"profile": "http://www.carmelagreco.dev",
119+
"contributions": [
120+
"doc"
121+
]
113122
}
114123
],
115124
"contributorsPerLine": 7,

README.md

Lines changed: 183 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
<h1 align="center">
2-
coronavirus-tracker (API)
3-
</h1>
4-
5-
> This is a fast (< 200ms) and basic API for tracking development of the new coronavirus (COVID-19, SARS-CoV-2). It's written in python using 🍼 Flask. Supports multiple sources!
1+
## Coronavirus Tracker API
2+
Provides up-to-date data about Coronavirus outbreak. Includes numbers about confirmed cases, deaths and recovered.
3+
Support multiple data-sources.
64

75
![Travis build](https://api.travis-ci.com/ExpDev07/coronavirus-tracker-api.svg?branch=master)
86
[![License](https://img.shields.io/github/license/ExpDev07/coronavirus-tracker-api)](LICENSE.md)
@@ -20,26 +18,63 @@
2018
![Covid-19 Recovered](https://covid19-badges.herokuapp.com/recovered/latest)
2119
![Covid-19 Deaths](https://covid19-badges.herokuapp.com/deaths/latest)
2220

23-
## Endpoints
21+
## Available data-sources:
22+
23+
Currently 2 different data-sources are available to retrieve the data:
2424

25-
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.
25+
* **jhu** - https://github.com/CSSEGISandData/COVID-19 - Worldwide Data repository operated by the Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE).
2626

27-
### Picking data source
27+
* **csbs** - https://www.csbs.org/information-covid-19-coronavirus - U.S. County data that comes from the Conference of State Bank Supervisors.
2828

29-
We provide multiple data-sources you can pick from, simply add the query parameter ``?source=your_source_of_choice`` to your requests. JHU will be used as a default if you don't provide one.
29+
__jhu__ data-source will be used as a default source if you don't specify a *source parameter* in your request.
3030

31-
#### Available sources:
3231

33-
* **jhu** - https://github.com/CSSEGISandData/COVID-19 - Data repository operated by the Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE).
32+
## API Reference
3433

35-
* **csbs** - https://www.csbs.org/information-covid-19-coronavirus - U.S. County data that comes from the Conference of State Bank Supervisors.
34+
All endpoints are located at ``coronavirus-tracker-api.herokuapp.com/v2/`` and are accessible via https. For instance: you can get data per location by using this URL:
35+
*[https://coronavirus-tracker-api.herokuapp.com/v2/locations](https://coronavirus-tracker-api.herokuapp.com/v2/locations)*
36+
37+
You can open the URL in your browser to further inspect the response. Or you can make this curl call in your terminal to see the prettified response:
38+
39+
```
40+
curl https://coronavirus-tracker-api.herokuapp.com/v2/locations | json_pp
41+
```
42+
43+
44+
## API Endpoints
45+
46+
### Sources Endpoint
47+
48+
Getting the data-sources that are currently available to Coronavirus Tracker API to retrieve the data of the pandemic.
49+
50+
```http
51+
GET /v2/sources
52+
```
53+
54+
__Sample response__
55+
```json
56+
{
57+
"sources": [
58+
"jhu",
59+
"csbs"
60+
]
61+
}
62+
```
3663

37-
* **... more to come later**.
64+
### Latest Endpoint
65+
66+
Getting latest amount of total confirmed cases, deaths, and recovered.
3867

39-
### Getting latest amount of total confirmed cases, deaths, and recoveries.
4068
```http
4169
GET /v2/latest
4270
```
71+
72+
__Query String Parameters__
73+
| __Query string parameter__ | __Description__ | __Type__ |
74+
| -------------------------- | -------------------------------------------------------------------------------- | -------- |
75+
| source | The data-source where data will be retrieved from *(jhu/csbs)*. Default is *jhu* | String |
76+
77+
__Sample response__
4378
```json
4479
{
4580
"latest": {
@@ -50,10 +85,69 @@ GET /v2/latest
5085
}
5186
```
5287

53-
### Getting all locations.
88+
### Locations Endpoint
89+
90+
Getting latest amount of confirmed cases, deaths, and recovered per location.
91+
92+
#### The Location Object
93+
```http
94+
GET /v2/locations/:id
95+
```
96+
97+
__Path Parameters__
98+
| __Path parameter__ | __Required/Optional__ | __Description__ | __Type__ |
99+
| ------------------ | --------------------- | ------------------------------------------------------------------------- | -------- |
100+
| id | OPTIONAL | The unique location id for which you want to call the Locations Endpoint. | Integer |
101+
102+
__Query String Parameters__
103+
| __Query string parameter__ | __Description__ | __Type__ |
104+
| -------------------------- | -------------------------------------------------------------------------------- | -------- |
105+
| source | The data-source where data will be retrieved from *(jhu/csbs)*. Default is *jhu* | String |
106+
107+
#### Example Request
108+
```http
109+
GET /v2/locations/39
110+
```
111+
112+
__Sample response__
113+
```json
114+
{
115+
"location": {
116+
"id": 39,
117+
"country": "Norway",
118+
"country_code": "NO",
119+
"province": "",
120+
"last_updated": "2020-03-21T06:59:11.315422Z",
121+
"coordinates": { },
122+
"latest": { },
123+
"timelines": {
124+
"confirmed": {
125+
"latest": 1463,
126+
"timeline": {
127+
"2020-03-16T00:00:00Z": 1333,
128+
"2020-03-17T00:00:00Z": 1463
129+
}
130+
},
131+
"deaths": { },
132+
"recovered": { }
133+
}
134+
}
135+
}
136+
```
137+
138+
#### List of all locations
54139
```http
55140
GET /v2/locations
56141
```
142+
143+
__Query String Parameters__
144+
| __Query string parameter__ | __Description__ | __Type__ |
145+
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | -------- |
146+
| source | The data-source where data will be retrieved from.<br>__Value__ can be: *jhu/csbs*. __Default__ is *jhu* | String |
147+
| country_code | The ISO ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)) to the Country/Province for which you're calling the Endpoint | String |
148+
| timelines | To set the visibility of timelines (*daily tracking*).<br>__Value__ can be: *0/1*. __Default__ is *0* (timelines are not visible) | Integer |
149+
150+
__Sample response__
57151
```json
58152
{
59153
"latest": {
@@ -98,54 +192,76 @@ GET /v2/locations
98192
}
99193
```
100194

101-
Additionally, you can also filter by any attribute, including province and country ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)).
102-
```http
103-
GET /v2/locations?country_code=US
104-
```
195+
__Response definitions__
196+
| __Response Item__ | __Description__ | __Type__ |
197+
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | -------- |
198+
| {latest} | The total amount of confirmed cases, deaths and recovered for all the locations | Object |
199+
| {latest}/confirmed | The up-to-date total number of confirmed cases for all the locations within the data-source | Integer |
200+
| {latest}/deaths | The up-to-date total amount of deaths for all the locations within the data-source | Integer |
201+
| {latest}/recovered | The up-to-date total amount of recovered for all the locations within the data-source | Integer |
202+
| {locations} | The collection of locations contained within the data-source | Object |
203+
| {location} | Information that identifies a location | Object |
204+
| {latest} | The amount of confirmed cases, deaths and recovered related to the specific location | Object |
205+
| {locations}/{location}/{latest}/confirmed | The up-to-date number of confirmed cases related to the specific location | Integer |
206+
| {locations}/{location}/{latest}/deaths | The up-to-date number of deaths related to the specific location | Integer |
207+
| {locations}/{location}/{latest}/recovered | The up-to-date number of recovered related to the specific location | Integer |
208+
| {locations}/{location}/id | The location id. This unique id is assigned to the location by the data-source. | Integer |
209+
| {locations}/{location}/country | The Country name | String |
210+
| {locations}/{location}/country_code | The [ISO alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) Country code for the location. | String |
211+
| {locations}/{location}/province | The province where the location belongs to. (Used for US locations coming from __csbs data-source__.<br>__Empty__ when *jhu data-source* is used | String |
212+
| {locations}/{location}/{coordinates}/latitude | The location latitude | Float |
213+
| {locations}/{location}/{coordinates}/longitude | The location longitude | Float |
214+
215+
216+
### Example Requests with parameters
217+
218+
__Parameter: country_code__
219+
220+
Getting data for the Country specified by the *country_code parameter*, in this case Italy - IT
105221

106-
Include timelines.
107222
```http
108-
GET /v2/locations?timelines=1
223+
GET /v2/locations?country_code=IT
109224
```
110225

111-
### Getting a specific location (includes timelines by default).
112-
```http
113-
GET /v2/locations/:id
114-
```
226+
__Sample Response__
115227
```json
116228
{
117-
"location": {
118-
"id": 39,
119-
"country": "Norway",
120-
"country_code": "NO",
121-
"province": "",
122-
"last_updated": "2020-03-21T06:59:11.315422Z",
123-
"coordinates": { },
124-
"latest": { },
125-
"timelines": {
126-
"confirmed": {
127-
"latest": 1463,
128-
"timeline": {
129-
"2020-03-16T00:00:00Z": 1333,
130-
"2020-03-17T00:00:00Z": 1463
229+
"latest": {
230+
"confirmed": 59138,
231+
"deaths": 5476,
232+
"recovered": 7024
233+
},
234+
"locations": [
235+
{
236+
"coordinates": {
237+
"latitude": "43",
238+
"longitude": "12"
239+
},
240+
"country": "Italy",
241+
"country_code": "IT",
242+
"id": 16,
243+
"last_updated": "2020-03-23T13:32:23.913872Z",
244+
"latest": {
245+
"confirmed": 59138,
246+
"deaths": 5476,
247+
"recovered": 7024
248+
},
249+
"province": ""
131250
}
132-
},
133-
"deaths": { },
134-
"recovered": { }
135-
}
136-
}
251+
]
137252
}
138253
```
139254

140-
Exclude timelines.
141-
```http
142-
GET /v2/locations?timelines=0
143-
```
255+
__Parameter: source__
256+
257+
Getting the data from the data-source specified by the *source parameter*, in this case [csbs](https://www.csbs.org/information-covid-19-coronavirus)
258+
144259

145-
### Getting US per county information.
146260
```http
147261
GET /v2/locations?source=csbs
148262
```
263+
264+
__Sample Response__
149265
```json
150266
{
151267
"latest": {
@@ -194,6 +310,23 @@ GET /v2/locations?source=csbs
194310
}
195311
```
196312

313+
__Parameter: timelines__
314+
315+
Getting the data for all the locations including the daily tracking of confirmed cases, deaths and recovered per location.
316+
317+
```http
318+
GET /v2/locations?timelines=1
319+
```
320+
Explore the response by opening the URL in your browser [https://coronavirus-tracker-api.herokuapp.com/v2/locations?timelines=1](https://coronavirus-tracker-api.herokuapp.com/v2/locations?timelines=1) or make the following curl call in your terminal:
321+
322+
```
323+
curl https://coronavirus-tracker-api.herokuapp.com/v2/locations?timelines=1 | json_pp
324+
```
325+
326+
__NOTE:__ Timelines tracking starts from day 22nd January 2020 and ends to the last available day in the data-source.
327+
328+
329+
197330
## Wrappers
198331

199332
These are the available API wrappers created by the community. They are not necessarily maintained by any of this project's authors or contributors.
@@ -284,6 +417,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
284417
<td align="center"><a href="https://github.com/Abdirahiim"><img src="https://avatars0.githubusercontent.com/u/13730460?v=4" width="100px;" alt=""/><br /><sub><b>Abdirahiim Yassin </b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=Abdirahiim" title="Documentation">📖</a></td>
285418
<td align="center"><a href="https://github.com/kant"><img src="https://avatars1.githubusercontent.com/u/32717?v=4" width="100px;" alt=""/><br /><sub><b>Darío Hereñú</b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=kant" title="Documentation">📖</a></td>
286419
<td align="center"><a href="https://github.com/o-ba"><img src="https://avatars1.githubusercontent.com/u/8812114?v=4" width="100px;" alt=""/><br /><sub><b>Oliver</b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=o-ba" title="Documentation">📖</a></td>
420+
<td align="center"><a href="http://www.carmelagreco.dev"><img src="https://avatars0.githubusercontent.com/u/5394906?v=4" width="100px;" alt=""/><br /><sub><b>carmelag</b></sub></a><br /><a href="https://github.com/ExpDev07/coronavirus-tracker-api/commits?author=carmelag" title="Documentation">📖</a></td>
287421
</tr>
288422
</table>
289423

app/routes/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from flask import Blueprint, redirect, request, current_app as app
1+
from flask import Blueprint, redirect, request, abort, current_app as app
22
from ..data import data_source
33

44
# Follow the import order to avoid circular dependency
55
api_v1 = Blueprint('api_v1', __name__, url_prefix='')
66
api_v2 = Blueprint('api_v2', __name__, url_prefix='/v2')
77

88
# API version 2.
9-
from .v2 import locations, latest
9+
from .v2 import locations, latest, sources
1010

1111
# API version 1.
1212
from .v1 import confirmed, deaths, recovered, all
@@ -23,8 +23,12 @@ def datasource():
2323
Attaches the datasource to the request.
2424
"""
2525
# Retrieve the datas ource from query param.
26-
source = request.args.get('source', type=str, default='jhu')
26+
source = data_source(request.args.get('source', type=str, default='jhu'))
27+
28+
# Abort with 404 if source cannot be found.
29+
if not source:
30+
return abort(404, description='The provided data-source was not found.')
2731

2832
# Attach source to request and return it.
29-
request.source = data_source(source)
33+
request.source = source
3034
pass

app/routes/v2/locations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def locations():
1717
try:
1818
locations = [j for j in locations if getattr(j, i) == args.get(i, type=str)]
1919
except AttributeError:
20-
print('TimelinedLocation object does not have attribute {}.'.format(i))
20+
print('Location does not have attribute {}.'.format(i))
2121

2222
# Serialize each location and return.
2323
return jsonify({

app/routes/v2/sources.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from flask import jsonify
2+
from ...data import data_sources
3+
from ...routes import api_v2 as api
4+
5+
@api.route('/sources')
6+
def sources():
7+
"""
8+
Retrieves a list of data-sources that are availble to use.
9+
"""
10+
return jsonify({
11+
'sources': list(data_sources.keys())
12+
})

0 commit comments

Comments
 (0)