Skip to content

Instantly share code, notes, and snippets.

@halivert
Created Jan 24, 2019
Embed
What would you like to do?
Flask minimal API
from flask import (Flask)
from flask_restplus import (Api)
import resources
app = Flask(__name__)
api = Api(app, prefix='/v1', catch_all_404s=True)
api.add_resource(resources.TasksList, '/tasks')
api.add_resource(resources.Task, '/tasks/<int:task_id>')
if __name__ == '__main__':
app.run(debug=False)
from flask_restplus import (Resource)
from flask import (abort)
tasks = [
{
'id': 1,
'title': u'Comprar cosas',
'description': u'Leche, Queso, Pizza, Fruta',
'done': False
},
{
'id': 2,
'title': u'Aprender python',
'description': u'Encontrar un buen tutorial',
'done': False
},
]
class TasksList(Resource):
def get(self):
return tasks, 200
class Task(Resource):
def get(self, task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
return task[0], 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment