Skip to content

Commit e173b61

Browse files
author
Joel Collins
committed
Restored W3C TD compatibility
1 parent 5c0ca64 commit e173b61

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

src/labthings/server/spec/td.py

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,65 @@ def find_schema_for_view(view: View):
5050
return prop_schema
5151

5252

53+
def build_forms_for_view(rules: list, view: View, op: list):
54+
"""Build a W3C form description for a particular View
55+
56+
Args:
57+
rules (list): List of Flask rules
58+
view (View): View class
59+
op (list): List of Form operations
60+
61+
Returns:
62+
[dict]: Form description
63+
"""
64+
forms = []
65+
prop_urls = [rule_to_path(rule) for rule in rules]
66+
67+
content_type = get_topmost_spec_attr(view, "_content_type") or "application/json"
68+
69+
for url in prop_urls:
70+
forms.append({"op": op, "href": url, "contentType": content_type})
71+
72+
return forms
73+
74+
75+
def view_to_thing_property_forms(rules: list, view: View):
76+
"""Build a W3C form description for a PropertyView
77+
78+
Args:
79+
rules (list): List of Flask rules
80+
view (View): View class
81+
op (list): List of Form operations
82+
83+
Returns:
84+
[dict]: Form description
85+
"""
86+
readable = hasattr(view, "post") or hasattr(view, "put") or hasattr(view, "delete")
87+
writeable = hasattr(view, "get")
88+
89+
op = []
90+
if readable:
91+
op.append("readproperty")
92+
if writeable:
93+
op.append("writeproperty")
94+
95+
return build_forms_for_view(rules, view, op=op)
96+
97+
98+
def view_to_thing_action_forms(rules: list, view: View):
99+
"""Build a W3C form description for an ActionView
100+
101+
Args:
102+
rules (list): List of Flask rules
103+
view (View): View class
104+
op (list): List of Form operations
105+
106+
Returns:
107+
[dict]: Form description
108+
"""
109+
return build_forms_for_view(rules, view, op=["invokeaction"])
110+
111+
53112
class ThingDescription:
54113
def __init__(self, apispec: APISpec):
55114
self._apispec = weakref.ref(apispec)
@@ -91,21 +150,26 @@ def add_link(self, view, rel, kwargs=None, params=None):
91150

92151
def to_dict(self):
93152
return {
94-
"@context": ["https://iot.mozilla.org/schemas/"],
153+
"@context": [
154+
"https://www.w3.org/2019/wot/td/v1",
155+
"https://iot.mozilla.org/schemas/",
156+
],
95157
"@type": current_labthing().types,
96158
"id": url_for("root", _external=True),
97159
"base": request.host_url,
98160
"title": current_labthing().title,
99161
"description": current_labthing().description,
100162
"properties": self.properties,
101163
"actions": self.actions,
102-
"events": self.events, # TODO: Enable once properly populated
164+
# "events": self.events, # TODO: Enable once properly populated
103165
"links": self.links,
166+
"securityDefinitions": {"nosec_sc": {"scheme": "nosec"}},
167+
"security": "nosec_sc",
104168
}
105169

106170
def event_to_thing_event(self, event: Event):
107171
# TODO: Include event schema
108-
return {}
172+
return {"forms": []}
109173

110174
def view_to_thing_property(self, rules: list, view: View):
111175
prop_urls = [rule_to_path(rule) for rule in rules]
@@ -122,8 +186,8 @@ def view_to_thing_property(self, rules: list, view: View):
122186
hasattr(view, "post") or hasattr(view, "put") or hasattr(view, "delete")
123187
),
124188
"writeOnly": not hasattr(view, "get"),
125-
# TODO: Make URLs absolute
126189
"links": [{"href": f"{url}"} for url in prop_urls],
190+
"forms": view_to_thing_property_forms(rules, view),
127191
"uriVariables": {},
128192
**get_semantic_type(view),
129193
}
@@ -181,6 +245,7 @@ def view_to_thing_action(self, rules: list, view: View):
181245
or (get_docstring(view.post) if hasattr(view, "post") else ""),
182246
# TODO: Make URLs absolute
183247
"links": [{"href": f"{url}"} for url in action_urls],
248+
"forms": view_to_thing_action_forms(rules, view),
184249
"safe": is_safe,
185250
"idempotent": is_idempotent,
186251
}

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class Helpers:
1717
@staticmethod
1818
def validate_thing_description(thing_description, app_ctx, schemas_path):
19-
schema = json.load(open(os.path.join(schemas_path, "td_schema.json"), "r"))
19+
schema = json.load(open(os.path.join(schemas_path, "w3c_td_schema.json"), "r"))
2020
jsonschema.Draft7Validator.check_schema(schema)
2121

2222
with app_ctx.test_request_context():

tests/test_server_spec_td.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class ViewClass:
4646

4747
def test_td_init(helpers, thing_description, app_ctx, schemas_path):
4848
assert thing_description
49-
5049
helpers.validate_thing_description(thing_description, app_ctx, schemas_path)
5150

5251

0 commit comments

Comments
 (0)