Skip to content

Commit

Permalink
Restored W3C TD compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jun 18, 2020
1 parent 5c0ca64 commit e173b61
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
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
1 change: 0 additions & 1 deletion tests/test_server_spec_td.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class ViewClass:

def test_td_init(helpers, thing_description, app_ctx, schemas_path):
assert thing_description

helpers.validate_thing_description(thing_description, app_ctx, schemas_path)


Expand Down

0 comments on commit e173b61

Please sign in to comment.