Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions src/labthings/server/spec/td.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,65 @@ def find_schema_for_view(view: View):
return prop_schema


def build_forms_for_view(rules: list, view: View, op: list):
"""Build a W3C form description for a particular View

Args:
rules (list): List of Flask rules
view (View): View class
op (list): List of Form operations

Returns:
[dict]: Form description
"""
forms = []
prop_urls = [rule_to_path(rule) for rule in rules]

content_type = get_topmost_spec_attr(view, "_content_type") or "application/json"

for url in prop_urls:
forms.append({"op": op, "href": url, "contentType": content_type})

return forms


def view_to_thing_property_forms(rules: list, view: View):
"""Build a W3C form description for a PropertyView

Args:
rules (list): List of Flask rules
view (View): View class
op (list): List of Form operations

Returns:
[dict]: Form description
"""
readable = hasattr(view, "post") or hasattr(view, "put") or hasattr(view, "delete")
writeable = hasattr(view, "get")

op = []
if readable:
op.append("readproperty")
if writeable:
op.append("writeproperty")

return build_forms_for_view(rules, view, op=op)


def view_to_thing_action_forms(rules: list, view: View):
"""Build a W3C form description for an ActionView

Args:
rules (list): List of Flask rules
view (View): View class
op (list): List of Form operations

Returns:
[dict]: Form description
"""
return build_forms_for_view(rules, view, op=["invokeaction"])


class ThingDescription:
def __init__(self, apispec: APISpec):
self._apispec = weakref.ref(apispec)
Expand Down Expand Up @@ -91,21 +150,26 @@ def add_link(self, view, rel, kwargs=None, params=None):

def to_dict(self):
return {
"@context": ["https://iot.mozilla.org/schemas/"],
"@context": [
"https://www.w3.org/2019/wot/td/v1",
"https://iot.mozilla.org/schemas/",
],
"@type": current_labthing().types,
"id": url_for("root", _external=True),
"base": request.host_url,
"title": current_labthing().title,
"description": current_labthing().description,
"properties": self.properties,
"actions": self.actions,
"events": self.events, # TODO: Enable once properly populated
# "events": self.events, # TODO: Enable once properly populated
"links": self.links,
"securityDefinitions": {"nosec_sc": {"scheme": "nosec"}},
"security": "nosec_sc",
}

def event_to_thing_event(self, event: Event):
# TODO: Include event schema
return {}
return {"forms": []}

def view_to_thing_property(self, rules: list, view: View):
prop_urls = [rule_to_path(rule) for rule in rules]
Expand All @@ -122,8 +186,8 @@ def view_to_thing_property(self, rules: list, view: View):
hasattr(view, "post") or hasattr(view, "put") or hasattr(view, "delete")
),
"writeOnly": not hasattr(view, "get"),
# TODO: Make URLs absolute
"links": [{"href": f"{url}"} for url in prop_urls],
"forms": view_to_thing_property_forms(rules, view),
"uriVariables": {},
**get_semantic_type(view),
}
Expand Down Expand Up @@ -181,6 +245,7 @@ def view_to_thing_action(self, rules: list, view: View):
or (get_docstring(view.post) if hasattr(view, "post") else ""),
# TODO: Make URLs absolute
"links": [{"href": f"{url}"} for url in action_urls],
"forms": view_to_thing_action_forms(rules, view),
"safe": is_safe,
"idempotent": is_idempotent,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Helpers:
@staticmethod
def validate_thing_description(thing_description, app_ctx, schemas_path):
schema = json.load(open(os.path.join(schemas_path, "td_schema.json"), "r"))
schema = json.load(open(os.path.join(schemas_path, "w3c_td_schema.json"), "r"))
jsonschema.Draft7Validator.check_schema(schema)

with app_ctx.test_request_context():
Expand Down
Loading