Web services without a proper documentation are usually useless.
To make it easy to document your own API, WSME provides a Sphinx extension.
Here we consider that you already quick-started a sphinx project.
In your conf.py file, add 'ext' to you extensions, and optionally set the enabled protocols.
extensions = ['ext']
wsme_protocols = ['restjson', 'restxml', 'extdirect']
Copy toggle.js and toggle.css in your _static directory.
The extension will add a new Sphinx domain providing a few directives.
A list of strings that are WSME protocol names. If provided by an additionnal package (for example WSME-Soap or WSME-ExtDirect), it must be installed.
The types and services generated documentation will include code samples for each of these protocols.
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.
Allow to define the service root controller in one documentation source file. To set it globally, see wsme_root.
A webpath option allow to override wsme_webpath.
Example:
.. wsme:root:: myapp.controllers.MyWSRoot
:webpath: /api
Declare a service.
Equivalent to the py:class directive to document a complex type
Equivalent to the py:attribute directive to document a complex type attribute. It takes an additionnal :type: field.
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
|
Theses directives scan your code to generate the documentation from the docstrings and your API types and controllers.
Generate the myapp.MyType documentation.
Generate the myapp.MyType.aname documentation.
Generate the myapp.MyService documentation.
Generate the myapp.MyService.myfunction documentation.
return six.b('samplestring')
if datatype is wsme.types.text:
return u'sample unicode'
if datatype is int:
return 5
sample_obj = getattr(datatype, 'sample', datatype)()
return sample_obj
def get_protocols(names):
names = list(names)
protocols = []
if 'rest' in names:
names.remove('rest')
protocols.extend('restjson', 'restxml')
if 'restjson' in names:
names.remove('restjson')
protocols.append(('Json', wsme.rest.json))
if 'restxml' in names:
names.remove('restxml')
protocols.append(('XML', wsme.rest.xml))
for name in names:
p = wsme.protocol.getprotocol(name)
protocols.append((p.displayname or p.name, p))
return protocols
.. default-domain:: wsmeext
.. autotype:: wsmeext.sphinxext.SampleType
:members:
.. autoservice:: wsmeext.sphinxext.SampleService
:members:
A Sample Type
Data samples:
{
"aint": 10
}
<value>
<aint>10</aint>
</value>
<Element 'value' at 0x3b756c0>
{
"aint": 10
}
Type: | int |
---|
A Int
Parameters samples: | |
---|---|
|
|
Return samples: |
|
Parameters: |
|
Return type: | SampleType |
Returns: | The data object with its aint field value changed. |