Commit 6581385a authored by Joel Collins's avatar Joel Collins
Browse files

Refactored JsonPayload to JsonResponse

parent d13341dd
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
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)
+1 −1
Original line number Diff line number Diff line
@@ -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 ###
+3 −3
Original line number Diff line number Diff line
@@ -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:
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ from werkzeug.exceptions import BadRequest
from flask import url_for


class JsonPayload:
class JsonResponse:
    def __init__(self, request):
        """
        Object to wrap up simple functionality for parsing a JSON response.
+2 −2
Original line number Diff line number Diff line
from openflexure_microscope.api.utilities import gen, JsonPayload
from openflexure_microscope.api.utilities import gen, JsonResponse
from openflexure_microscope.api.v1.views import MicroscopeView

from flask import Response, Blueprint, jsonify, request
@@ -203,7 +203,7 @@ class ConfigAPI(MicroscopeView):
        :status 200: capture created

        """
        payload = JsonPayload(request)
        payload = JsonResponse(request)

        logging.debug("Updating settings from POST request:")
        logging.debug(payload.json)
Loading