Permalink
Browse files

make things 'actually' restful ;)

  • Loading branch information...
1 parent b68548e commit 736ef1c030d551cd18445178636cf6d8312564b5 @mmautner committed Jun 30, 2014
Showing with 77 additions and 64 deletions.
  1. +1 −0 .gitignore
  2. +2 −2 README.md
  3. +5 −54 app.py
  4. +11 −0 db.py
  5. +0 −8 models.py
  6. +58 −0 resources.py
View
@@ -52,3 +52,4 @@ coverage.xml
# Sphinx documentation
docs/_build/
+main.db
View
@@ -21,13 +21,13 @@ Just a demo REST API using Flask, Flask-Restful and SQLAlchemy
[]
>> requests.post('http://localhost:5000/todos',
headers={'Content-Type': 'application/json'},
- data=json.dumps({'task': 'go outside!'}).json()
+ data=json.dumps({'task': 'go outside!'})).json()
{u'id': 1, u'task': u'go outside!'}
>> requests.get('http://localhost:5000/todos/1').json()
{u'id': 1, u'task': u'go outside!'}
>> requests.put('http://localhost:5000/todos/1',
headers={'Content-Type': 'application/json'},
- data=json.dumps({'task': 'go to the gym'}).json()
+ data=json.dumps({'task': 'go to the gym'})).json()
{u'id': 1, u'task': u'go to the gym'}
>> requests.delete('http://localhost:5000/todos/1')
>> requests.get('http://localhost:5000/todos').json()
View
59 app.py
@@ -1,65 +1,16 @@
#!/usr/bin/env python
from flask import Flask
-from flask.ext.restful import reqparse, abort, Api, Resource
-from sqlalchemy import create_engine
-from sqlalchemy.orm import scoped_session
-from sqlalchemy.orm import sessionmaker
-from models import Todo
+from flask.ext.restful import Api
app = Flask(__name__)
api = Api(app)
-from sqlalchemy import create_engine
-from settings import DB_URI
-
-Session = sessionmaker(autocommit=False,
- autoflush=False,
- bind=create_engine(DB_URI))
-session = scoped_session(Session)
-
-parser = reqparse.RequestParser()
-parser.add_argument('task', type=str)
-
-class TodoResource(Resource):
- def get(self, todo_id):
- todo = session.query(Todo).filter(Todo.id == todo_id).first()
- if not todo:
- abort(404, message="Todo {} doesn't exist".format(todo_id))
- return todo.serialize
-
- def delete(self, todo_id):
- todo = session.query(Todo).filter(Todo.id == todo_id).first()
- if not todo:
- abort(404, message="Todo {} doesn't exist".format(todo_id))
- session.delete(todo)
- session.commit()
- return {}, 204
-
- def put(self, todo_id):
- parsed_args = parser.parse_args()
- todo = session.query(Todo).filter(Todo.id == todo_id).first()
- todo.task = parsed_args['task']
- session.add(todo)
- session.commit()
- return todo.serialize, 201
-
-
-class TodoListResource(Resource):
- def get(self):
- todos = session.query(Todo).all()
- return [todo.serialize for todo in todos]
-
- def post(self):
- parsed_args = parser.parse_args()
- todo = Todo(task=parsed_args['task'])
- session.add(todo)
- session.commit()
- return todo.serialize, 201
-
-api.add_resource(TodoListResource, '/todos')
-api.add_resource(TodoResource, '/todos/<string:todo_id>')
+from resources import TodoListResource
+from resources import TodoResource
+api.add_resource(TodoListResource, '/todos', endpoint='todos')
+api.add_resource(TodoResource, '/todos/<string:id>', endpoint='todo')
if __name__ == '__main__':
app.run(debug=True)
View
11 db.py
@@ -0,0 +1,11 @@
+
+from sqlalchemy import create_engine
+from sqlalchemy.orm import scoped_session
+from sqlalchemy.orm import sessionmaker
+from settings import DB_URI
+
+Session = sessionmaker(autocommit=False,
+ autoflush=False,
+ bind=create_engine(DB_URI))
+session = scoped_session(Session)
+
View
@@ -13,14 +13,6 @@ class Todo(Base):
id = Column(Integer, primary_key=True)
task = Column(String(255))
- @property
- def serialize(self):
- """Return object data in easily serializeable format"""
- return {
- 'id' : self.id,
- 'task' : self.task
- }
-
if __name__ == "__main__":
from sqlalchemy import create_engine
from settings import DB_URI
View
@@ -0,0 +1,58 @@
+
+from models import Todo
+from db import session
+
+from flask.ext.restful import reqparse
+from flask.ext.restful import abort
+from flask.ext.restful import Resource
+from flask.ext.restful import fields
+from flask.ext.restful import marshal_with
+
+todo_fields = {
+ 'id': fields.Integer,
+ 'task': fields.String,
+ 'uri': fields.Url('todo', absolute=True),
+}
+
+parser = reqparse.RequestParser()
+parser.add_argument('task', type=str)
+
+class TodoResource(Resource):
+ @marshal_with(todo_fields)
+ def get(self, id):
+ todo = session.query(Todo).filter(Todo.id == id).first()
+ if not todo:
+ abort(404, message="Todo {} doesn't exist".format(id))
+ return todo
+
+ def delete(self, id):
+ todo = session.query(Todo).filter(Todo.id == id).first()
+ if not todo:
+ abort(404, message="Todo {} doesn't exist".format(id))
+ session.delete(todo)
+ session.commit()
+ return {}, 204
+
+ @marshal_with(todo_fields)
+ def put(self, id):
+ parsed_args = parser.parse_args()
+ todo = session.query(Todo).filter(Todo.id == id).first()
+ todo.task = parsed_args['task']
+ session.add(todo)
+ session.commit()
+ return todo, 201
+
+
+class TodoListResource(Resource):
+ @marshal_with(todo_fields)
+ def get(self):
+ todos = session.query(Todo).all()
+ return todos
+
+ @marshal_with(todo_fields)
+ def post(self):
+ parsed_args = parser.parse_args()
+ todo = Todo(task=parsed_args['task'])
+ session.add(todo)
+ session.commit()
+ return todo, 201

0 comments on commit 736ef1c

Please sign in to comment.