Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Separate different API endpoint in Blueprints #93
  • Loading branch information
gribok committed Mar 19, 2020
commit be71d1da9214a5e287d3bc457f6ce1cdf15219f8
5 changes: 4 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def create_app():
with app.app_context():
# Import routes.
from . import routes
#register api endpoints
app.register_blueprint(routes.rest_api_v1)
app.register_blueprint(routes.rest_api_v2)

# Return created app.
return app
return app
Empty file added app/data/__init__.py
Empty file.
11 changes: 8 additions & 3 deletions app/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from flask import redirect, current_app as app

# API version 1.
from .v1 import confirmed, deaths, recovered, all
#follow the import order to avoid circular dependency
from flask import Blueprint
rest_api_v1 = Blueprint("rest_api_v1", __name__, url_prefix="")
rest_api_v2 = Blueprint("rest_api_v2", __name__, url_prefix="/v2")

# API version 2.
from .v2 import locations, latest

# API version 1.
from .v1 import confirmed, deaths, recovered, all

# Redirect to project page on index.
@app.route('/')
def index():
return redirect('https://github.com/ExpDev07/coronavirus-tracker-api', 302)
return redirect('https://github.com/ExpDev07/coronavirus-tracker-api', 302)
5 changes: 3 additions & 2 deletions app/routes/v1/all.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from flask import jsonify
from flask import current_app as app
from ...services.location.jhu import get_category
from ...routes import rest_api_v1

@app.route('/all')
@rest_api_v1.route('/all')
def all():
# Get all the categories.
confirmed = get_category('confirmed')
Expand All @@ -21,4 +22,4 @@ def all():
'deaths': deaths['latest'],
'recovered': recovered['latest'],
}
})
})
5 changes: 3 additions & 2 deletions app/routes/v1/confirmed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import jsonify
from flask import current_app as app
from ...services.location.jhu import get_category
from ...routes import rest_api_v1

@app.route('/confirmed')
@rest_api_v1.route('/confirmed')
def confirmed():
return jsonify(get_category('confirmed'))
return jsonify(get_category('confirmed'))
5 changes: 3 additions & 2 deletions app/routes/v1/deaths.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import jsonify
from flask import current_app as app
from ...services.location.jhu import get_category
from ...routes import rest_api_v1

@app.route('/deaths')
@rest_api_v1.route('/deaths')
def deaths():
return jsonify(get_category('deaths'))
return jsonify(get_category('deaths'))
5 changes: 3 additions & 2 deletions app/routes/v1/recovered.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import jsonify
from flask import current_app as app
from ...services.location.jhu import get_category
from ...routes import rest_api_v1

@app.route('/recovered')
@rest_api_v1.route('/recovered')
def recovered():
return jsonify(get_category('recovered'))
return jsonify(get_category('recovered'))
5 changes: 3 additions & 2 deletions app/routes/v2/latest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import jsonify, current_app as app
from ...services import jhu
from ...routes import rest_api_v2

@app.route('/v2/latest')
@rest_api_v2.route('/latest')
def latest():
# Get the serialized version of all the locations.
locations = [ location.serialize() for location in jhu.get_all() ]
Expand All @@ -15,4 +16,4 @@ def latest():
'deaths' : sum(map(lambda latest: latest['deaths'], latest)),
'recovered': sum(map(lambda latest: latest['recovered'], latest)),
}
})
})
5 changes: 3 additions & 2 deletions app/routes/v2/locations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import jsonify, request, current_app as app
from ...services import jhu
from ...routes import rest_api_v2

@app.route('/v2/locations')
@rest_api_v2.route('/locations')
def locations():
# Query parameters.
timelines = request.args.get('timelines', type=bool, default=False)
Expand All @@ -21,7 +22,7 @@ def locations():
]
})

@app.route('/v2/locations/<int:id>')
@rest_api_v2.route('/locations/<int:id>')
def location(id):
# Serialize the location with timelines.
return jsonify({
Expand Down