Skip to content

Commit 33f8ea8

Browse files
authored
Merge pull request #76 from ExpDev07/timelines-option
Timelines option
2 parents 75eaed7 + eda3e95 commit 33f8ea8

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ Additionally, you can also filter by country ([alpha-2 country_code](https://en.
7474
GET /v2/locations?country_code=US
7575
```
7676

77-
### Getting a specific location (includes timeline).
77+
Include timelines.
78+
```http
79+
GET /v2/locations?country_code=US&timelines=true
80+
```
81+
82+
### Getting a specific location (includes timeline by default).
7883
```http
7984
GET /v2/locations/:id
8085
```

app/coordinates.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ def __init__(self, latitude, longitude):
1010
def serialize(self):
1111
"""
1212
Serializes the coordinates into a dict.
13+
14+
:returns: The serialized coordinates.
15+
:rtype: dict
1316
"""
1417
return {
1518
'latitude' : self.latitude,

app/location.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ def country_code(self):
2626
"""
2727
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
2828

29-
def serialize(self):
29+
def serialize(self, timelines = False):
3030
"""
3131
Serializes the location into a dict.
32+
33+
:param timelines: Whether to include the timelines.
34+
:returns: The serialized location.
35+
:rtype: dict
3236
"""
33-
return {
37+
serialized = {
3438
# General info.
3539
'id' : self.id,
3640
'country' : self.country,
@@ -45,5 +49,16 @@ def serialize(self):
4549
'confirmed': self.confirmed.latest,
4650
'deaths' : self.deaths.latest,
4751
'recovered': self.recovered.latest
48-
}
49-
}
52+
},
53+
}
54+
55+
# Whether to include the timelines or not.
56+
if timelines:
57+
serialized.update({ 'timelines': {
58+
'confirmed': self.confirmed.serialize(),
59+
'deaths' : self.deaths.serialize(),
60+
'recovered': self.recovered.serialize(),
61+
}})
62+
63+
# Return the serialized location.
64+
return serialized

app/routes/v2/locations.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@app.route('/v2/locations')
55
def locations():
66
# Query parameters.
7+
timelines = request.args.get('timelines', type=bool, default=False)
78
country_code = request.args.get('country_code', type=str)
89

910
# Retrieve all the locations.
@@ -16,25 +17,13 @@ def locations():
1617
# Serialize each location and return.
1718
return jsonify({
1819
'locations': [
19-
location.serialize() for location in locations
20+
location.serialize(timelines) for location in locations
2021
]
2122
})
2223

2324
@app.route('/v2/locations/<int:id>')
2425
def location(id):
25-
# Retrieve location with the provided id.
26-
location = jhu.get(id)
27-
28-
# Get all the timelines.
29-
timelines = {
30-
'confirmed': location.confirmed.serialize(),
31-
'deaths' : location.deaths.serialize(),
32-
'recovered': location.recovered.serialize(),
33-
}
34-
35-
# Serialize the location, add timelines, and then return.
26+
# Serialize the location with timelines.
3627
return jsonify({
37-
'location': {
38-
**jhu.get(id).serialize(), **{ 'timelines': timelines }
39-
}
28+
'location': jhu.get(id).serialize(True)
4029
})

app/timeline.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ def latest(self):
2525

2626
def serialize(self):
2727
"""
28-
Serializes the data into a dict.
28+
Serializes the timeline into a dict.
29+
30+
:returns: The serialized timeline.
31+
:rtype: dict
2932
"""
3033
return {
3134
'latest' : self.latest,

0 commit comments

Comments
 (0)