From 6581385ab911c009b1f0ed9d43067f83ab7a7691 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Fri, 13 Sep 2019 18:04:33 +0100 Subject: [PATCH] Refactored JsonPayload to JsonResponse --- docs/source/plugins/example/plugin.py | 6 +++--- docs/source/plugins/hardware.rst | 2 +- docs/source/plugins/routes.rst | 6 +++--- openflexure_microscope/api/utilities.py | 2 +- openflexure_microscope/api/v1/blueprints/base.py | 4 ++-- .../api/v1/blueprints/camera/capture.py | 10 +++++----- .../api/v1/blueprints/camera/function.py | 6 +++--- .../api/v1/blueprints/camera/preview.py | 4 ++-- openflexure_microscope/api/v1/blueprints/stage.py | 4 ++-- openflexure_microscope/config.py | 6 ------ openflexure_microscope/devel/__init__.py | 2 +- .../plugins/default/autofocus/api.py | 8 ++++---- .../plugins/default/camera_calibration/plugin.py | 2 +- openflexure_microscope/plugins/default/scan/api.py | 4 ++-- openflexure_microscope/plugins/example/api.py | 8 ++++---- .../plugins/testing/schema_example/api.py | 6 +++--- 16 files changed, 37 insertions(+), 43 deletions(-) diff --git a/docs/source/plugins/example/plugin.py b/docs/source/plugins/example/plugin.py index 4a3baea5..a0570992 100644 --- a/docs/source/plugins/example/plugin.py +++ b/docs/source/plugins/example/plugin.py @@ -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) diff --git a/docs/source/plugins/hardware.rst b/docs/source/plugins/hardware.rst index 8c622064..cf46addf 100644 --- a/docs/source/plugins/hardware.rst +++ b/docs/source/plugins/hardware.rst @@ -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 ### diff --git a/docs/source/plugins/routes.rst b/docs/source/plugins/routes.rst index a7c2618b..4d6b27e8 100644 --- a/docs/source/plugins/routes.rst +++ b/docs/source/plugins/routes.rst @@ -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: diff --git a/openflexure_microscope/api/utilities.py b/openflexure_microscope/api/utilities.py index cdeca9d0..e014f171 100644 --- a/openflexure_microscope/api/utilities.py +++ b/openflexure_microscope/api/utilities.py @@ -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. diff --git a/openflexure_microscope/api/v1/blueprints/base.py b/openflexure_microscope/api/v1/blueprints/base.py index 3ef923ca..74e1cc32 100644 --- a/openflexure_microscope/api/v1/blueprints/base.py +++ b/openflexure_microscope/api/v1/blueprints/base.py @@ -1,4 +1,4 @@ -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) diff --git a/openflexure_microscope/api/v1/blueprints/camera/capture.py b/openflexure_microscope/api/v1/blueprints/camera/capture.py index 3af8ee09..51bd6b27 100644 --- a/openflexure_microscope/api/v1/blueprints/camera/capture.py +++ b/openflexure_microscope/api/v1/blueprints/camera/capture.py @@ -1,4 +1,4 @@ -from openflexure_microscope.api.utilities import get_bool, JsonPayload +from openflexure_microscope.api.utilities import get_bool, JsonResponse from openflexure_microscope.api.v1.views import MicroscopeView from openflexure_microscope.utilities import filter_dict @@ -98,7 +98,7 @@ class ListAPI(MicroscopeView): :
1.0.1? -if (not os.path.isfile(USER_CONFIG_FILE)) and (os.path.isfile(USER_CONFIG_FILE_OLD)): - os.rename(USER_CONFIG_FILE_OLD, USER_CONFIG_FILE) - - # HANDLE CUSTOM YAML PARSING def construct_yaml_bool(self, node): """ diff --git a/openflexure_microscope/devel/__init__.py b/openflexure_microscope/devel/__init__.py index df94377a..3e5f1c92 100644 --- a/openflexure_microscope/devel/__init__.py +++ b/openflexure_microscope/devel/__init__.py @@ -8,7 +8,7 @@ as well as some Flask imports to simplify API route development # Plugin classes 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 # Exceptions from openflexure_microscope.exceptions import TaskDeniedException diff --git a/openflexure_microscope/plugins/default/autofocus/api.py b/openflexure_microscope/plugins/default/autofocus/api.py index 5f598449..cefd53fb 100644 --- a/openflexure_microscope/plugins/default/autofocus/api.py +++ b/openflexure_microscope/plugins/default/autofocus/api.py @@ -1,17 +1,17 @@ import numpy as np import logging -from openflexure_microscope.devel import MicroscopeViewPlugin, JsonPayload, request, jsonify +from openflexure_microscope.devel import MicroscopeViewPlugin, JsonResponse, request, jsonify class MeasureSharpnessAPI(MicroscopeViewPlugin): def post(self): - payload = JsonPayload(request) + payload = JsonResponse(request) return jsonify({'sharpness': self.plugin.measure_sharpness()}) class AutofocusAPI(MicroscopeViewPlugin): def post(self): - payload = JsonPayload(request) + payload = JsonResponse(request) # Figure out the range of z values to use dz = payload.param("dz", default=np.linspace(-300, 300, 7), convert=np.array) @@ -24,7 +24,7 @@ class AutofocusAPI(MicroscopeViewPlugin): class FastAutofocusAPI(MicroscopeViewPlugin): def post(self): - payload = JsonPayload(request) + payload = JsonResponse(request) # Figure out the parameters to use dz = payload.param("dz", default=2000, convert=int) diff --git a/openflexure_microscope/plugins/default/camera_calibration/plugin.py b/openflexure_microscope/plugins/default/camera_calibration/plugin.py index e57a4a47..c8c45023 100644 --- a/openflexure_microscope/plugins/default/camera_calibration/plugin.py +++ b/openflexure_microscope/plugins/default/camera_calibration/plugin.py @@ -1,4 +1,4 @@ -from openflexure_microscope.devel import MicroscopePlugin, MicroscopeViewPlugin, JsonPayload, request, jsonify +from openflexure_microscope.devel import MicroscopePlugin, MicroscopeViewPlugin, JsonResponse, request, jsonify import logging diff --git a/openflexure_microscope/plugins/default/scan/api.py b/openflexure_microscope/plugins/default/scan/api.py index 9e8981e5..71ff8681 100644 --- a/openflexure_microscope/plugins/default/scan/api.py +++ b/openflexure_microscope/plugins/default/scan/api.py @@ -1,10 +1,10 @@ -from openflexure_microscope.devel import MicroscopeViewPlugin, JsonPayload, request, jsonify, abort +from openflexure_microscope.devel import MicroscopeViewPlugin, JsonResponse, request, jsonify, abort import logging class TileScanAPI(MicroscopeViewPlugin): def post(self): - payload = JsonPayload(request) + payload = JsonResponse(request) # Get params filename = payload.param('filename') diff --git a/openflexure_microscope/plugins/example/api.py b/openflexure_microscope/plugins/example/api.py index 0afe6dd2..f8d65266 100644 --- a/openflexure_microscope/plugins/example/api.py +++ b/openflexure_microscope/plugins/example/api.py @@ -1,4 +1,4 @@ -from openflexure_microscope.api.utilities import JsonPayload +from openflexure_microscope.api.utilities import JsonResponse from openflexure_microscope.api.v1.views import MicroscopeViewPlugin from openflexure_microscope.exceptions import TaskDeniedException @@ -40,7 +40,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) @@ -61,7 +61,7 @@ class LongRunningAPI(MicroscopeViewPlugin): Method to call when an HTTP POST request is made. """ # Get payload JSON - payload = JsonPayload(request) + payload = JsonResponse(request) # Extract a values from the JSON payload. time_to_run = payload.param('time', default=10, convert=int) @@ -84,7 +84,7 @@ class SomeExceptionAPI(MicroscopeViewPlugin): Method to call when an HTTP POST request is made. """ # Get payload JSON - payload = JsonPayload(request) + payload = JsonResponse(request) # Attach the long-running method as a microscope task try: diff --git a/openflexure_microscope/plugins/testing/schema_example/api.py b/openflexure_microscope/plugins/testing/schema_example/api.py index 04cefe66..302c4757 100644 --- a/openflexure_microscope/plugins/testing/schema_example/api.py +++ b/openflexure_microscope/plugins/testing/schema_example/api.py @@ -1,4 +1,4 @@ -from openflexure_microscope.devel import MicroscopeViewPlugin, TaskDeniedException, JsonPayload, Response, request, escape, jsonify +from openflexure_microscope.devel import MicroscopeViewPlugin, TaskDeniedException, JsonResponse, Response, request, escape, jsonify import logging @@ -12,7 +12,7 @@ class DoAPI(MicroscopeViewPlugin): def post(self): # Get payload JSON - payload = JsonPayload(request) + payload = JsonResponse(request) # Extract a values from the JSON payload. val_int = payload.param('val_int', default=None, convert=int) @@ -49,7 +49,7 @@ class TaskAPI(MicroscopeViewPlugin): def post(self): # Get payload JSON - payload = JsonPayload(request) + payload = JsonResponse(request) # Extract a values from the JSON payload. val_int = payload.param('run_time', default=5, convert=int)