Skip to content

Commit be71d1d

Browse files
committed
Separate different API endpoint in Blueprints ExpDev07#93
1 parent 63c80f4 commit be71d1d

File tree

9 files changed

+30
-16
lines changed

9 files changed

+30
-16
lines changed

app/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ def create_app():
1919
with app.app_context():
2020
# Import routes.
2121
from . import routes
22+
#register api endpoints
23+
app.register_blueprint(routes.rest_api_v1)
24+
app.register_blueprint(routes.rest_api_v2)
2225

2326
# Return created app.
24-
return app
27+
return app

app/data/__init__.py

Whitespace-only changes.

app/routes/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
from flask import redirect, current_app as app
22

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

68
# API version 2.
79
from .v2 import locations, latest
810

11+
# API version 1.
12+
from .v1 import confirmed, deaths, recovered, all
13+
914
# Redirect to project page on index.
1015
@app.route('/')
1116
def index():
12-
return redirect('https://github.com/ExpDev07/coronavirus-tracker-api', 302)
17+
return redirect('https://github.com/ExpDev07/coronavirus-tracker-api', 302)

app/routes/v1/all.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from flask import jsonify
22
from flask import current_app as app
33
from ...services.location.jhu import get_category
4+
from ...routes import rest_api_v1
45

5-
@app.route('/all')
6+
@rest_api_v1.route('/all')
67
def all():
78
# Get all the categories.
89
confirmed = get_category('confirmed')
@@ -21,4 +22,4 @@ def all():
2122
'deaths': deaths['latest'],
2223
'recovered': recovered['latest'],
2324
}
24-
})
25+
})

app/routes/v1/confirmed.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from flask import jsonify
22
from flask import current_app as app
33
from ...services.location.jhu import get_category
4+
from ...routes import rest_api_v1
45

5-
@app.route('/confirmed')
6+
@rest_api_v1.route('/confirmed')
67
def confirmed():
7-
return jsonify(get_category('confirmed'))
8+
return jsonify(get_category('confirmed'))

app/routes/v1/deaths.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from flask import jsonify
22
from flask import current_app as app
33
from ...services.location.jhu import get_category
4+
from ...routes import rest_api_v1
45

5-
@app.route('/deaths')
6+
@rest_api_v1.route('/deaths')
67
def deaths():
7-
return jsonify(get_category('deaths'))
8+
return jsonify(get_category('deaths'))

app/routes/v1/recovered.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from flask import jsonify
22
from flask import current_app as app
33
from ...services.location.jhu import get_category
4+
from ...routes import rest_api_v1
45

5-
@app.route('/recovered')
6+
@rest_api_v1.route('/recovered')
67
def recovered():
7-
return jsonify(get_category('recovered'))
8+
return jsonify(get_category('recovered'))

app/routes/v2/latest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from flask import jsonify, current_app as app
22
from ...services import jhu
3+
from ...routes import rest_api_v2
34

4-
@app.route('/v2/latest')
5+
@rest_api_v2.route('/latest')
56
def latest():
67
# Get the serialized version of all the locations.
78
locations = [ location.serialize() for location in jhu.get_all() ]
@@ -15,4 +16,4 @@ def latest():
1516
'deaths' : sum(map(lambda latest: latest['deaths'], latest)),
1617
'recovered': sum(map(lambda latest: latest['recovered'], latest)),
1718
}
18-
})
19+
})

app/routes/v2/locations.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from flask import jsonify, request, current_app as app
22
from ...services import jhu
3+
from ...routes import rest_api_v2
34

4-
@app.route('/v2/locations')
5+
@rest_api_v2.route('/locations')
56
def locations():
67
# Query parameters.
78
timelines = request.args.get('timelines', type=bool, default=False)
@@ -21,7 +22,7 @@ def locations():
2122
]
2223
})
2324

24-
@app.route('/v2/locations/<int:id>')
25+
@rest_api_v2.route('/locations/<int:id>')
2526
def location(id):
2627
# Serialize the location with timelines.
2728
return jsonify({

0 commit comments

Comments
 (0)