1
- from flask import url_for , request
1
+ from flask import url_for , request , has_request_context
2
2
3
3
from .views import View
4
4
from .event import Event
5
5
from .json .schemas import schema_to_json , rule_to_params , rule_to_path
6
6
from .find import current_labthing
7
- from .utilities import get_docstring , snake_to_camel
7
+ from .utilities import ResourceURL , get_docstring , snake_to_camel
8
8
9
9
10
- def view_to_thing_action_forms (rules : list , view : View ):
10
+ def view_to_thing_action_forms (rules : list , view : View , external : bool = True ):
11
11
"""Build a W3C form description for an ActionView
12
12
13
13
:param rules: List of Flask rules
@@ -16,6 +16,7 @@ def view_to_thing_action_forms(rules: list, view: View):
16
16
:type view: View
17
17
:param rules: list:
18
18
:param view: View:
19
+ :param external: bool: Use external links where possible
19
20
:returns: Form description
20
21
:rtype: [dict]
21
22
@@ -36,7 +37,7 @@ def view_to_thing_action_forms(rules: list, view: View):
36
37
form = {
37
38
"op" : "invokeaction" ,
38
39
"htv:methodName" : "POST" ,
39
- "href" : url ,
40
+ "href" : ResourceURL ( url , external = external ) ,
40
41
"contentType" : content_type ,
41
42
}
42
43
if response_content_type != content_type :
@@ -47,7 +48,7 @@ def view_to_thing_action_forms(rules: list, view: View):
47
48
return forms
48
49
49
50
50
- def view_to_thing_property_forms (rules : list , view : View ):
51
+ def view_to_thing_property_forms (rules : list , view : View , external : bool = True ):
51
52
"""Build a W3C form description for a PropertyView
52
53
53
54
:param rules: List of Flask rules
@@ -56,6 +57,7 @@ def view_to_thing_property_forms(rules: list, view: View):
56
57
:type view: View
57
58
:param rules: list:
58
59
:param view: View:
60
+ :param external: bool: Use external links where possible
59
61
:returns: Form description
60
62
:rtype: [dict]
61
63
@@ -74,7 +76,7 @@ def view_to_thing_property_forms(rules: list, view: View):
74
76
form = {
75
77
"op" : "readproperty" ,
76
78
"htv:methodName" : "GET" ,
77
- "href" : url ,
79
+ "href" : ResourceURL ( url , external = external ) ,
78
80
"contentType" : content_type ,
79
81
}
80
82
forms .append (form )
@@ -85,7 +87,7 @@ def view_to_thing_property_forms(rules: list, view: View):
85
87
form = {
86
88
"op" : "writeproperty" ,
87
89
"htv:methodName" : "PUT" ,
88
- "href" : url ,
90
+ "href" : ResourceURL ( url , external = external ) ,
89
91
"contentType" : content_type ,
90
92
}
91
93
forms .append (form )
@@ -96,7 +98,7 @@ def view_to_thing_property_forms(rules: list, view: View):
96
98
form = {
97
99
"op" : "writeproperty" ,
98
100
"htv:methodName" : "POST" ,
99
- "href" : url ,
101
+ "href" : ResourceURL ( url , external = external ) ,
100
102
"contentType" : content_type ,
101
103
}
102
104
forms .append (form )
@@ -106,11 +108,20 @@ def view_to_thing_property_forms(rules: list, view: View):
106
108
107
109
class ThingDescription :
108
110
""" """
109
- def __init__ (self ):
111
+
112
+ def __init__ (self , external_links : bool = True ):
113
+ # Public attributes
110
114
self .properties = {}
111
115
self .actions = {}
112
116
self .events = {}
117
+
118
+ # Private attributes
113
119
self ._links = []
120
+
121
+ # Settings
122
+ self .external_links = external_links
123
+
124
+ # Init
114
125
super ().__init__ ()
115
126
116
127
@property
@@ -124,7 +135,7 @@ def links(self):
124
135
"href" : current_labthing ().url_for (
125
136
link_description .get ("view" ),
126
137
** link_description .get ("params" ),
127
- _external = True ,
138
+ _external = self . external_links ,
128
139
),
129
140
** link_description .get ("kwargs" ),
130
141
}
@@ -150,14 +161,13 @@ def add_link(self, view, rel, kwargs=None, params=None):
150
161
151
162
def to_dict (self ):
152
163
""" """
153
- return {
164
+ td = {
154
165
"@context" : [
155
166
"https://www.w3.org/2019/wot/td/v1" ,
156
167
"https://iot.mozilla.org/schemas/" ,
157
168
],
158
169
"@type" : current_labthing ().types ,
159
170
"id" : url_for ("root" , _external = True ),
160
- "base" : request .host_url ,
161
171
"title" : current_labthing ().title ,
162
172
"description" : current_labthing ().description ,
163
173
"properties" : self .properties ,
@@ -168,6 +178,11 @@ def to_dict(self):
168
178
"security" : "nosec_sc" ,
169
179
}
170
180
181
+ if not self .external_links and has_request_context ():
182
+ td ["base" ] = request .host_url
183
+
184
+ return td
185
+
171
186
def event_to_thing_event (self , event : Event ):
172
187
"""
173
188
@@ -194,8 +209,13 @@ def view_to_thing_property(self, rules: list, view: View):
194
209
hasattr (view , "post" ) or hasattr (view , "put" ) or hasattr (view , "delete" )
195
210
),
196
211
"writeOnly" : not hasattr (view , "get" ),
197
- "links" : [{"href" : f"{ url } " } for url in prop_urls ],
198
- "forms" : view_to_thing_property_forms (rules , view ),
212
+ "links" : [
213
+ {"href" : ResourceURL (url , external = self .external_links )}
214
+ for url in prop_urls
215
+ ],
216
+ "forms" : view_to_thing_property_forms (
217
+ rules , view , external = self .external_links
218
+ ),
199
219
"uriVariables" : {},
200
220
}
201
221
@@ -243,10 +263,15 @@ def view_to_thing_action(self, rules: list, view: View):
243
263
action_description = {
244
264
"title" : getattr (view , "title" , None ) or view .__name__ ,
245
265
"description" : getattr (view , "description" , None ) or get_docstring (view ),
246
- "links" : [{"href" : f"{ url } " } for url in action_urls ],
266
+ "links" : [
267
+ {"href" : ResourceURL (url , external = self .external_links )}
268
+ for url in action_urls
269
+ ],
247
270
"safe" : getattr (view , "safe" , False ),
248
271
"idempotent" : getattr (view , "idempotent" , False ),
249
- "forms" : view_to_thing_action_forms (rules , view ),
272
+ "forms" : view_to_thing_action_forms (
273
+ rules , view , external = self .external_links
274
+ ),
250
275
}
251
276
252
277
# Look for a _params in the Action classes API Spec
0 commit comments