]> git.zarvox.org Git - imoo.git/commitdiff
WIP: Add API skeleton using Flask-RESTful
authorDrew Fisher <drew@aerofs.com>
Wed, 12 Mar 2014 17:21:57 +0000 (10:21 -0700)
committerDrew Fisher <drew@aerofs.com>
Wed, 12 Mar 2014 17:58:20 +0000 (10:58 -0700)
README.md
imoo/__init__.py
imoo/api.py [new file with mode: 0644]

index a68d7931f34bdda2eb0d2d51aa4e76fd4b91a1cd..e56155d7d4a7bef2e4195329299165ac5ae7d503 100644 (file)
--- 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
 ```
index 99d898768f060d3271261513af644a3cb3e81a39..d6f0a4c84ba46e2d5705bdccd880bf8876534a98 100644 (file)
@@ -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 (file)
index 0000000..a0eaf13
--- /dev/null
@@ -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/<string:user_id>')
+api.add_resource(ChatNetworkAccountResource, '/accounts/<string:account_id>')