Commit 8afb0757 authored by jtc42's avatar jtc42
Browse files

Better handle response documentation

parent 1ccdad8a
Loading
Loading
Loading
Loading
+3 −7
Original line number Diff line number Diff line
@@ -10,18 +10,14 @@ from openflexure_microscope.common.flask_labthings.find import (
    find_extension,
)
from openflexure_microscope.common.flask_labthings.extensions import BaseExtension
from openflexure_microscope.common.flask_labthings.views.tasks import TaskSchema
from openflexure_microscope.common.flask_labthings.decorators import (
    marshal_task,
    marshal_with,
    use_args,
)
from openflexure_microscope.common.flask_labthings import fields

from openflexure_microscope.devel import (
    taskify,
    abort,
    update_task_progress,
)
from openflexure_microscope.devel import taskify, abort, update_task_progress

from openflexure_microscope.common.flask_labthings.resource import Resource
import time
@@ -361,7 +357,7 @@ class TileScanAPI(Resource):
            "resize": fields.Dict(missing=None),  # TODO: Validate keys
        }
    )
    @marshal_with(TaskSchema())
    @marshal_task
    def post(self, args):
        microscope = find_device("openflexure_microscope")

+5 −1
Original line number Diff line number Diff line
@@ -2,7 +2,11 @@ import logging
import os
import errno
from werkzeug.exceptions import BadRequest
from flask import url_for, Blueprint
from flask import url_for, Blueprint, current_app


def view_class_from_endpoint(endpoint: str):
    return current_app.view_functions[endpoint].view_class


def blueprint_for_module(module_name, api_ver=2, suffix=""):
+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ class CaptureAPI(Resource):
        }
    )
    @marshal_with(capture_schema)
    @doc_response(200, "Capture successful")
    @doc_response(200, description="Capture successful")
    def post(self, args):
        """
        Create a new capture
+27 −5
Original line number Diff line number Diff line
from webargs import flaskparser
from functools import wraps, update_wrapper
from flask import make_response
from http import HTTPStatus

from openflexure_microscope.common.labthings_core.utilities import rupdate

from .spec import update_spec
from .schema import TaskSchema


def unpack(value):
@@ -28,23 +30,23 @@ def unpack(value):


class marshal_with(object):
    def __init__(self, schema):
    def __init__(self, schema, code=200):
        """
        :param schema: a dict of whose keys will make up the final
                        serialized response output
        """
        self.schema = schema
        self.code = code

    def __call__(self, f):
        # Pass params to call function attribute for external access
        update_spec(f, {"_schema": self.schema})
        update_spec(f, {"_schema": {self.code: self.schema}})
        # Wrapper function
        @wraps(f)
        def wrapper(*args, **kwargs):
            resp = f(*args, **kwargs)
            if isinstance(resp, tuple):
                data, code, headers = unpack(resp)
                print((data, code, headers))
                return make_response(self.schema.jsonify(data), code, headers)
            else:
                return make_response(self.schema.jsonify(resp))
@@ -52,6 +54,23 @@ class marshal_with(object):
        return wrapper


def marshal_task(f):
    # Pass params to call function attribute for external access
    update_spec(f, {"responses": {201: {"description": "Task started successfully"}}})
    update_spec(f, {"_schema": {201: TaskSchema()}})
    # Wrapper function
    @wraps(f)
    def wrapper(*args, **kwargs):
        resp = f(*args, **kwargs)
        if isinstance(resp, tuple):
            data, code, headers = unpack(resp)
            return make_response(TaskSchema().jsonify(data), code, headers)
        else:
            return make_response(TaskSchema().jsonify(resp))

    return wrapper


class use_args(object):
    def __init__(self, schema, **kwargs):
        """
@@ -88,7 +107,7 @@ class doc(object):


class doc_response(object):
    def __init__(self, code, description, **kwargs):
    def __init__(self, code, description=None, **kwargs):
        self.code = code
        self.description = description
        self.kwargs = kwargs
@@ -99,7 +118,10 @@ class doc_response(object):
            f,
            {
                "responses": {
                    self.code: {"description": self.description, **self.kwargs}
                    self.code: {
                        "description": self.description or HTTPStatus(self.code).phrase,
                        **self.kwargs,
                    }
                }
            },
        )
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ from openflexure_microscope.common.labthings_core.utilities import (
    snake_to_spine,
)


class BaseExtension:
    """
    Parent class for all extensions.
Loading