Skip to content

Commit 75eaed7

Browse files
authored
Merge pull request #69 from ExpDev07/v2
Version 2 has landed!
2 parents 28dd4c8 + a0327d4 commit 75eaed7

File tree

19 files changed

+417
-110
lines changed

19 files changed

+417
-110
lines changed

README.md

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,95 @@
1212

1313
## Endpoints
1414

15-
All requests must be made to the base url: ``https://coronavirus-tracker-api.herokuapp.com`` (e.g: https://coronavirus-tracker-api.herokuapp.com/all). You can try them out in your browser to further inspect responses.
15+
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.
1616

17-
Getting confirmed cases, deaths, and recoveries:
17+
### Getting latest amount of total confirmed cases, deaths, and recoveries.
1818
```http
19-
GET /all
19+
GET /v2/latest
2020
```
2121
```json
22-
{ "latest": { ... }, "confirmed": { ... }, "deaths": { ... }, "recovered": { ... } }
22+
{
23+
"latest": {
24+
"confirmed": 197146,
25+
"deaths": 7905,
26+
"recovered": 80840
27+
}
28+
}
2329
```
2430

25-
Getting just confirmed:
31+
### Getting all locations.
2632
```http
27-
GET /confirmed
33+
GET /v2/locations
2834
```
2935
```json
3036
{
31-
"latest": 42767,
32-
"locations": [ ... ],
33-
"last_updated": "2020-03-07T18:08:58.432242Z",
34-
"source": "https://github.com/ExpDev07/coronavirus-tracker-api"
37+
"locations": [
38+
{
39+
"id": 0,
40+
"country": "Thailand",
41+
"country_code": "TH",
42+
"province": "",
43+
"coordinates": {
44+
"latitude": "15",
45+
"longitude": "101"
46+
},
47+
"latest": {
48+
"confirmed": 177,
49+
"deaths": 1,
50+
"recovered": 41
51+
}
52+
},
53+
{
54+
"id": 39,
55+
"country": "Norway",
56+
"country_code": "NO",
57+
"province": "",
58+
"coordinates": {
59+
"latitude": "60.472",
60+
"longitude": "8.4689"
61+
},
62+
"latest": {
63+
"confirmed": 1463,
64+
"deaths": 3,
65+
"recovered": 1
66+
}
67+
}
68+
]
3569
}
3670
```
3771

38-
Getting just deaths:
72+
Additionally, you can also filter by country ([alpha-2 country_code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)).
3973
```http
40-
GET /deaths
74+
GET /v2/locations?country_code=US
4175
```
4276

43-
Getting just recoveries:
77+
### Getting a specific location (includes timeline).
4478
```http
45-
GET /recovered
79+
GET /v2/locations/:id
80+
```
81+
```json
82+
{
83+
"location": {
84+
"id": 39,
85+
"country": "Norway",
86+
"country_code": "NO",
87+
"province": "",
88+
"coordinates": { },
89+
"latest": { },
90+
"timelines": {
91+
"confirmed": {
92+
"latest": 1463,
93+
"timeline": {
94+
"2020-03-16T00:00:00Z": 1333,
95+
"2020-03-17T00:00:00Z": 1463
96+
}
97+
},
98+
"deaths": { },
99+
"recovered": { }
100+
}
101+
}
102+
}
103+
}
46104
```
47105

48106
## Data
@@ -112,5 +170,5 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
112170

113171
## License
114172

115-
The data is available to the public strictly for educational and academic research purposes. Please link to this repo somewhere in your project if you can (not required) :).
173+
The data is available to the public strictly for educational and academic research purposes. Please link to this repo somewhere in your project :).
116174

app/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from flask import Flask
22
from flask_cors import CORS
3-
from . import settings
43

54
def create_app():
65
"""
@@ -12,7 +11,7 @@ def create_app():
1211
CORS(app)
1312

1413
# Set app config from settings.
15-
app.config.from_pyfile('settings.py');
14+
app.config.from_pyfile('config/settings.py');
1615

1716
with app.app_context():
1817
# Import routes.
File renamed without changes.

app/coordinates.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Coordinates:
2+
"""
3+
A position on earth using decimal coordinates (latitude and longitude).
4+
"""
5+
6+
def __init__(self, latitude, longitude):
7+
self.latitude = latitude
8+
self.longitude = longitude
9+
10+
def serialize(self):
11+
"""
12+
Serializes the coordinates into a dict.
13+
"""
14+
return {
15+
'latitude' : self.latitude,
16+
'longitude': self.longitude
17+
}
18+
19+
def __str__(self):
20+
return 'lat: %s, long: %s' % (self.latitude, self.longitude)

app/data/__init__.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

app/location.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from .coordinates import Coordinates
2+
from .utils import countrycodes
3+
4+
class Location:
5+
"""
6+
A location in the world affected by the coronavirus.
7+
"""
8+
9+
def __init__(self, id, country, province, coordinates, confirmed, deaths, recovered):
10+
# General info.
11+
self.id = id
12+
self.country = country.strip()
13+
self.province = province.strip()
14+
self.coordinates = coordinates
15+
16+
# Data.
17+
self.confirmed = confirmed
18+
self.deaths = deaths
19+
self.recovered = recovered
20+
21+
22+
@property
23+
def country_code(self):
24+
"""
25+
Gets the alpha-2 code represention of the country. Returns 'XX' if none is found.
26+
"""
27+
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
28+
29+
def serialize(self):
30+
"""
31+
Serializes the location into a dict.
32+
"""
33+
return {
34+
# General info.
35+
'id' : self.id,
36+
'country' : self.country,
37+
'province' : self.province,
38+
'country_code': self.country_code,
39+
40+
# Coordinates.
41+
'coordinates': self.coordinates.serialize(),
42+
43+
# Latest data.
44+
'latest': {
45+
'confirmed': self.confirmed.latest,
46+
'deaths' : self.deaths.latest,
47+
'recovered': self.recovered.latest
48+
}
49+
}

app/models/location.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

app/routes/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from . import confirmed
2-
from . import deaths
3-
from . import recovered
4-
from . import all
1+
# API version 1.
2+
from .v1 import confirmed, deaths, recovered, all
3+
4+
# API version 2.
5+
from .v2 import locations, latest

app/routes/all.py renamed to app/routes/v1/all.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from flask import jsonify
22
from flask import current_app as app
3-
from ..data import get_data
3+
from ...services.location.jhu import get_category
44

55
@app.route('/all')
66
def all():
77
# Get all the categories.
8-
confirmed = get_data('confirmed')
9-
deaths = get_data('deaths')
10-
recovered = get_data('recovered')
8+
confirmed = get_category('confirmed')
9+
deaths = get_category('deaths')
10+
recovered = get_category('recovered')
1111

1212
return jsonify({
1313
# Data.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask import jsonify
22
from flask import current_app as app
3-
from ..data import get_data
3+
from ...services.location.jhu import get_category
44

55
@app.route('/confirmed')
66
def confirmed():
7-
return jsonify(get_data('confirmed'))
7+
return jsonify(get_category('confirmed'))

0 commit comments

Comments
 (0)