From 19347bc1b867b3aa518c1ae17023f988f7d9a81e Mon Sep 17 00:00:00 2001 From: gribok <40306040+gribok@users.noreply.github.com> Date: Thu, 19 Mar 2020 20:15:22 +0000 Subject: [PATCH 1/5] add empty __init__.py for pylint. See ExpDev07/coronavirus-tracker-api#91 --- app/config/__init__.py | 0 app/routes/v1/__init__.py | 0 app/routes/v2/__init__.py | 1 + app/utils/__init__.py | 0 4 files changed, 1 insertion(+) create mode 100644 app/config/__init__.py create mode 100644 app/routes/v1/__init__.py create mode 100644 app/routes/v2/__init__.py create mode 100644 app/utils/__init__.py diff --git a/app/config/__init__.py b/app/config/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/routes/v1/__init__.py b/app/routes/v1/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/routes/v2/__init__.py b/app/routes/v2/__init__.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/app/routes/v2/__init__.py @@ -0,0 +1 @@ + diff --git a/app/utils/__init__.py b/app/utils/__init__.py new file mode 100644 index 00000000..e69de29b From 63c80f46c0f7dbcb9f259c0bcaf1df014f87528b Mon Sep 17 00:00:00 2001 From: gribok <40306040+gribok@users.noreply.github.com> Date: Thu, 19 Mar 2020 20:20:46 +0000 Subject: [PATCH 2/5] add __version__ to module. See ExpDev07/coronavirus-tracker-api#91 --- app/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 6414cb3e..58219a12 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,6 +1,9 @@ from flask import Flask from flask_cors import CORS +#see PEP396 +__version__ = '2.0' + def create_app(): """ Construct the core application. From be71d1da9214a5e287d3bc457f6ce1cdf15219f8 Mon Sep 17 00:00:00 2001 From: gribok <40306040+gribok@users.noreply.github.com> Date: Thu, 19 Mar 2020 20:25:02 +0000 Subject: [PATCH 3/5] Separate different API endpoint in Blueprints ExpDev07/coronavirus-tracker-api#93 --- app/__init__.py | 5 ++++- app/data/__init__.py | 0 app/routes/__init__.py | 11 ++++++++--- app/routes/v1/all.py | 5 +++-- app/routes/v1/confirmed.py | 5 +++-- app/routes/v1/deaths.py | 5 +++-- app/routes/v1/recovered.py | 5 +++-- app/routes/v2/latest.py | 5 +++-- app/routes/v2/locations.py | 5 +++-- 9 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 app/data/__init__.py diff --git a/app/__init__.py b/app/__init__.py index 58219a12..26088cbf 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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 \ No newline at end of file + return app diff --git a/app/data/__init__.py b/app/data/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/routes/__init__.py b/app/routes/__init__.py index 37355cc3..1a016284 100644 --- a/app/routes/__init__.py +++ b/app/routes/__init__.py @@ -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) \ No newline at end of file + return redirect('https://github.com/ExpDev07/coronavirus-tracker-api', 302) diff --git a/app/routes/v1/all.py b/app/routes/v1/all.py index 735b4d10..6c0472ef 100644 --- a/app/routes/v1/all.py +++ b/app/routes/v1/all.py @@ -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') @@ -21,4 +22,4 @@ def all(): 'deaths': deaths['latest'], 'recovered': recovered['latest'], } - }) \ No newline at end of file + }) diff --git a/app/routes/v1/confirmed.py b/app/routes/v1/confirmed.py index 52fcf7e0..d1489fd7 100644 --- a/app/routes/v1/confirmed.py +++ b/app/routes/v1/confirmed.py @@ -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')) \ No newline at end of file + return jsonify(get_category('confirmed')) diff --git a/app/routes/v1/deaths.py b/app/routes/v1/deaths.py index 76913b6d..6b398ebc 100644 --- a/app/routes/v1/deaths.py +++ b/app/routes/v1/deaths.py @@ -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')) \ No newline at end of file + return jsonify(get_category('deaths')) diff --git a/app/routes/v1/recovered.py b/app/routes/v1/recovered.py index ded8eb7c..aad645b0 100644 --- a/app/routes/v1/recovered.py +++ b/app/routes/v1/recovered.py @@ -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')) \ No newline at end of file + return jsonify(get_category('recovered')) diff --git a/app/routes/v2/latest.py b/app/routes/v2/latest.py index 0f3ba265..b8c487db 100644 --- a/app/routes/v2/latest.py +++ b/app/routes/v2/latest.py @@ -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() ] @@ -15,4 +16,4 @@ def latest(): 'deaths' : sum(map(lambda latest: latest['deaths'], latest)), 'recovered': sum(map(lambda latest: latest['recovered'], latest)), } - }) \ No newline at end of file + }) diff --git a/app/routes/v2/locations.py b/app/routes/v2/locations.py index 345f3d3b..7fcc924b 100644 --- a/app/routes/v2/locations.py +++ b/app/routes/v2/locations.py @@ -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) @@ -21,7 +22,7 @@ def locations(): ] }) -@app.route('/v2/locations/') +@rest_api_v2.route('/locations/') def location(id): # Serialize the location with timelines. return jsonify({ From 4151cd017b24fd3f511ba385fa0f74f053734431 Mon Sep 17 00:00:00 2001 From: gribok <40306040+gribok@users.noreply.github.com> Date: Thu, 19 Mar 2020 20:27:40 +0000 Subject: [PATCH 4/5] Add Makefile for automation of common steps ExpDev07/coronavirus-tracker-api#94 --- Makefile | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..8b5fc47f --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +# +# Makefile for coronavirus-tracker-api +# ~~~~~~~~~~~~~~~~~~~~~ +# +# Combines scripts for common tasks. +# + +PYTHON ?= python3 + +PHONY: all check test + +all: test pylint + +APP = app +TEST = tests + +test: + $(PYTHON) `which py.test` -s -v $(TEST) + +lint: + pylint $(APP) || true From 779e329222f2267efabb2213681aa8af0be2eeb7 Mon Sep 17 00:00:00 2001 From: gribok <40306040+gribok@users.noreply.github.com> Date: Thu, 19 Mar 2020 20:28:22 +0000 Subject: [PATCH 5/5] add makefile syntax to readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 48d4b137..a435a202 100644 --- a/README.md +++ b/README.md @@ -160,11 +160,11 @@ You will need the following things properly installed on your computer. ### Running Tests -* `py.test -s -v tests/` +* `make test` ### Linting -* `pylint app/` +* `make lint` ### Building