Skip to content

Commit 2b4c3c5

Browse files
authored
Merge pull request #243 from labthings/improve-openapi
Improve openapi
2 parents a2ef870 + 8ddfaec commit 2b4c3c5

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

src/labthings/apispec/plugins.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,19 @@ def spec_for_interaction(cls, interaction):
6060

6161
for method in http_method_funcs:
6262
if hasattr(interaction, method):
63+
prop = getattr(interaction, method)
6364
d[method] = {
64-
"description": getattr(interaction, "description", None)
65-
or get_docstring(interaction),
66-
"summary": getattr(interaction, "summary", None)
65+
"description": getattr(prop, "description", None)
66+
or get_docstring(prop, remove_newlines=False)
67+
or getattr(interaction, "description", None)
68+
or get_docstring(interaction, remove_newlines=False),
69+
"summary": getattr(prop, "summary", None)
70+
or get_summary(prop)
71+
or getattr(interaction, "summary", None)
6772
or get_summary(interaction),
6873
"tags": list(interaction.get_tags()),
6974
"responses": {
70-
"default": {
75+
"5XX": {
7176
"description": "Unexpected error",
7277
"content": {
7378
"application/json": {

src/labthings/utilities.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import inspect
23
import operator
34
import os
45
import re
@@ -152,12 +153,12 @@ def get_docstring(obj: Any, remove_newlines=True) -> str:
152153
153154
"""
154155
ds = obj.__doc__
155-
if ds:
156+
if not ds:
157+
return ""
158+
if remove_newlines:
156159
stripped = [line.strip() for line in ds.splitlines() if line]
157-
if not remove_newlines:
158-
return "\n".join(stripped)
159160
return " ".join(stripped).replace("\n", " ").replace("\r", "")
160-
return ""
161+
return inspect.cleandoc(ds) # Strip spurious indentation/newlines
161162

162163

163164
def get_summary(obj: Any) -> str:

src/labthings/views/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
build_action_schema,
1919
)
2020
from ..utilities import unpack
21-
from . import builder, op
2221

2322
__all__ = ["MethodView", "View", "ActionView", "PropertyView", "op", "builder"]
2423

@@ -172,7 +171,12 @@ def __init_subclass__(cls):
172171
@classmethod
173172
def get(cls):
174173
"""
175-
Default method for GET requests. Returns the action queue (including already finished actions) for this action
174+
List running and completed actions.
175+
176+
Actions are run with `POST` requests. See the `POST` method for this URL for
177+
details of the action. Sending a `GET` request to an action endpoint will return
178+
action descriptions for each time the action has been run, including whether they
179+
have completed, and any return values.
176180
"""
177181
queue_schema = build_action_schema(cls.schema, cls.args)(many=True)
178182
return queue_schema.dump(cls._deque)

src/labthings/views/builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from typing import Type
55

66
from flask import abort, send_file
7-
from flask.views import MethodView
7+
from . import View
88

99

10-
def static_from(static_folder: str, name=None) -> Type[MethodView]:
10+
def static_from(static_folder: str, name=None) -> Type[View]:
1111
"""
1212
:param static_folder: str:
1313
:param name: (Default value = None)
@@ -37,6 +37,6 @@ def _get(_, path=""):
3737
return send_file(indexes[0])
3838

3939
# Generate a basic property class
40-
generated_class = type(name, (MethodView, object), {"get": _get})
40+
generated_class = type(name, (View, object), {"get": _get})
4141

4242
return generated_class

tests/test_utilities.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def test_get_docstring(example_class):
3030
utilities.get_docstring(example_class)
3131
== "First line of class docstring. Second line of class docstring. "
3232
)
33+
assert (
34+
utilities.get_docstring(example_class, remove_newlines=False)
35+
== "First line of class docstring.\nSecond line of class docstring."
36+
)
3337

3438
assert utilities.get_docstring(example_class.class_method) == (
3539
"First line of class method docstring. Second line of class method docstring. "

0 commit comments

Comments
 (0)