Skip to content

Instantly share code, notes, and snippets.

@halivert
Created January 24, 2019 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halivert/7dc06a38b30c2f7093f5b841e893ec6a to your computer and use it in GitHub Desktop.
Save halivert/7dc06a38b30c2f7093f5b841e893ec6a to your computer and use it in GitHub Desktop.
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