Skip to content

Commit 48196ec

Browse files
author
Joel Collins
committed
Added default error schema to API spec
1 parent 2371bd6 commit 48196ec

File tree

1 file changed

+54
-26
lines changed

1 file changed

+54
-26
lines changed

src/labthings/view/__init__.py

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
from .args import use_args
99
from .marshalling import marshal_with
1010

11-
from ..utilities import unpack, get_docstring, get_summary
11+
from ..utilities import unpack, get_docstring, get_summary, merge
1212
from ..representations import DEFAULT_REPRESENTATIONS
1313
from ..find import current_labthing
1414
from ..event import PropertyStatusEvent
1515
from ..schema import Schema, ActionSchema, build_action_schema
1616
from ..tasks import taskify
1717
from ..deque import Deque, resize_deque
1818
from ..json.schemas import schema_to_json
19+
from .. import fields
1920

2021
from gevent.timeout import Timeout
2122

@@ -62,6 +63,22 @@ def get_apispec(cls):
6263
or get_docstring(cls),
6364
"summary": getattr(cls, "summary", None) or get_summary(cls),
6465
"tags": list(cls.get_tags()),
66+
"responses": {
67+
"default": {
68+
"description": "Unexpected error",
69+
"content": {
70+
"application/json": {
71+
"schema": schema_to_json(
72+
{
73+
"code": fields.Integer(),
74+
"message": fields.String(),
75+
"name": fields.String(),
76+
}
77+
)
78+
}
79+
},
80+
}
81+
},
6582
}
6683
return d
6784

@@ -152,7 +169,8 @@ def get_apispec(cls):
152169
# Get basic view spec
153170
d = super(ActionView, cls).get_apispec()
154171
# Add in Action spec
155-
d.update(
172+
d = merge(
173+
d,
156174
{
157175
"post": {
158176
"requestBody": {
@@ -201,7 +219,7 @@ def get_apispec(cls):
201219
}
202220
},
203221
},
204-
}
222+
},
205223
)
206224
# Enable custom responses from POST
207225
d["post"]["responses"].update(cls.responses)
@@ -272,40 +290,50 @@ def get_apispec(cls):
272290
# Add in writeproperty methods
273291
for method in ("put", "post"):
274292
if hasattr(cls, method):
275-
d[method] = {
276-
"requestBody": {
277-
"content": {
278-
cls.content_type: (
279-
{"schema": class_json_schema}
280-
if class_json_schema
281-
else {}
282-
)
283-
}
293+
d[method] = merge(
294+
d.get(method, {}),
295+
{
296+
"requestBody": {
297+
"content": {
298+
cls.content_type: (
299+
{"schema": class_json_schema}
300+
if class_json_schema
301+
else {}
302+
)
303+
}
304+
},
305+
"responses": {
306+
200: {
307+
"content_type": cls.content_type,
308+
"description": "Write property",
309+
**(
310+
{"schema": class_json_schema}
311+
if class_json_schema
312+
else {}
313+
),
314+
}
315+
},
284316
},
317+
)
318+
319+
# Add in readproperty methods
320+
if hasattr(cls, "get"):
321+
d["get"] = merge(
322+
d.get("get", {}),
323+
{
285324
"responses": {
286325
200: {
287-
"content_type": cls.content_type,
288-
"description": "Write property",
326+
"content_type": "application/json",
327+
"description": "Read property",
289328
**(
290329
{"schema": class_json_schema}
291330
if class_json_schema
292331
else {}
293332
),
294333
}
295334
},
296-
}
297-
298-
# Add in readproperty methods
299-
if hasattr(cls, "get"):
300-
d["get"] = {
301-
"responses": {
302-
200: {
303-
"content_type": "application/json",
304-
"description": "Read property",
305-
**({"schema": class_json_schema} if class_json_schema else {}),
306-
}
307335
},
308-
}
336+
)
309337

310338
# Enable custom responses from all methods
311339
for method in d.keys():

0 commit comments

Comments
 (0)