diff --git a/openflexure_microscope/api/default_extensions/zip_builder.py b/openflexure_microscope/api/default_extensions/zip_builder.py index 42470901..5db25a70 100644 --- a/openflexure_microscope/api/default_extensions/zip_builder.py +++ b/openflexure_microscope/api/default_extensions/zip_builder.py @@ -11,8 +11,6 @@ from labthings.schema import Schema, pre_dump from labthings.utilities import description_from_view from labthings.views import ActionView, PropertyView, View -from openflexure_microscope.devel import JsonResponse, request - class ZipObjectSchema(Schema): id = fields.String() @@ -128,12 +126,15 @@ default_zip_manager = ZipManager() class ZipBuilderAPIView(ActionView): - def post(self): - ids = list(JsonResponse(request).json) + args = fields.List(fields.String(), required=True) + + def post(self, args): microscope = find_component("org.openflexure.microscope") # Return a handle on the autofocus task - return default_zip_manager.marshaled_build_zip_from_capture_ids(microscope, ids) + return default_zip_manager.marshaled_build_zip_from_capture_ids( + microscope, args + ) class ZipListAPIView(PropertyView): diff --git a/openflexure_microscope/api/utilities/__init__.py b/openflexure_microscope/api/utilities/__init__.py index 6e01e9dc..8f72a948 100644 --- a/openflexure_microscope/api/utilities/__init__.py +++ b/openflexure_microscope/api/utilities/__init__.py @@ -24,54 +24,6 @@ def blueprint_name_for_module(module_name, api_ver=2, suffix=""): return f"v{api_ver}_{bp_name}_blueprint{suffix}" -class JsonResponse: - def __init__(self, request): - """ - Object to wrap up simple functionality for parsing a JSON response. - """ - # Try to load as json - try: - self.json = ( - request.get_json() - ) #: dict: Dictionary representation of request JSON - # If malformed JSON is passed, make an empty dictionary - except BadRequest as e: - logging.error(e) - self.json = {} - - if self.json is None: - self.json = {} - - # Store raw response data - self.data = request.get_data() #: str: String representation of request data - - if self.data is None: - self.data = "" - - def param(self, key, default=None, convert=None): - """ - Check if a key exists in a JSON/dictionary payload, and returns it. - - Args: - key (str): JSON key to look for - default: Value to return if no matching key-value pair is found. - convert: Converter function. By passing a type, the value will be converted to that type. - **Use with caution!** - """ - # If no JSON payload exists, make an empty dictionary - if not self.json: - self.json = {} - - if key in self.json: - val = self.json[key] - else: - val = default - - if convert and (val is not None): - val = convert(val) - return val - - def gen(camera): """Video streaming generator function.""" while True: diff --git a/openflexure_microscope/api/v2/views/captures.py b/openflexure_microscope/api/v2/views/captures.py index 9272aafb..f9035498 100644 --- a/openflexure_microscope/api/v2/views/captures.py +++ b/openflexure_microscope/api/v2/views/captures.py @@ -2,12 +2,12 @@ import logging from flask import abort, redirect, request, send_file, url_for from labthings import Schema, fields, find_component -from labthings.marshalling import marshal_with +from labthings.marshalling import marshal_with, use_args from labthings.utilities import description_from_view from labthings.views import PropertyView, View from marshmallow import pre_dump -from openflexure_microscope.api.utilities import JsonResponse, get_bool +from openflexure_microscope.api.utilities import get_bool class InstrumentSchema(Schema): @@ -203,7 +203,8 @@ class CaptureTags(View): return capture_obj.tags - def put(self, id_): + @use_args(fields.List(fields.String(), required=True)) + def put(self, args, id_): """ Add tags to a single image capture """ @@ -213,17 +214,12 @@ class CaptureTags(View): if not capture_obj: return abort(404) # 404 Not Found - # TODO: Replace with normal Flask request JSON thing - data_dict = JsonResponse(request).json - - if type(data_dict) != list: - return abort(400) - - capture_obj.put_tags(data_dict) + capture_obj.put_tags(args) return capture_obj.tags - def delete(self, id_): + @use_args(fields.List(fields.String(), required=True)) + def delete(self, args, id_): """ Delete tags from a single image capture """ @@ -233,12 +229,7 @@ class CaptureTags(View): if not capture_obj: return abort(404) # 404 Not Found - data_dict = JsonResponse(request).json - - if type(data_dict) != list: - return abort(400) - - for tag in data_dict: + for tag in args: capture_obj.delete_tag(str(tag)) return capture_obj.tags @@ -259,7 +250,8 @@ class CaptureAnnotations(View): return capture_obj.annotations - def put(self, id_): + @use_args(fields.Dict()) + def put(self, args, id_): """ Update metadata for a single image capture """ @@ -269,12 +261,6 @@ class CaptureAnnotations(View): if not capture_obj: return abort(404) # 404 Not Found - data_dict = JsonResponse(request).json - logging.debug(data_dict) - - if type(data_dict) != dict: - return abort(400) - - capture_obj.put_annotations(data_dict) + capture_obj.put_annotations(args) return capture_obj.annotations diff --git a/openflexure_microscope/api/v2/views/instrument.py b/openflexure_microscope/api/v2/views/instrument.py index 000bfb6f..3056f800 100644 --- a/openflexure_microscope/api/v2/views/instrument.py +++ b/openflexure_microscope/api/v2/views/instrument.py @@ -1,12 +1,11 @@ import logging from flask import abort, request -from labthings import find_component +from labthings import find_component, fields +from labthings.marshalling import use_args from labthings.utilities import create_from_path, get_by_path, set_by_path from labthings.views import PropertyView, View -from openflexure_microscope.api.utilities import JsonResponse - class SettingsProperty(PropertyView): def get(self): @@ -16,17 +15,17 @@ class SettingsProperty(PropertyView): microscope = find_component("org.openflexure.microscope") return microscope.read_settings() - def put(self): + @use_args(fields.Dict()) + def put(self, args): """ Update current microscope settings, including camera and stage """ microscope = find_component("org.openflexure.microscope") - payload = JsonResponse(request) logging.debug("Updating settings from PUT request:") - logging.debug(payload.json) + logging.debug(args) - microscope.update_settings(payload.json) + microscope.update_settings(args) microscope.save_settings() return self.get() @@ -50,16 +49,16 @@ class NestedSettingsProperty(View): return value - def put(self, route): + @use_args(fields.Dict()) + def put(self, args, route): """ Update a nested section of the current microscope settings """ microscope = find_component("org.openflexure.microscope") keys = route.split("/") - payload = JsonResponse(request) dictionary = create_from_path(keys) - set_by_path(dictionary, keys, payload.json) + set_by_path(dictionary, keys, args) microscope.update_settings(dictionary) microscope.save_settings() diff --git a/openflexure_microscope/devel/__init__.py b/openflexure_microscope/devel/__init__.py index a8e5d39a..fe8d387b 100644 --- a/openflexure_microscope/devel/__init__.py +++ b/openflexure_microscope/devel/__init__.py @@ -13,8 +13,6 @@ from labthings import current_action as current_task from labthings import update_action_data as update_task_data from labthings import update_action_progress as update_task_progress -from openflexure_microscope.api.utilities import JsonResponse - __all__ = [ "current_task", "update_task_progress",