用来测试接口的 flask server
Restful规范
"""
RESTFul url 规范,参考:http://www.pythondoc.com/flask-restful/second.html
========== =============================================== =============================
HTTP 方法 URL 动作
========== =============================================== ==============================
GET http://[hostname]/todo/api/v1.0/tasks 检索任务列表
GET http://[hostname]/todo/api/v1.0/tasks/[task_id] 检索某个任务
POST http://[hostname]/todo/api/v1.0/tasks 创建新任务
PUT http://[hostname]/todo/api/v1.0/tasks/[task_id] 更新任务
DELETE http://[hostname]/todo/api/v1.0/tasks/[task_id] 删除任务
========== ================================================ =============================
命名规范:
# 我们使用 SomeListHandler 命名而不是复数形式方便区分,比如 VideosHandler 和 VideoHandler 不容易区分
"""
class TaskListHandler(BaseHandler):
def get(self):
"""获取列表"""
pass
def post(self):
"""创建一条新的记录"""
pass
class TaskHandler(BaseHandler):
def get(self, _id):
"""获取一条记录的信息"""
pass
def put(self, _id):
"""更新一条记录的信息"""
pass
def delete(self, _id):
"""删除一条记录"""
pass
测试 server
# pip install flask Flask-RESTful
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
from flask import request
app = Flask(__name__)
api = Api(app)
TODOS = {
'todo1': {'task': 'build an API'},
'todo2': {'task': '?????'},
'todo3': {'task': 'profit!'},
}
def abort_if_todo_doesnt_exist(todo_id):
if todo_id not in TODOS:
abort(404, message="Todo {} doesn't exist".format(todo_id))
parser = reqparse.RequestParser()
parser.add_argument('task')
# Todo
# shows a single todo item and lets you delete a todo item
class Todo(Resource):
def get(self, todo_id):
abort_if_todo_doesnt_exist(todo_id)
return TODOS[todo_id]
def delete(self, todo_id):
abort_if_todo_doesnt_exist(todo_id)
del TODOS[todo_id]
return '', 204
def put(self, todo_id):
args = parser.parse_args()
task = {'task': args['task']}
TODOS[todo_id] = task
return task, 201
# TodoList
# shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
def get(self):
return TODOS
def post(self):
print(request.data)
args = parser.parse_args()
todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1
todo_id = 'todo%i' % todo_id
TODOS[todo_id] = {'task': args['task']}
return TODOS[todo_id], 201
class TaskList(Resource):
def post(self):
print(request.data)
return {"id": 1}
class Ping(Resource):
def get(self):
return 'pong'
##
# Actually setup the Api resource routing here
##
api.add_resource(Ping, '/')
api.add_resource(TodoList, '/todos')
api.add_resource(Todo, '/todos/<todo_id>')
api.add_resource(TaskList, '/tasks')
if __name__ == '__main__':
app.run(debug=True) # port=5000
当前内容版权归 PegasusWang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 PegasusWang .