@@ -50,6 +50,65 @@ def find_schema_for_view(view: View):
50
50
return prop_schema
51
51
52
52
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
+
53
112
class ThingDescription :
54
113
def __init__ (self , apispec : APISpec ):
55
114
self ._apispec = weakref .ref (apispec )
@@ -91,21 +150,26 @@ def add_link(self, view, rel, kwargs=None, params=None):
91
150
92
151
def to_dict (self ):
93
152
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
+ ],
95
157
"@type" : current_labthing ().types ,
96
158
"id" : url_for ("root" , _external = True ),
97
159
"base" : request .host_url ,
98
160
"title" : current_labthing ().title ,
99
161
"description" : current_labthing ().description ,
100
162
"properties" : self .properties ,
101
163
"actions" : self .actions ,
102
- "events" : self .events , # TODO: Enable once properly populated
164
+ # "events": self.events, # TODO: Enable once properly populated
103
165
"links" : self .links ,
166
+ "securityDefinitions" : {"nosec_sc" : {"scheme" : "nosec" }},
167
+ "security" : "nosec_sc" ,
104
168
}
105
169
106
170
def event_to_thing_event (self , event : Event ):
107
171
# TODO: Include event schema
108
- return {}
172
+ return {"forms" : [] }
109
173
110
174
def view_to_thing_property (self , rules : list , view : View ):
111
175
prop_urls = [rule_to_path (rule ) for rule in rules ]
@@ -122,8 +186,8 @@ def view_to_thing_property(self, rules: list, view: View):
122
186
hasattr (view , "post" ) or hasattr (view , "put" ) or hasattr (view , "delete" )
123
187
),
124
188
"writeOnly" : not hasattr (view , "get" ),
125
- # TODO: Make URLs absolute
126
189
"links" : [{"href" : f"{ url } " } for url in prop_urls ],
190
+ "forms" : view_to_thing_property_forms (rules , view ),
127
191
"uriVariables" : {},
128
192
** get_semantic_type (view ),
129
193
}
@@ -181,6 +245,7 @@ def view_to_thing_action(self, rules: list, view: View):
181
245
or (get_docstring (view .post ) if hasattr (view , "post" ) else "" ),
182
246
# TODO: Make URLs absolute
183
247
"links" : [{"href" : f"{ url } " } for url in action_urls ],
248
+ "forms" : view_to_thing_action_forms (rules , view ),
184
249
"safe" : is_safe ,
185
250
"idempotent" : is_idempotent ,
186
251
}
0 commit comments