|
8 | 8 | from .args import use_args
|
9 | 9 | from .marshalling import marshal_with
|
10 | 10 |
|
11 |
| -from ..utilities import unpack, get_docstring, get_summary |
| 11 | +from ..utilities import unpack, get_docstring, get_summary, merge |
12 | 12 | from ..representations import DEFAULT_REPRESENTATIONS
|
13 | 13 | from ..find import current_labthing
|
14 | 14 | from ..event import PropertyStatusEvent
|
15 | 15 | from ..schema import Schema, ActionSchema, build_action_schema
|
16 | 16 | from ..tasks import taskify
|
17 | 17 | from ..deque import Deque, resize_deque
|
18 | 18 | from ..json.schemas import schema_to_json
|
| 19 | +from .. import fields |
19 | 20 |
|
20 | 21 | from gevent.timeout import Timeout
|
21 | 22 |
|
@@ -62,6 +63,22 @@ def get_apispec(cls):
|
62 | 63 | or get_docstring(cls),
|
63 | 64 | "summary": getattr(cls, "summary", None) or get_summary(cls),
|
64 | 65 | "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 | + }, |
65 | 82 | }
|
66 | 83 | return d
|
67 | 84 |
|
@@ -152,7 +169,8 @@ def get_apispec(cls):
|
152 | 169 | # Get basic view spec
|
153 | 170 | d = super(ActionView, cls).get_apispec()
|
154 | 171 | # Add in Action spec
|
155 |
| - d.update( |
| 172 | + d = merge( |
| 173 | + d, |
156 | 174 | {
|
157 | 175 | "post": {
|
158 | 176 | "requestBody": {
|
@@ -201,7 +219,7 @@ def get_apispec(cls):
|
201 | 219 | }
|
202 | 220 | },
|
203 | 221 | },
|
204 |
| - } |
| 222 | + }, |
205 | 223 | )
|
206 | 224 | # Enable custom responses from POST
|
207 | 225 | d["post"]["responses"].update(cls.responses)
|
@@ -272,40 +290,50 @@ def get_apispec(cls):
|
272 | 290 | # Add in writeproperty methods
|
273 | 291 | for method in ("put", "post"):
|
274 | 292 | 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 | + }, |
284 | 316 | },
|
| 317 | + ) |
| 318 | + |
| 319 | + # Add in readproperty methods |
| 320 | + if hasattr(cls, "get"): |
| 321 | + d["get"] = merge( |
| 322 | + d.get("get", {}), |
| 323 | + { |
285 | 324 | "responses": {
|
286 | 325 | 200: {
|
287 |
| - "content_type": cls.content_type, |
288 |
| - "description": "Write property", |
| 326 | + "content_type": "application/json", |
| 327 | + "description": "Read property", |
289 | 328 | **(
|
290 | 329 | {"schema": class_json_schema}
|
291 | 330 | if class_json_schema
|
292 | 331 | else {}
|
293 | 332 | ),
|
294 | 333 | }
|
295 | 334 | },
|
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 |
| - } |
307 | 335 | },
|
308 |
| - } |
| 336 | + ) |
309 | 337 |
|
310 | 338 | # Enable custom responses from all methods
|
311 | 339 | for method in d.keys():
|
|
0 commit comments