如果我有这个代码:
class MyApp():
def __init__(self):
self.bottle = Bottle()
self.bottle.route('/')(self.show_api)
self.bottle.route('/api/')(self.show_api)
self.bottle.route('/api/item', method='PUT')(self.save_item)
def show_api(self):
return <JSON representation of the API?>
是否可以从中获取 JSON 格式的 REST API 文档? 由于某种原因 self.bottle.routes 没有返回任何有用的内容。
谢谢!
最佳答案
实际上生成 JSON API 描述的正确方法似乎是:
from collections import defaultdict
import json
def show_api(self):
api_dict = defaultdict(dict)
for route in self.bottle.routes:
api_dict[route.rule]['url'] = 'http://myhost:port{}'.format(route.rule)
api_dict[route.rule]['method'] = route.method
# additional config params
for key in route.config:
api_dict[route.rule][key] = route.config[key]
return json.dumps(api_dict)
关于python - 有没有办法从 Bottle 路由自动生成 REST API JSON 描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27412784/