RESTful web service API documentation with Sphinx

10,748

Solution 1

The Sphinx Contrib project also seems to have an HTTP Domain package for documenting RESTful HTTP APIs. You can find its documentation on the Python packages site. I can't speak to its fitness: I'm only just starting to look into Sphinx, and I have a need to document RESTful APIs as well, and noticed this contributed package.

Solution 2

Since there doesn't appear to be any existing solution, I have implemented a very simple HTTP domain that I can use to mark up API methods:

import re

from docutils import nodes

from sphinx import addnodes
from sphinx.locale import l_, _
from sphinx.domains import Domain, ObjType
from sphinx.directives import ObjectDescription


http_method_sig_re = re.compile(r'^(GET|POST|PUT|DELETE)?\s?(\S+)(.*)$')

class HTTPMethod(ObjectDescription):

    def handle_signature(self, sig, signode):
        m = http_method_sig_re.match(sig)
        if m is None:
            raise ValueError

        verb, url, query = m.groups()
        if verb is None:
            verb = 'GET'

        signode += addnodes.desc_addname(verb, verb)
        signode += addnodes.desc_name(url, url)

        if query:
            params = query.strip().split()
            for param in params:
                signode += addnodes.desc_optional(param, param)

        return url


class HTTPDomain(Domain):
    """HTTP language domain."""
    name = 'http'
    label = 'HTTP'
    object_types = {
        'method':    ObjType(l_('method'),    'method')
    }
    directives = {
        'method':       HTTPMethod
    }

def setup(app):
    app.add_domain(HTTPDomain)

It allows me to mark up methods like this and they'll be styled somewhat visually nicely:

.. http:method:: GET /api/foo/bar/:id/:slug

   :param id: An id
   :param slug: A slug

   Retrieve list of foobars matching given id.

This was my first foray into both Sphinx and Python, so this should be considered very rudimentary code. If anybody is interested in fleshing this out, please fork this project on Github!

Share:
10,748
Gromski
Author by

Gromski

More or less independent web dude washed ashore in Japan. More or less recently more less than more. More recently also less Japan and more Europe. Full-stack developer, most heavily focussed on web development. Has a background in system administration, so "full-stack" pretty much starts at putting CPUs on motherboards, via installing and configuring the OS, writing the backend and frontend, to designing the interface. Bread and butter is PHP, current favourite stack is Python, AngularJS and Polymer; has long standing love affair with Haskell, Erlang and others. Likes tweaking nginx configuration files for some reason. Is writing articles about topics that keep popping up regularly on Stackoverflow but seldom get the treatment they require, like: What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text The Great Escapism (Or: What You Need To Know To Work With Text Within Text) The Definitive Guide To PHP's isset And empty Handling Unicode Front To Back In A Web App How Not To Kill Your Testability Using Statics

Updated on July 11, 2022

Comments

  • Gromski
    Gromski almost 2 years

    What's the best way to markup methods/URLs for a RESTful webservice using ReST/Sphinx? Is there a default domain that's suitable for marking up URLs with their possible parameters, HTTP methods, headers and body content?

    Something along the lines of:

    .. rest:method:: GET /api/foo
    
       :param bar: A valid bar
       :extension: json or xml
    
       Retrieve foos for the given parameters. E.g.::
    
          GET /api/foo.json?bar=baz
    

    Does something like this already exist or is one of the default extensions usable, or will I have to write one myself?