用来测试接口的 flask server

Restful规范

  1. """
  2. RESTFul url 规范,参考:http://www.pythondoc.com/flask-restful/second.html
  3. ========== =============================================== =============================
  4. HTTP 方法 URL 动作
  5. ========== =============================================== ==============================
  6. GET http://[hostname]/todo/api/v1.0/tasks 检索任务列表
  7. GET http://[hostname]/todo/api/v1.0/tasks/[task_id] 检索某个任务
  8. POST http://[hostname]/todo/api/v1.0/tasks 创建新任务
  9. PUT http://[hostname]/todo/api/v1.0/tasks/[task_id] 更新任务
  10. DELETE http://[hostname]/todo/api/v1.0/tasks/[task_id] 删除任务
  11. ========== ================================================ =============================
  12. 命名规范:
  13. # 我们使用 SomeListHandler 命名而不是复数形式方便区分,比如 VideosHandler 和 VideoHandler 不容易区分
  14. """
  15. class TaskListHandler(BaseHandler):
  16. def get(self):
  17. """获取列表"""
  18. pass
  19. def post(self):
  20. """创建一条新的记录"""
  21. pass
  22. class TaskHandler(BaseHandler):
  23. def get(self, _id):
  24. """获取一条记录的信息"""
  25. pass
  26. def put(self, _id):
  27. """更新一条记录的信息"""
  28. pass
  29. def delete(self, _id):
  30. """删除一条记录"""
  31. pass

测试 server

  1. # pip install flask Flask-RESTful
  2. from flask import Flask
  3. from flask_restful import reqparse, abort, Api, Resource
  4. from flask import request
  5. app = Flask(__name__)
  6. api = Api(app)
  7. TODOS = {
  8. 'todo1': {'task': 'build an API'},
  9. 'todo2': {'task': '?????'},
  10. 'todo3': {'task': 'profit!'},
  11. }
  12. def abort_if_todo_doesnt_exist(todo_id):
  13. if todo_id not in TODOS:
  14. abort(404, message="Todo {} doesn't exist".format(todo_id))
  15. parser = reqparse.RequestParser()
  16. parser.add_argument('task')
  17. # Todo
  18. # shows a single todo item and lets you delete a todo item
  19. class Todo(Resource):
  20. def get(self, todo_id):
  21. abort_if_todo_doesnt_exist(todo_id)
  22. return TODOS[todo_id]
  23. def delete(self, todo_id):
  24. abort_if_todo_doesnt_exist(todo_id)
  25. del TODOS[todo_id]
  26. return '', 204
  27. def put(self, todo_id):
  28. args = parser.parse_args()
  29. task = {'task': args['task']}
  30. TODOS[todo_id] = task
  31. return task, 201
  32. # TodoList
  33. # shows a list of all todos, and lets you POST to add new tasks
  34. class TodoList(Resource):
  35. def get(self):
  36. return TODOS
  37. def post(self):
  38. print(request.data)
  39. args = parser.parse_args()
  40. todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1
  41. todo_id = 'todo%i' % todo_id
  42. TODOS[todo_id] = {'task': args['task']}
  43. return TODOS[todo_id], 201
  44. class TaskList(Resource):
  45. def post(self):
  46. print(request.data)
  47. return {"id": 1}
  48. class Ping(Resource):
  49. def get(self):
  50. return 'pong'
  51. ##
  52. # Actually setup the Api resource routing here
  53. ##
  54. api.add_resource(Ping, '/')
  55. api.add_resource(TodoList, '/todos')
  56. api.add_resource(Todo, '/todos/<todo_id>')
  57. api.add_resource(TaskList, '/tasks')
  58. if __name__ == '__main__':
  59. app.run(debug=True) # port=5000