Chances are, if you intend to publicly expose your API, that you want to supply documentation. Writing documentation is a tedious process, and even more so if you change things in your code.
Luckily, Piston can do a lot of the heavy lifting for you here as well.
In piston.doc there is a set of methods, allowing you to easily generate documentation using standard Django views and templates.
The function generate_doc returns a HandlerDocumentation instance, which has a few methods:
get_methods yields a set of HandlerMethod’s which are more interesting:
For example:
from piston.handler import BaseHandler
from piston.doc import generate_doc
class BlogpostHandler(BaseHandler):
model = Blogpost
def read(self, request, post_slug=None):
"""
Reads all blogposts, or a specific blogpost if
`post_slug` is supplied.
"""
pass
@staticmethod
def resource_uri():
return ('api_blogpost_handler', ['id'])
doc = generate_doc(BlogpostHandler)
print doc.name # -> 'BlogpostHandler'
print doc.model # -> <class 'Blogpost'>
print doc.resource_uri_template # -> '/api/post/{id}'
methods = doc.get_methods()
for method in methods:
print method.name # -> 'read'
print method.signature # -> 'read(post_slug=<optional>)'
sig = ''
for argn, argdef in method.iter_args():
sig += argn
if argdef:
sig += "=%s" % argdef
sig += ', '
sig = sig.rstrip(",")
print sig # -> 'read(repo_slug=None)'
Each resource can have an URI. They can be accessed in the Handler via his .resource_uri() method.
See also