Skip to content

Commit

Permalink
Move content_type into class responses dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jun 29, 2020
1 parent 6d4c873 commit a2302fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/labthings/server/spec/apispec.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,19 @@ def view_to_apispec_operations(view, apispec: APISpec):
if hasattr(view, "get_responses"):
ops[op]["responses"] = {}

for code, schema in view.get_responses().items():
for code, response in view.get_responses().items():
ops[op]["responses"][code] = {
"description": HTTPStatus(code).phrase,
"description": response.get("description")
or HTTPStatus(code).phrase,
"content": {
getattr(view, "content_type", "application/json"): {
"schema": convert_to_schema_or_json(schema, apispec)
# See if response description specifies a content_type
# If not, assume application/json
response.get("content_type", "application/json"): {
"schema": convert_to_schema_or_json(
response.get("schema"), apispec
)
}
if schema
if response.get("schema")
else {} # If no schema is defined, don't include one in the APISpec
},
}
Expand Down
13 changes: 8 additions & 5 deletions src/labthings/server/view/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class View(MethodView):
tags: list = []
title: None

# Content type of response, usually application/json
content_type: str = "application/json"

responses: dict = {}

def __init__(self, *args, **kwargs):
Expand All @@ -58,7 +55,7 @@ def __init__(self, *args, **kwargs):

@classmethod
def get_responses(cls):
r = {200: cls.schema}
r = {200: {"schema": cls.schema, "content_type": "application/json",}}
r.update(cls.responses)
return r

Expand Down Expand Up @@ -131,7 +128,13 @@ class ActionView(View):
@classmethod
def get_responses(cls):
"""Build an output schema that includes the Action wrapper object"""
r = {201: build_action_schema(cls.schema, cls.args)()}
r = {
201: {
"schema": build_action_schema(cls.schema, cls.args)(),
"content_type": "application/json",
"description": "Action started",
}
}
r.update(cls.responses)
return r

Expand Down

0 comments on commit a2302fe

Please sign in to comment.