Removed old JSONResponse class

This commit is contained in:
Joel Collins 2020-11-12 13:50:48 +00:00
parent 7c450b6214
commit b350f62013
5 changed files with 26 additions and 90 deletions

View file

@ -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):

View file

@ -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:

View file

@ -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

View file

@ -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()

View file

@ -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",