什么是RESTful
RESTful是一种设计思想,一种风格。
RESTful主要包括 资源 和 对资源的操作。
资源: 对实体的抽象,图书、音乐、电影、学生等都属于资源。
对资源的操作:
========== ===================== ==================================
HTTP 方法 行为 示例
========== ===================== ==================================
GET 获取资源的信息 http://example.com/api/students
GET 获取某个特定资源的信息 http://example.com/api/students/123
POST 创建新资源 http://example.com/api/students
PUT 更新资源 http://example.com/api/students/123
DELETE 删除资源 http://example.com/api/students/123
========== ====================== ==================================
使用Flask开发一个简单的RESTful
要实现的接口:
========== ===================== ================================================
请求方法 行为 接口地址
========== ===================== ================================================
GET 获取资源的信息 http://127.0.0.1:5000/todo/api/v1.0/tasks
GET 获取某个特定资源的信息 http://127.0.0.1:5000/todo/api/v1.0/tasks/123
POST 创建新资源 http://127.0.0.1:5000/todo/api/v1.0/tasks
PUT 更新资源 http://127.0.0.1:5000/todo/api/v1.0/tasks/123
DELETE 删除资源 http://127.0.0.1:5000/todo/api/v1.0/tasks/123
========== ====================== ================================================
完整代码如下:
#!/usr/bin/env python
from flask import Flask, jsonify
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
"""获取所有任务"""
return jsonify({'tasks': tasks})
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
"""获取指定task_id的任务"""
task = filter(lambda t: t['id'] == task_id, tasks)
if len(task) == 0:
abort(404)
return jsonify({'task': task[0]})
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
"""创建任务"""
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task': task}), 201
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
"""更新任务"""
task = filter(lambda t: t['id'] == task_id, tasks)
if len(task) == 0:
abort(404)
if not request.json:
abort(400)
if 'title' in request.json and type(request.json['title']) != unicode:
abort(400)
if 'description' in request.json and type(request.json['description']) is not unicode:
abort(400)
if 'done' in request.json and type(request.json['done']) is not bool:
abort(400)
task[0]['title'] = request.json.get('title', task[0]['title'])
task[0]['description'] = request.json.get('description', task[0]['description'])
task[0]['done'] = request.json.get('done', task[0]['done'])
return jsonify({'task': task[0]})
@app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
"""删除任务"""
task = filter(lambda t: t['id'] == task_id, tasks)
if len(task) == 0:
abort(404)
tasks.remove(task[0])
return jsonify({'result': True})
if __name__ == '__main__':
app.run(debug=True)
视频地址
https://v.douyu.com/show/brN0MmOjmrrvpyxA
参考资料
http://www.pythondoc.com/flask-restful/first.html
http://www.pythondoc.com/Flask-RESTful/quickstart.html