From b3f32f23095c45f9dfc1d7962793a04f211a66aa Mon Sep 17 00:00:00 2001 From: jtc42 Date: Thu, 2 Jan 2020 22:43:44 +0000 Subject: [PATCH] Added decorator for documenting response codes --- .../api/v2/views/actions/camera.py | 3 ++- .../common/flask_labthings/decorators.py | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/openflexure_microscope/api/v2/views/actions/camera.py b/openflexure_microscope/api/v2/views/actions/camera.py index b6a7ef7f..66a3af92 100644 --- a/openflexure_microscope/api/v2/views/actions/camera.py +++ b/openflexure_microscope/api/v2/views/actions/camera.py @@ -1,7 +1,7 @@ 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") diff --git a/openflexure_microscope/common/flask_labthings/decorators.py b/openflexure_microscope/common/flask_labthings/decorators.py index 41bdd368..2952ca91 100644 --- a/openflexure_microscope/common/flask_labthings/decorators.py +++ b/openflexure_microscope/common/flask_labthings/decorators.py @@ -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""" @@ -83,4 +84,25 @@ class doc(object): # Pass params to call function attribute for external access 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