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 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 diff --git a/app/__init__.py b/app/__init__.py index 6414cb3e..26088cbf 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. @@ -16,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/config/__init__.py b/app/config/__init__.py new file mode 100644 index 00000000..e69de29b 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/__init__.py b/app/routes/v1/__init__.py new file mode 100644 index 00000000..e69de29b 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/__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/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 540b6303..2e448d54 100644 --- a/app/routes/v2/locations.py +++ b/app/routes/v2/locations.py @@ -1,8 +1,9 @@ from distutils.util import strtobool 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 = strtobool(request.args.get('timelines', default='0')) @@ -22,7 +23,7 @@ def locations(): ] }) -@app.route('/v2/locations/') +@rest_api_v2.route('/locations/') def location(id): # Query parameters. timelines = strtobool(request.args.get('timelines', default='1')) diff --git a/app/utils/__init__.py b/app/utils/__init__.py new file mode 100644 index 00000000..e69de29b