1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
todos = {}
class TodoSimple(Resource):
"""
You can try this example as follow:
$ curl http://localhost:5000/todo1 -d "data=Remember the milk" -X PUT
$ curl http://localhost:5000/todo1
{"todo1": "Remember the milk"}
$ curl http://localhost:5000/todo2 -d "data=Change my breakpads" -X PUT
$ curl http://localhost:5000/todo2
{"todo2": "Change my breakpads"}
Or from python if you have requests :
>>> from requests import put, get
>>> put('http://localhost:5000/todo1', data={'data': 'Remember the milk'}).json
{u'todo1': u'Remember the milk'}
>>> get('http://localhost:5000/todo1').json
{u'todo1': u'Remember the milk'}
>>> put('http://localhost:5000/todo2', data={'data': 'Change my breakpads'}).json
{u'todo2': u'Change my breakpads'}
>>> get('http://localhost:5000/todo2').json
{u'todo2': u'Change my breakpads'}
"""
def get(self, todo_id):
return {todo_id: todos[todo_id]}
def put(self, todo_id):
todos[todo_id] = request.form['data']
return {todo_id: todos[todo_id]}
api.add_resource(TodoSimple, '/<string:todo_id>')
if __name__ == '__main__':
app.run(debug=False)
|