From: Drew Fisher Date: Wed, 12 Mar 2014 17:21:57 +0000 (-0700) Subject: WIP: Add API skeleton using Flask-RESTful X-Git-Url: http://git.zarvox.org/%7Bthis.props.bicon_url%7D?a=commitdiff_plain;h=7c1af2b822506797bc9b79cc9a3509a203debc86;p=imoo.git WIP: Add API skeleton using Flask-RESTful --- diff --git a/README.md b/README.md index a68d793..e56155d 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,6 @@ SETUP ```bash virtualenv env -env/bin/pip install Flask Flask-Login Flask-SQLAlchemy Flask-WTF Flask-Scrypt sqlalchemy-migrate +env/bin/pip install Flask Flask-Login Flask-SQLAlchemy Flask-WTF Flask-Scrypt Flask-RESTful sqlalchemy-migrate ./run.py ``` diff --git a/imoo/__init__.py b/imoo/__init__.py index 99d8987..d6f0a4c 100644 --- a/imoo/__init__.py +++ b/imoo/__init__.py @@ -30,7 +30,7 @@ def migrate_database(app): # Apply all known migrations to bring the database schema up to date migrate_api.upgrade(db_uri, repo, migrate_api.version(repo)) -from imoo import views +from imoo import views, api def create_app(): app = Flask(__name__) @@ -53,6 +53,7 @@ def create_app(): # Enable routes app.register_blueprint(views.blueprint, url_prefix="") + app.register_blueprint(api.blueprint, url_prefix="/api") # Return configured app return app diff --git a/imoo/api.py b/imoo/api.py new file mode 100644 index 0000000..a0eaf13 --- /dev/null +++ b/imoo/api.py @@ -0,0 +1,32 @@ +from flask import Blueprint +from flask.ext.restful import Api, Resource, marshal, marshal_with, fields + +blueprint = Blueprint('api', 'api') +api = Api(blueprint) + +user_fields = { + 'id': fields.Integer, + 'username': fields.String, + } + +chatnetwork_fields = { + 'id': fields.Integer, + 'user_id': fields.Integer, + 'network': fields.Integer, # TODO: map this to/from ChatNetworkAccount.chatnetworks + 'username': fields.String, + } + +class UserResource(Resource): + @marshal_with(user_fields) + def get(self, user_id): + user = models.User.query.get_or_404(user_id) + return user + +class ChatNetworkAccountResource(Resource): + @marshal_with(chatnetwork_fields) + def get(self, account_id): + cna = models.ChatNetworkAccount.query.get_or_404(account_id) + return cna + +api.add_resource(UserResource, '/users/') +api.add_resource(ChatNetworkAccountResource, '/accounts/')