Skip to content

Commit bc8ddc5

Browse files
author
Joel Collins
committed
SImplified interaction contentType definitions
1 parent 1140b8f commit bc8ddc5

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/labthings/view/__init__.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,14 @@ def represent_response(self, response):
113113

114114

115115
class ActionView(View):
116-
# TODO: Better support for overriding content type
117-
# TODO: Better support for custom 200 responses
118-
119116
# Data formatting
120117
schema: Schema = None
121118
args: dict = None
122119
semtype: str = None
123120

124121
# Spec overrides
122+
content_type = "application/json" # Input contentType
123+
response_content_type = "application/json" # Output contentType
125124
responses = {} # Custom responses for invokeaction
126125

127126
# Spec parameters
@@ -151,28 +150,38 @@ def get_apispec(cls):
151150
"tags": list(cls.get_tags()),
152151
"requestBody": {
153152
"content": {
154-
"application/json": (
155-
{"schema": class_args} if class_args else {}
156-
)
153+
cls.content_type: ({"schema": class_args} if class_args else {})
157154
}
158155
},
159156
"responses": {
160-
# Our POST 201 will usually be application/json
157+
200: {
158+
# Allow customising 200 (immediate response) content type
159+
"content_type": cls.response_content_type,
160+
"description": "Action completed immediately",
161+
**(
162+
# If an action JSON schema is defined, set the response schema
163+
{"schema": action_json_schema}
164+
if (action_json_schema)
165+
else {}
166+
),
167+
},
168+
# Our POST 201 MUST be application/json
169+
# Responses like images must be added as 200 responses with cls.responses = {200: {...}}
161170
201: {
162171
"content_type": "application/json",
163172
"description": "Action started",
164173
**(
165174
{"schema": action_json_schema} if action_json_schema else {}
166175
),
167-
}
176+
},
168177
},
169178
},
170179
"get": {
171180
"description": "Action queue",
172181
"summary": "Action queue",
173182
"tags": list(cls.get_tags()),
174183
"responses": {
175-
# Our GET 200 will usually be application/json
184+
# Our GET 200 MUST be application/json
176185
200: {
177186
"content_type": "application/json",
178187
"description": "Action started",
@@ -212,7 +221,6 @@ def dispatch_request(self, *args, **kwargs):
212221
# Wait up to 2 second for the action to complete or error
213222
try:
214223
task.get(block=True, timeout=1)
215-
logging.debug("Got Action response quickly")
216224
except Timeout:
217225
pass
218226

@@ -231,6 +239,7 @@ class PropertyView(View):
231239
semtype: str = None
232240

233241
# Spec overrides
242+
content_type = "application/json" # Input and output contentType
234243
responses = {} # Custom responses for invokeaction
235244

236245
_cls_tags = {"properties"}
@@ -250,7 +259,7 @@ def get_apispec(cls):
250259
"tags": list(cls.get_tags()),
251260
"requestBody": {
252261
"content": {
253-
"application/json": (
262+
cls.content_type: (
254263
{"schema": class_json_schema}
255264
if class_json_schema
256265
else {}
@@ -259,7 +268,7 @@ def get_apispec(cls):
259268
},
260269
"responses": {
261270
200: {
262-
"content_type": "application/json",
271+
"content_type": cls.content_type,
263272
"description": "Write property",
264273
**(
265274
{"schema": class_json_schema}

0 commit comments

Comments
 (0)