Commit b3f32f23 authored by jtc42's avatar jtc42
Browse files

Added decorator for documenting response codes

parent 3bcfd86c
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
from openflexure_microscope.api.utilities import get_bool, JsonResponse
from openflexure_microscope.common.flask_labthings.resource import Resource
from openflexure_microscope.common.flask_labthings.find import find_device
from openflexure_microscope.common.flask_labthings.decorators import use_args, marshal_with, doc
from openflexure_microscope.common.flask_labthings.decorators import use_args, marshal_with, doc, response
from openflexure_microscope.common.flask_labthings import fields
from openflexure_microscope.utilities import filter_dict

@@ -28,6 +28,7 @@ class CaptureAPI(Resource):
        }
    )
    @marshal_with(capture_schema)
    @response(200, "Capture successful")
    def post(self, args):
        microscope = find_device("openflexure_microscope")

+22 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ from webargs import flaskparser
from functools import wraps, update_wrapper
from flask import make_response

from .utilities import rupdate

def unpack(value):
    """Return a three tuple of data, code, and headers"""
@@ -84,3 +85,24 @@ class doc(object):
        f.__apispec__ = f.__dict__.get('__apispec__', {})
        f.__apispec__.update(self.kwargs)
        return f


class response(object):
    def __init__(self, code, description, **kwargs):
        self.code = code
        self.description = description
        self.kwargs = kwargs

    def __call__(self, f):
        # Pass params to call function attribute for external access
        f.__apispec__ = f.__dict__.get('__apispec__', {})
        d = {
            "responses": {
                self.code: {
                    "description": self.description,
                    **self.kwargs
                }
            }
        }
        rupdate(f.__apispec__, d)
        return f
 No newline at end of file