Skip to content

Commit

Permalink
Added support for multiple API versions (#533)
Browse files Browse the repository at this point in the history
* 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

* reformatted using black

/Users/drthrash/PycharmProjects/ihatemoney/ihatemoney/api/v1/resources.py
reformatted /Users/drthrash/PycharmProjects/ihatemoney/ihatemoney/api/common.py
All done! ✨ 🍰 ✨

* Applying fix for unused import in init.py

https://stackoverflow.com/questions/31079047/python-pep8-class-in-init-imported-but-not-used

* Formatting changes recommended by black

All done! ✨ 🍰 ✨
1 file reformatted, 22 files left unchanged.
  • Loading branch information
jimbo2106 authored Feb 20, 2020
1 parent 32d7617 commit 72653c0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
Empty file added ihatemoney/api/__init__.py
Empty file.
26 changes: 2 additions & 24 deletions ihatemoney/api.py → ihatemoney/api/common.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,11 +9,6 @@
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.
Expand Down Expand Up @@ -195,19 +189,3 @@ def get(self, project):

token = project.generate_token()
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>"
)
5 changes: 5 additions & 0 deletions ihatemoney/api/v1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .resources import api

__all__ = [
"api",
]
34 changes: 34 additions & 0 deletions ihatemoney/api/v1/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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>"
)
4 changes: 2 additions & 2 deletions ihatemoney/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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 apiv1
from ihatemoney.models import db
from ihatemoney.utils import (
IhmJSONEncoder,
Expand Down Expand Up @@ -132,7 +132,7 @@ def create_app(

validate_configuration(app)
app.register_blueprint(web_interface)
app.register_blueprint(api)
app.register_blueprint(apiv1)
app.register_error_handler(404, page_not_found)

# Configure the a, root="main"pplication
Expand Down

0 comments on commit 72653c0

Please sign in to comment.