Last active
October 12, 2017 16:22
-
-
Save bewt85/5781926 to your computer and use it in GitHub Desktop.
A better (non-blueprinting) way of splitting a Flask-RESTful project over a number of files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.ext.restful import Api | |
from todo import TodoList, Todo | |
from done import CompleteTaskList, CompleteTask | |
app = Flask(__name__) | |
api = Api(app) | |
api.add_resource(CompleteTaskList, '/dones/') | |
api.add_resource(CompleteTask, '/dones/<string:id>', endpoint='done_ep') | |
api.add_resource(TodoList, '/todos/') | |
api.add_resource(Todo, '/todos/<string:id>', endpoint='todo_ep') | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask.ext.restful import reqparse, abort, Api, Resource | |
from flask.ext.restful import fields, marshal, marshal_with, Api | |
COMPLETED_TASKS = [ | |
{'id': 'done1', 'task': 'buy milk'}, | |
{'id': 'done2', 'task': 'buy tea bag(s)'}, | |
{'id': 'done3', 'task': 'make tea'}, | |
] | |
def get_done_or_404(id): | |
for done in COMPLETED_TASKS: | |
if 'id' in done and done['id'] == id: | |
return done | |
abort(404, message="Complete task {} doesn't exist".format(id)) | |
parser = reqparse.RequestParser() | |
parser.add_argument('task', type=str) | |
done_fields = { | |
'id': fields.String, | |
'task': fields.String, | |
'uri': fields.Url('.done_ep') | |
} | |
# CompleteTask | |
# show a single done item and lets you delete them | |
class CompleteTask(Resource): | |
@marshal_with(done_fields) | |
def get(self, id): | |
done = get_done_or_404(id) | |
return done | |
def delete(self, id): | |
done = get_done_or_404(id) | |
COMPLETED_TASKS.remove(done) | |
return '', 204 | |
@marshal_with(done_fields) | |
def put(self, id): | |
done = get_done_or_404(id) | |
args = parser.parse_args() | |
done['task'] = args['task'] | |
return done, 201 | |
# CompleteTaskList | |
# shows a list of all dones, and lets you POST to add new tasks | |
class CompleteTaskList(Resource): | |
@marshal_with(done_fields) | |
def get(self): | |
return COMPLETED_TASKS | |
@marshal_with(done_fields) | |
def post(self): | |
args = parser.parse_args() | |
id = 'done%d' % (len(COMPLETED_TASKS) + 1) | |
done = {'id': id, 'task': args['task']} | |
COMPLETED_TASKS.append(done) | |
return done, 201 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask.ext.restful import reqparse, abort, Api, Resource | |
from flask.ext.restful import fields, marshal, marshal_with, Api | |
TODOS = [ | |
{'id': 'todo1', 'task': 'build an API'}, | |
{'id': 'todo2', 'task': '?????'}, | |
{'id': 'todo3', 'task': 'profit!'}, | |
] | |
def get_todo_or_404(id): | |
for todo in TODOS: | |
if 'id' in todo and todo['id'] == id: | |
return todo | |
abort(404, message="Todo {} doesn't exist".format(id)) | |
parser = reqparse.RequestParser() | |
parser.add_argument('task', type=str) | |
todo_fields = { | |
'id': fields.String, | |
'task': fields.String, | |
'uri': fields.Url('.todo_ep') | |
} | |
# Todo | |
# show a single todo item and lets you delete them | |
class Todo(Resource): | |
@marshal_with(todo_fields) | |
def get(self, id): | |
todo = get_todo_or_404(id) | |
return todo | |
def delete(self, id): | |
todo = get_todo_or_404(id) | |
TODOS.remove(todo) | |
return '', 204 | |
@marshal_with(todo_fields) | |
def put(self, id): | |
todo = get_todo_or_404(id) | |
args = parser.parse_args() | |
todo['task'] = args['task'] | |
return todo, 201 | |
# TodoList | |
# shows a list of all todos, and lets you POST to add new tasks | |
class TodoList(Resource): | |
@marshal_with(todo_fields) | |
def get(self): | |
return TODOS | |
@marshal_with(todo_fields) | |
def post(self): | |
args = parser.parse_args() | |
id = 'todo%d' % (len(TODOS) + 1) | |
todo = {'id': id, 'task': args['task']} | |
TODOS.append(todo) | |
return todo, 201 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you tell what is the point can this be ?