From 7bb67a56b6db9540205e9ff31e302a6c1217ff61 Mon Sep 17 00:00:00 2001 From: DavidRThrashJr Date: Sat, 8 Feb 2020 15:37:47 -0500 Subject: [PATCH] * Added support for multiple API version Note that no changes were made to the api, the code was refactored to allow for new versions of the api to be created down the road. Here's what this would look like: +-- api/ +-- v1/ +-- __init__.py +-- resources.py +-- v1_1/ +-- __init__.py +-- resources.py +-- v2/ +-- __init__.py +-- resources.py +-- __init__.py +-- common.py --- ihatemoney/api/__init__.py | 0 ihatemoney/{api.py => api/common.py} | 29 +++------------------------- ihatemoney/api/v1/__init__.py | 1 + ihatemoney/api/v1/resources.py | 26 +++++++++++++++++++++++++ ihatemoney/run.py | 5 +++-- 5 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 ihatemoney/api/__init__.py rename ihatemoney/{api.py => api/common.py} (86%) create mode 100644 ihatemoney/api/v1/__init__.py create mode 100644 ihatemoney/api/v1/resources.py diff --git a/ihatemoney/api/__init__.py b/ihatemoney/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ihatemoney/api.py b/ihatemoney/api/common.py similarity index 86% rename from ihatemoney/api.py rename to ihatemoney/api/common.py index 67c6cc18..9d200e53 100644 --- a/ihatemoney/api.py +++ b/ihatemoney/api/common.py @@ -1,7 +1,6 @@ # coding: utf8 -from flask import Blueprint, request, current_app -from flask_restful import Resource, Api, abort -from flask_cors import CORS +from flask import request, current_app +from flask_restful import Resource, abort from wtforms.fields.core import BooleanField from ihatemoney.models import db, Project, Person, Bill @@ -9,12 +8,6 @@ from ihatemoney.forms import ProjectForm, EditProjectForm, MemberForm, get_billf from werkzeug.security import check_password_hash from functools import wraps - -api = Blueprint("api", __name__, url_prefix="/api") -CORS(api) -restful_api = Api(api) - - def need_auth(f): """Check the request for basic authentication for a given project. @@ -194,20 +187,4 @@ class TokenHandler(Resource): return "Not Found", 404 token = project.generate_token() - return {"token": token}, 200 - - -restful_api.add_resource(ProjectsHandler, "/projects") -restful_api.add_resource(ProjectHandler, "/projects/") -restful_api.add_resource(TokenHandler, "/projects//token") -restful_api.add_resource(MembersHandler, "/projects//members") -restful_api.add_resource( - ProjectStatsHandler, "/projects//statistics" -) -restful_api.add_resource( - MemberHandler, "/projects//members/" -) -restful_api.add_resource(BillsHandler, "/projects//bills") -restful_api.add_resource( - BillHandler, "/projects//bills/" -) + return {"token": token}, 200 \ No newline at end of file diff --git a/ihatemoney/api/v1/__init__.py b/ihatemoney/api/v1/__init__.py new file mode 100644 index 00000000..3dc290f4 --- /dev/null +++ b/ihatemoney/api/v1/__init__.py @@ -0,0 +1 @@ +from .resources import api \ No newline at end of file diff --git a/ihatemoney/api/v1/resources.py b/ihatemoney/api/v1/resources.py new file mode 100644 index 00000000..445a2dc5 --- /dev/null +++ b/ihatemoney/api/v1/resources.py @@ -0,0 +1,26 @@ +# coding: utf8 +from flask import Blueprint +from flask_restful import Api +from flask_cors import CORS + +from ihatemoney.api.common import ProjectsHandler, ProjectHandler, TokenHandler\ + , MemberHandler, ProjectStatsHandler, MembersHandler, BillHandler, BillsHandler + +api = Blueprint("api", __name__, url_prefix="/api") +CORS(api) +restful_api = Api(api) + +restful_api.add_resource(ProjectsHandler, "/projects") +restful_api.add_resource(ProjectHandler, "/projects/") +restful_api.add_resource(TokenHandler, "/projects//token") +restful_api.add_resource(MembersHandler, "/projects//members") +restful_api.add_resource( + ProjectStatsHandler, "/projects//statistics" +) +restful_api.add_resource( + MemberHandler, "/projects//members/" +) +restful_api.add_resource(BillsHandler, "/projects//bills") +restful_api.add_resource( + BillHandler, "/projects//bills/" +) \ No newline at end of file diff --git a/ihatemoney/run.py b/ihatemoney/run.py index 6d1e0329..1d0b05a2 100644 --- a/ihatemoney/run.py +++ b/ihatemoney/run.py @@ -8,7 +8,7 @@ from flask_mail import Mail from flask_migrate import Migrate, upgrade, stamp from werkzeug.middleware.proxy_fix import ProxyFix -from ihatemoney.api import api +from ihatemoney.api.v1 import api as api_v1 from ihatemoney.models import db from ihatemoney.utils import ( IhmJSONEncoder, @@ -132,7 +132,8 @@ def create_app( validate_configuration(app) app.register_blueprint(web_interface) - app.register_blueprint(api) + app.register_blueprint(api_v1) + app.register_blueprint(api_v2) app.register_error_handler(404, page_not_found) # Configure the a, root="main"pplication