mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-06 05:01:48 +02:00
Added support for multiple API versions
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
This commit is contained in:
parent
1f62f18154
commit
a78b2f4a31
5 changed files with 32 additions and 27 deletions
0
ihatemoney/api/__init__.py
Normal file
0
ihatemoney/api/__init__.py
Normal file
|
@ -1,7 +1,6 @@
|
||||||
# coding: utf8
|
# coding: utf8
|
||||||
from flask import Blueprint, request, current_app
|
from flask import request, current_app
|
||||||
from flask_restful import Resource, Api, abort
|
from flask_restful import Resource, abort
|
||||||
from flask_cors import CORS
|
|
||||||
from wtforms.fields.core import BooleanField
|
from wtforms.fields.core import BooleanField
|
||||||
|
|
||||||
from ihatemoney.models import db, Project, Person, Bill
|
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 werkzeug.security import check_password_hash
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
|
||||||
api = Blueprint("api", __name__, url_prefix="/api")
|
|
||||||
CORS(api)
|
|
||||||
restful_api = Api(api)
|
|
||||||
|
|
||||||
|
|
||||||
def need_auth(f):
|
def need_auth(f):
|
||||||
"""Check the request for basic authentication for a given project.
|
"""Check the request for basic authentication for a given project.
|
||||||
|
|
||||||
|
@ -196,18 +189,3 @@ class TokenHandler(Resource):
|
||||||
token = project.generate_token()
|
token = project.generate_token()
|
||||||
return {"token": token}, 200
|
return {"token": token}, 200
|
||||||
|
|
||||||
|
|
||||||
restful_api.add_resource(ProjectsHandler, "/projects")
|
|
||||||
restful_api.add_resource(ProjectHandler, "/projects/<string:project_id>")
|
|
||||||
restful_api.add_resource(TokenHandler, "/projects/<string:project_id>/token")
|
|
||||||
restful_api.add_resource(MembersHandler, "/projects/<string:project_id>/members")
|
|
||||||
restful_api.add_resource(
|
|
||||||
ProjectStatsHandler, "/projects/<string:project_id>/statistics"
|
|
||||||
)
|
|
||||||
restful_api.add_resource(
|
|
||||||
MemberHandler, "/projects/<string:project_id>/members/<int:member_id>"
|
|
||||||
)
|
|
||||||
restful_api.add_resource(BillsHandler, "/projects/<string:project_id>/bills")
|
|
||||||
restful_api.add_resource(
|
|
||||||
BillHandler, "/projects/<string:project_id>/bills/<int:bill_id>"
|
|
||||||
)
|
|
1
ihatemoney/api/v1/__init__.py
Normal file
1
ihatemoney/api/v1/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from .resources import api
|
26
ihatemoney/api/v1/resources.py
Normal file
26
ihatemoney/api/v1/resources.py
Normal file
|
@ -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/<string:project_id>")
|
||||||
|
restful_api.add_resource(TokenHandler, "/projects/<string:project_id>/token")
|
||||||
|
restful_api.add_resource(MembersHandler, "/projects/<string:project_id>/members")
|
||||||
|
restful_api.add_resource(
|
||||||
|
ProjectStatsHandler, "/projects/<string:project_id>/statistics"
|
||||||
|
)
|
||||||
|
restful_api.add_resource(
|
||||||
|
MemberHandler, "/projects/<string:project_id>/members/<int:member_id>"
|
||||||
|
)
|
||||||
|
restful_api.add_resource(BillsHandler, "/projects/<string:project_id>/bills")
|
||||||
|
restful_api.add_resource(
|
||||||
|
BillHandler, "/projects/<string:project_id>/bills/<int:bill_id>"
|
||||||
|
)
|
|
@ -8,7 +8,7 @@ from flask_mail import Mail
|
||||||
from flask_migrate import Migrate, upgrade, stamp
|
from flask_migrate import Migrate, upgrade, stamp
|
||||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||||
|
|
||||||
from ihatemoney.api import api
|
from ihatemoney.api.v1 import api as apiv1
|
||||||
from ihatemoney.models import db
|
from ihatemoney.models import db
|
||||||
from ihatemoney.utils import (
|
from ihatemoney.utils import (
|
||||||
IhmJSONEncoder,
|
IhmJSONEncoder,
|
||||||
|
@ -132,7 +132,7 @@ def create_app(
|
||||||
|
|
||||||
validate_configuration(app)
|
validate_configuration(app)
|
||||||
app.register_blueprint(web_interface)
|
app.register_blueprint(web_interface)
|
||||||
app.register_blueprint(api)
|
app.register_blueprint(apiv1)
|
||||||
app.register_error_handler(404, page_not_found)
|
app.register_error_handler(404, page_not_found)
|
||||||
|
|
||||||
# Configure the a, root="main"pplication
|
# Configure the a, root="main"pplication
|
||||||
|
|
Loading…
Reference in a new issue