33
44from ..view import View
55
6- from .utilities import get_spec , convert_schema , schema_to_json
6+ from .utilities import get_spec , convert_schema , schema_to_json , get_topmost_spec_attr
77from .paths import rule_to_params , rule_to_path
88
99from ..find import current_labthing
@@ -79,12 +79,17 @@ def add_link(self, view, rel, kwargs=None, params=None):
7979 def to_dict (self ):
8080 return {
8181 "@context" : "https://www.w3.org/2019/wot/td/v1" ,
82+ "@type" : current_labthing ().types ,
8283 "id" : url_for ("root" , _external = True ),
84+ "base" : url_for ("root" , _external = True ),
8385 "title" : current_labthing ().title ,
8486 "description" : current_labthing ().description ,
8587 "properties" : self .properties ,
8688 "actions" : self .actions ,
8789 "links" : self .links ,
90+ # TODO: Add proper security schemes
91+ "securityDefinitions" : {"nosec_sc" : {"scheme" : "nosec" }},
92+ "security" : ["nosec_sc" ],
8893 }
8994
9095 def view_to_thing_property (self , rules : list , view : View ):
@@ -104,6 +109,7 @@ def view_to_thing_property(self, rules: list, view: View):
104109 "writeOnly" : not hasattr (view , "get" ),
105110 # TODO: Make URLs absolute
106111 "links" : [{"href" : f"{ url } " } for url in prop_urls ],
112+ "forms" : self .view_to_thing_property_forms (rules , view ),
107113 "uriVariables" : {},
108114 }
109115
@@ -134,6 +140,20 @@ def view_to_thing_property(self, rules: list, view: View):
134140
135141 return prop_description
136142
143+ def view_to_thing_property_forms (self , rules : list , view : View ):
144+ readable = (
145+ hasattr (view , "post" ) or hasattr (view , "put" ) or hasattr (view , "delete" )
146+ )
147+ writeable = hasattr (view , "get" )
148+
149+ op = []
150+ if readable :
151+ op .append ("readproperty" )
152+ if writeable :
153+ op .append ("writeproperty" )
154+
155+ return self .build_forms_for_view (rules , view , op = op )
156+
137157 def view_to_thing_action (self , rules : list , view : View ):
138158 action_urls = [rule_to_path (rule ) for rule in rules ]
139159
@@ -145,14 +165,31 @@ def view_to_thing_action(self, rules: list, view: View):
145165 or (get_docstring (view .post ) if hasattr (view , "post" ) else "" ),
146166 # TODO: Make URLs absolute
147167 "links" : [{"href" : f"{ url } " } for url in action_urls ],
168+ "forms" : self .view_to_thing_action_forms (rules , view ),
148169 }
149170
150171 return action_description
151172
173+ def view_to_thing_action_forms (self , rules : list , view : View ):
174+ return self .build_forms_for_view (rules , view , op = ["invokeaction" ])
175+
152176 def property (self , rules : list , view : View ):
153177 key = snake_to_camel (view .endpoint )
154178 self .properties [key ] = self .view_to_thing_property (rules , view )
155179
156180 def action (self , rules : list , view : View ):
157181 key = snake_to_camel (view .endpoint )
158182 self .actions [key ] = self .view_to_thing_action (rules , view )
183+
184+ def build_forms_for_view (self , rules : list , view : View , op : list ):
185+ forms = []
186+ prop_urls = [rule_to_path (rule ) for rule in rules ]
187+
188+ content_type = (
189+ get_topmost_spec_attr (view , "_content_type" ) or "application/json"
190+ )
191+
192+ for url in prop_urls :
193+ forms .append ({"op" : op , "href" : url , "contentType" : content_type })
194+
195+ return forms
0 commit comments