Refactored JsonPayload to JsonResponse
This commit is contained in:
parent
d13341dd5b
commit
6581385ab9
16 changed files with 37 additions and 43 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from openflexure_microscope.plugins import MicroscopePlugin
|
||||
from openflexure_microscope.api.v1.views import MicroscopeViewPlugin
|
||||
from openflexure_microscope.api.utilities import JsonPayload
|
||||
from openflexure_microscope.api.utilities import JsonResponse
|
||||
|
||||
import os
|
||||
import time
|
||||
|
|
@ -115,7 +115,7 @@ class HelloWorldAPI(MicroscopeViewPlugin):
|
|||
Assumes request will include a JSON payload.
|
||||
"""
|
||||
# Get payload JSON
|
||||
payload = JsonPayload(request)
|
||||
payload = JsonResponse(request)
|
||||
|
||||
# Extract a value from the JSON key 'plugin_string', and convert to a string. If no value is given, default to empty.
|
||||
new_plugin_string = payload.param('plugin_string', default='', convert=str)
|
||||
|
|
@ -134,7 +134,7 @@ class TimelapseAPI(MicroscopeViewPlugin):
|
|||
def post(self):
|
||||
|
||||
# Get any JSON data in the body of the POST request
|
||||
payload = JsonPayload(request)
|
||||
payload = JsonResponse(request)
|
||||
|
||||
# Extract the "n_images" parameter if it was passed. Otherwise, default to 10.
|
||||
n_images = payload.param('n_images', default=10, convert=int)
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ For example, a timelapse plugin may look like:
|
|||
from openflexure_microscope.plugins import MicroscopePlugin
|
||||
from openflexure_microscope.exceptions import TaskDeniedException
|
||||
from openflexure_microscope.api.v1.views import MicroscopeViewPlugin
|
||||
from openflexure_microscope.api.utilities import JsonPayload
|
||||
from openflexure_microscope.api.utilities import JsonResponse
|
||||
|
||||
|
||||
### MICROSCOPE PLUGIN ###
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ An example web route with simple responses may look like:
|
|||
|
||||
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.
|
||||
To ease obtaining values from a JSON payload attached to an HTTP POST request, you can use the :py:class:`openflexure_microscope.api.utilities.JsonResponse` 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
|
||||
|
||||
|
|
@ -139,7 +139,7 @@ To ease obtaining values from a JSON payload attached to an HTTP POST request, y
|
|||
|
||||
def post(self):
|
||||
# Get payload JSON from request
|
||||
payload = JsonPayload(request)
|
||||
payload = JsonResponse(request)
|
||||
|
||||
# Try to find value associated with 'my_string' key.
|
||||
# If that key doesn't exist in the payload, return '' instead.
|
||||
|
|
@ -147,5 +147,5 @@ To ease obtaining values from a JSON payload attached to an HTTP POST request, y
|
|||
new_plugin_string = payload.param('my_string', default='', convert=str)
|
||||
...
|
||||
|
||||
.. autoclass:: openflexure_microscope.api.utilities.JsonPayload
|
||||
.. autoclass:: openflexure_microscope.api.utilities.JsonResponse
|
||||
:members:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue