diff --git a/README.md b/README.md index 0f7f694b..7a9ea353 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/app/coordinates.py b/app/coordinates.py index 0243e637..93b5bd9e 100644 --- a/app/coordinates.py +++ b/app/coordinates.py @@ -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, diff --git a/app/location.py b/app/location.py index e348c697..9d552371 100644 --- a/app/location.py +++ b/app/location.py @@ -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, @@ -45,5 +49,16 @@ def serialize(self): 'confirmed': self.confirmed.latest, 'deaths' : self.deaths.latest, 'recovered': self.recovered.latest - } - } \ No newline at end of file + }, + } + + # 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 \ No newline at end of file diff --git a/app/routes/v2/locations.py b/app/routes/v2/locations.py index eb50e251..17f93e1b 100644 --- a/app/routes/v2/locations.py +++ b/app/routes/v2/locations.py @@ -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. @@ -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/') 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) }) \ No newline at end of file diff --git a/app/timeline.py b/app/timeline.py index 48af1ac5..dc9adacd 100644 --- a/app/timeline.py +++ b/app/timeline.py @@ -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,