在 Flask-RESTful 中,我们添加如下 api 路由
api.add_resource(CuteKitty,'/api/kitty')
class CuteKitty(Resource):
def get(self): return {}
def post(self): return {}
def put(self): return {}
def delete(self): return None, 204
这样GET /api/kitty
--> 到CuteKitty.get()
方法;对所有 HTTP 动词都这样
假设我需要为我的 api 消费者提供一个可爱的 api,例如
POST /api/kitty/drink/milk ---> CuteKitty.drink(what="milk")
POST /api/kitty/meow ---> CuteKitty.meow()
我怎样才能实现上述路由api.add_resource
class CuteKitty(Resource):
def get(self): return {}
def post(self): return {}
def put(self): return {}
def delete(self): return None, 204
def drink(self,what="milk"): return {}
def meow(self): return {}
明智地如何添加路线,例如/api/kitty/<int:kitty_id>/habits
-->CuteKitty.habits(kitty_id)