Created
September 7, 2018 15:55
-
-
Save Sylvance/e2e9eee395a616e378ba70638d33641b to your computer and use it in GitHub Desktop.
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_restplus import Api, Resource, fields | |
from werkzeug.contrib.fixers import ProxyFix | |
app = Flask(__name__) | |
app.wsgi_app = ProxyFix(app.wsgi_app) | |
api = Api(app, version='1.0', title='Todo API', | |
description='A simple Todo API', | |
) | |
ns = api.namespace('todos', description='TODO operations') | |
TODOS = { | |
'todo1': {'description': 'Build a REST API'}, | |
'todo2': {'description': 'Connect to database'}, | |
'todo3': {'description': 'Host documentation on Heroku'}, | |
} | |
TASKS = { | |
'task1': {'todo_id': 'todo1', 'description': 'Write controller logic.'}, | |
'task2': {'todo_id': 'todo1', 'description': 'Write model logic.'}, | |
'task3': {'todo_id': 'todo1', 'description': 'Write view logic.'}, | |
} | |
def abort_if_todo_doesnt_exist(todo_id=None, task_id=None): | |
if todo_id not in TODOS: | |
abort(404, message="Todo {} doesn't exist".format(todo_id)) | |
if task_id not in TASKS: | |
abort(404, message="Task {} doesn't exist".format(task_id)) | |
parser = reqparse.RequestParser() | |
parser.add_argument('task') | |
# Todo | |
# shows a single todo item and lets you delete a todo item | |
@ns.route('/<int:todo_id>') | |
class Todo(Resource): | |
@ns.doc('show_todo') | |
def get(self, todo_id): | |
abort_if_todo_doesnt_exist(todo_id) | |
return TODOS[todo_id] | |
@ns.doc('delete_todo') | |
@ns.response(204, 'Todo deleted') | |
def delete(self, todo_id): | |
abort_if_todo_doesnt_exist(todo_id) | |
del TODOS[todo_id] | |
return '', 204 | |
@ns.doc('update_todo') | |
def put(self, todo_id): | |
args = parser.parse_args() | |
task = {'description': args['task']} | |
TODOS[todo_id] = task | |
return task, 201 | |
# TodoList | |
# shows a list of all todos, and lets you POST to add new tasks | |
@ns.route('/') | |
class TodoList(Resource): | |
@ns.doc('list_todos') | |
def get(self): | |
return TODOS | |
@ns.doc('create_todo') | |
def post(self): | |
args = parser.parse_args() | |
todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1 | |
todo_id = 'todo%i' % todo_id | |
TODOS[todo_id] = {'description': args['task']} | |
return TODOS[todo_id], 201 | |
# Task | |
# shows a single task item and lets you delete a task item | |
@ns.route('/<int:todo_id>/tasks/<int:task_id>') | |
class Task(Resource): | |
@ns.doc('show_task') | |
def get(self, todo_id, task_id): | |
abort_if_todo_doesnt_exist(todo_id, task_id) | |
return TASKS[task_id] | |
@ns.doc('delete_task') | |
@ns.response(204, 'Task deleted') | |
def delete(self, todo_id, task_id): | |
abort_if_todo_doesnt_exist(todo_id, task_id) | |
del TASKS[task_id] | |
return '', 204 | |
@ns.doc('update_task') | |
def put(self, todo_id, task_id): | |
args = parser.parse_args() | |
task = {'todo_id': todo_id, 'description': args['task']} | |
TASKS[task_id] = task | |
return task, 201 | |
# TaskList | |
# shows a list of all tasks for a particular Todo, and lets you POST to add new tasks | |
@ns.route('/<int:todo_id>/tasks') | |
class TaskList(Resource): | |
@ns.doc('show_tasks_of_todo_id') | |
def get(self, todo_id): | |
tasks_of_todo_id = {} | |
for key, value in TASKS.items(): | |
if value['todo_id'] == todo_id | |
tasks_of_todo_id[key] = value | |
return tasks_of_todo_id | |
@ns.doc('create_task_of_todo_id') | |
def post(self, todo_id): | |
args = parser.parse_args() | |
task_id = int(max(TASKS.keys()).lstrip('task')) + 1 | |
task_id = 'task%i' % task_id | |
TASKS[task_id] = {'todo_id': todo_id, 'description': args['task']} | |
return TASKS[task_id], 201 | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment