@@ -93,6 +93,28 @@ For example, your ``api_views`` dictionary may look like:
Here, ``MyRouteAPI`` is a web API plugin class, subclassing :py:class:`openflexure_microscope.api.v1.views.MicroscopeViewPlugin`. If your plugin package were named ``mypluginpackage``, an API route would be automatically added at ``/api/v1/plugin/mypluginpackage/myplugin``.
Parsing JSON from HTTP POST requests
++++++++++++++++++++++++++++++++++++
To ease obtaining values from a JSON payload attached to an HTTP POST request, you can use the :py:class:`openflexure_microscope.api.utilities.JsonPayload` class. This allows parameters to be extracted by their key, with a default value supplied to avoid errors associated with nonexistent keys. Additionally, a converter function may be passed, used in the following examples to convert the type of a value. In principle, however, you can pass any single-argument function and it will apply that function to the value, if it exists.
.. code-block:: python
...
class MyRouteAPI(MicroscopeViewPlugin):
def post(self):
# Get payload JSON from request
payload = JsonPayload(request)
# Try to find value associated with 'my_string' key.
# If that key doesn't exist in the payload, return '' instead.
# If a value does exist, convert it to a string, regardless of its original type.