Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ Additionally, you can also filter by country ([alpha-2 country_code](https://en.
GET /v2/locations?country_code=US
```

### Getting a specific location (includes timeline).
Include timelines.
```http
GET /v2/locations?country_code=US&timelines=true
```

### Getting a specific location (includes timeline by default).
```http
GET /v2/locations/:id
```
Expand Down
3 changes: 3 additions & 0 deletions app/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def __init__(self, latitude, longitude):
def serialize(self):
"""
Serializes the coordinates into a dict.

:returns: The serialized coordinates.
:rtype: dict
"""
return {
'latitude' : self.latitude,
Expand Down
23 changes: 19 additions & 4 deletions app/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ def country_code(self):
"""
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()

def serialize(self):
def serialize(self, timelines = False):
"""
Serializes the location into a dict.

:param timelines: Whether to include the timelines.
:returns: The serialized location.
:rtype: dict
"""
return {
serialized = {
# General info.
'id' : self.id,
'country' : self.country,
Expand All @@ -45,5 +49,16 @@ def serialize(self):
'confirmed': self.confirmed.latest,
'deaths' : self.deaths.latest,
'recovered': self.recovered.latest
}
}
},
}

# Whether to include the timelines or not.
if timelines:
serialized.update({ 'timelines': {
'confirmed': self.confirmed.serialize(),
'deaths' : self.deaths.serialize(),
'recovered': self.recovered.serialize(),
}})

# Return the serialized location.
return serialized
19 changes: 4 additions & 15 deletions app/routes/v2/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@app.route('/v2/locations')
def locations():
# Query parameters.
timelines = request.args.get('timelines', type=bool, default=False)
country_code = request.args.get('country_code', type=str)

# Retrieve all the locations.
Expand All @@ -16,25 +17,13 @@ def locations():
# Serialize each location and return.
return jsonify({
'locations': [
location.serialize() for location in locations
location.serialize(timelines) for location in locations
]
})

@app.route('/v2/locations/<int:id>')
def location(id):
# Retrieve location with the provided id.
location = jhu.get(id)

# Get all the timelines.
timelines = {
'confirmed': location.confirmed.serialize(),
'deaths' : location.deaths.serialize(),
'recovered': location.recovered.serialize(),
}

# Serialize the location, add timelines, and then return.
# Serialize the location with timelines.
return jsonify({
'location': {
**jhu.get(id).serialize(), **{ 'timelines': timelines }
}
'location': jhu.get(id).serialize(True)
})
5 changes: 4 additions & 1 deletion app/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def latest(self):

def serialize(self):
"""
Serializes the data into a dict.
Serializes the timeline into a dict.

:returns: The serialized timeline.
:rtype: dict
"""
return {
'latest' : self.latest,
Expand Down