Skip to content

Commit 8427445

Browse files
author
Joel Collins
committed
Handle different request and response types in TD
1 parent 6eb71ed commit 8427445

File tree

3 files changed

+68
-38
lines changed

3 files changed

+68
-38
lines changed

src/labthings/td.py

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ def view_to_thing_action_forms(rules: list, view: View):
4444
break
4545

4646
for url in prop_urls:
47-
forms.append(
48-
{
49-
"op": ["invokeaction"],
50-
"htv:methodName": "POST",
51-
"href": url,
52-
"contentType": content_type,
53-
"response": {"contentType": response_content_type},
54-
}
55-
)
47+
form = {
48+
"op": ["invokeaction"],
49+
"htv:methodName": "POST",
50+
"href": url,
51+
"contentType": content_type,
52+
}
53+
if response_content_type != content_type:
54+
form["responses"] = {"contentType": response_content_type}
55+
56+
forms.append(form)
5657

5758
return forms
5859

@@ -84,43 +85,49 @@ def view_to_thing_property_forms(rules: list, view: View):
8485
response_content_type = responses[response_code].get("content_type")
8586
break
8687

88+
# TODO: Clean up repeated code
89+
8790
# HTTP readproperty requires GET method
8891
if hasattr(view, "get"):
8992
for url in prop_urls:
90-
forms.append(
91-
{
92-
"op": ["readproperty"],
93-
"htv:methodName": "GET",
94-
"href": url,
95-
"contentType": content_type,
96-
"response": {"contentType": response_content_type},
97-
}
98-
)
93+
form = {
94+
"op": ["readproperty"],
95+
"htv:methodName": "GET",
96+
"href": url,
97+
"contentType": content_type,
98+
"response": {"contentType": response_content_type},
99+
}
100+
if response_content_type != content_type:
101+
form["responses"] = {"contentType": response_content_type}
102+
forms.append(form)
99103

100104
# HTTP writeproperty requires PUT method
101105
if hasattr(view, "put"):
102106
for url in prop_urls:
103-
forms.append(
104-
{
105-
"op": ["writeproperty"],
106-
"htv:methodName": "PUT",
107-
"href": url,
108-
"contentType": content_type,
109-
"response": {"contentType": response_content_type},
110-
}
111-
)
107+
form = {
108+
"op": ["writeproperty"],
109+
"htv:methodName": "PUT",
110+
"href": url,
111+
"contentType": content_type,
112+
"response": {"contentType": response_content_type},
113+
}
114+
if response_content_type != content_type:
115+
form["responses"] = {"contentType": response_content_type}
116+
forms.append(form)
117+
112118
# HTTP writeproperty may use POST method
113119
elif hasattr(view, "post"):
114120
for url in prop_urls:
115-
forms.append(
116-
{
117-
"op": ["writeproperty"],
118-
"htv:methodName": "POST",
119-
"href": url,
120-
"contentType": content_type,
121-
"response": {"contentType": response_content_type},
122-
}
123-
)
121+
form = {
122+
"op": ["writeproperty"],
123+
"htv:methodName": "POST",
124+
"href": url,
125+
"contentType": content_type,
126+
"response": {"contentType": response_content_type},
127+
}
128+
if response_content_type != content_type:
129+
form["responses"] = {"contentType": response_content_type}
130+
forms.append(form)
124131

125132
return forms
126133

tests/old_api/test_server_spec_td.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def test_td_action_with_schema(
8787
"htv:methodName": "POST",
8888
"href": "/",
8989
"contentType": "application/json",
90-
"response": {"contentType": "application/json"},
9190
}
9291
],
9392
"input": {

tests/test_td.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def test_td_action_with_schema(
8787
"htv:methodName": "POST",
8888
"href": "/",
8989
"contentType": "application/json",
90-
"response": {"contentType": "application/json"},
9190
}
9291
],
9392
"input": {
@@ -191,3 +190,28 @@ def post(self):
191190
== "POST"
192191
)
193192
helpers.validate_thing_description(thing_description, app_ctx, schemas_path)
193+
194+
195+
def test_td_property_different_response_type(
196+
helpers, app, thing_description, app_ctx, schemas_path
197+
):
198+
class Index(View):
199+
def get(self):
200+
return "GET"
201+
202+
def put(self):
203+
return "PUT"
204+
205+
Index.schema = fields.Int()
206+
Index.responses[200] = {"content_type": "text/plain; charset=us-ascii"}
207+
208+
app.add_url_rule("/", view_func=Index.as_view("index"))
209+
rules = app.url_map._rules_by_endpoint["index"]
210+
211+
thing_description.property(rules, Index)
212+
213+
with app_ctx.test_request_context():
214+
assert "index" in thing_description.to_dict()["properties"]
215+
for form in thing_description.to_dict()["properties"]["index"]["forms"]:
216+
assert form["response"]["contentType"] == "text/plain; charset=us-ascii"
217+
helpers.validate_thing_description(thing_description, app_ctx, schemas_path)

0 commit comments

Comments
 (0)