Document your API

Web services without a proper documentation are usually useless.

To make it easy to document your own API, WSME provides a Sphinx extension.

Install the extension

Here we consider that you already quick-started a sphinx project.

  1. In your conf.py file, add 'ext' to your extensions, and optionally set the enabled protocols.

    extensions = ['ext']
    
    wsme_protocols = ['restjson', 'restxml']
    
  2. Copy toggle.js and toggle.css in your _static directory.

The wsme domain

The extension will add a new Sphinx domain providing a few directives.

Config values

wsme_protocols

A list of strings that are WSME protocol names.

The types and services generated documentation will include code samples for each of these protocols.

wsme_root

A string that is the full name of the service root controller. It will be used to determinate the relative path of the other controllers when they are autodocumented, and calculate the complete webpath of the other controllers.

wsme_webpath

A string that is the webpath where the wsme_root is mounted.

Directives

.. root:: <WSRoot full path>

Define the service root controller for this documentation source file. To set it globally, see wsme_root.

A webpath option allows override of wsme_webpath.

Example:

.. wsme:root:: myapp.controllers.MyWSRoot
    :webpath: /api
.. service:: name/space/ServiceName

Declare a service.

.. type:: MyComplexType

Equivalent to the py:class directive to document a complex type

.. attribute:: aname

Equivalent to the py:attribute directive to document a complex type attribute. It takes an additional :type: field.

Example

Source

Result

.. wsme:root:: wsmeext.sphinxext.SampleService
    :webpath: /api

.. wsme:type:: MyType

    .. wsme:attribute:: test

        :type: int

.. wsme:service:: name/space/SampleService

    .. wsme:function:: doit
type MyType
test
Type:

int

service name/space/SampleService
getType()

Returns a MyType

Autodoc directives

Theses directives scan your code to generate the documentation from the docstrings and your API types and controllers.

.. autotype:: myapp.MyType

Generate the myapp.MyType documentation.

.. autoattribute:: myapp.MyType.aname

Generate the myapp.MyType.aname documentation.

.. autoservice:: myapp.MyService

Generate the myapp.MyService documentation.

.. autofunction:: myapp.MyService.myfunction

Generate the myapp.MyService.myfunction documentation.

Full Example

Python source

    """A Sample Type"""

    #: A Int
    aint = int

    def __init__(self, aint=None):
        if aint:
            self.aint = aint

    @classmethod
    def sample(cls):
        return cls(10)


class SampleService(wsme.WSRoot):
    @wsme.expose(SampleType)
    @wsme.validate(SampleType, int, str)
    def change_aint(data, aint, dummy='useless'):
        """
        :param aint: The new value

        :return: The data object with its aint field value changed.
        """
        data.aint = aint
        return data


def getroot(env, force=False):

Documentation source

.. default-domain:: wsme

.. type:: int

    An integer

.. autotype:: wsmeext.sphinxext.SampleType
    :members:

.. autoservice:: wsmeext.sphinxext.SampleService
    :members:

Result

type int

An integer

type SampleType

A Sample Type

Data samples:

Json
{
    "aint": 10
}
XML
b'<value>\n  <aint>10</aint>\n</value>'
aint
Type:

int

A Int

service /
SampleService.change_aint(aint, dummy='useless')[source]
Parameters:

aint – The new value

Returns:

The data object with its aint field value changed.