Created
January 24, 2019 05:51
-
-
Save halivert/7dc06a38b30c2f7093f5b841e893ec6a to your computer and use it in GitHub Desktop.
Flask minimal API
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) | |
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) |
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_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