In case of I'm having this code:
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?>
Is it possible to get a REST API documentation in JSON format from that?
fro some reason self.bottle.routes didn't return anything useful.
Thanks!
Actually the right way to generate the JSON API description seems to be:
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)
Bottle.route()
is meant to be used as a decorator:
@app.route('/hello/:name')
def hello(name):
return 'Hello %s' % name
So it does return something useful: the decorated function.
You can then use app.routes
to get a list of declared routes.
print(app.routes)
Output:
[<GET '/hello/:name' <function hello at 0x7f4137192e18>>]
A function is not directly JSON serializable. But you could easily use str
to get a string represenation.