Skip to content

Commit d88462a

Browse files
committed
updates property handlers to move towards compatibility with http basic profile
1 parent 35dd9b9 commit d88462a

2 files changed

Lines changed: 49 additions & 13 deletions

File tree

webthing/server.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def get(self):
151151
for thing in self.things.get_things():
152152
description = thing.as_thing_description()
153153
description['href'] = thing.get_href()
154-
description['forms'].append({
154+
description['links'].append({
155155
'rel': 'alternate',
156156
'href': '{}{}'.format(ws_href, thing.get_href()),
157157
})
@@ -243,7 +243,7 @@ def get(self, thing_id='0'):
243243
)
244244

245245
description = self.thing.as_thing_description()
246-
description['forms'].append({
246+
description['links'].append({
247247
'rel': 'alternate',
248248
'href': '{}{}'.format(ws_href, self.thing.get_href()),
249249
})
@@ -419,6 +419,36 @@ def get(self, thing_id='0'):
419419
self.set_header('Content-Type', 'application/json')
420420
self.write(json.dumps(thing.get_properties()))
421421

422+
def put(self, thing_id='0'):
423+
"""
424+
Handle a PUT request.
425+
426+
thing_id -- ID of the thing this request is for
427+
"""
428+
thing = self.get_thing(thing_id)
429+
if thing is None:
430+
self.set_status(404)
431+
return
432+
433+
try:
434+
properties = json.loads(self.request.body.decode())
435+
except ValueError:
436+
self.set_status(400)
437+
return
438+
439+
for property_name, value in properties.items():
440+
if thing.has_property(property_name):
441+
try:
442+
thing.set_property(property_name, value)
443+
except PropertyError:
444+
self.set_status(400)
445+
return
446+
else:
447+
self.set_status(404)
448+
return
449+
450+
self.set_status(204)
451+
422452

423453
class PropertyHandler(BaseHandler):
424454
"""Handle a request to /properties/<property>."""
@@ -466,8 +496,7 @@ def put(self, thing_id='0', property_name=None):
466496
self.set_status(400)
467497
return
468498

469-
self.set_header('Content-Type', 'application/json')
470-
self.write(json.dumps(thing.get_property(property_name)))
499+
self.set_status(204)
471500
else:
472501
self.set_status(404)
473502

@@ -570,6 +599,7 @@ def post(self, thing_id='0', action_name=None):
570599
return
571600

572601
# Allow payloads wrapped inside `value` field
602+
#TODO: remove this in the future
573603
if 'value' in input_:
574604
input_ = input_['value']
575605

webthing/thing.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def __init__(self, id_, title, type_=[], description=''):
2020
type_ = [type_]
2121

2222
self.id = id_
23-
self.context = 'https://webthings.io/schemas'
23+
self.context = ['https://www.w3.org/ns/wot-next/td', 'https://webthings.io/schemas']
24+
self.profiles = ["https://www.w3.org/2022/wot/profile/http-basic/v1"]
2425
self.type = type_
2526
self.title = title
2627
self.description = description
@@ -32,6 +33,9 @@ def __init__(self, id_, title, type_=[], description=''):
3233
self.subscribers = set()
3334
self.href_prefix = ''
3435
self.ui_href = None
36+
self.securityDefinitions = { "nosec_sc": { "scheme": "nosec" }}
37+
self.security = "nosec_sc"
38+
3539

3640
def as_thing_description(self):
3741
"""
@@ -43,27 +47,29 @@ def as_thing_description(self):
4347
'id': self.id,
4448
'title': self.title,
4549
'@context': self.context,
50+
'profile': self.profiles,
4651
'properties': self.get_property_descriptions(),
4752
'actions': {},
4853
'events': {},
4954
'forms': [
5055
{
51-
'rel': 'properties',
56+
'op': "readallproperties",
5257
'href': '{}/properties'.format(self.href_prefix),
58+
'contentType': 'application/json',
5359
},
5460
{
55-
'rel': 'actions',
56-
'href': '{}/actions'.format(self.href_prefix),
57-
},
58-
{
59-
'rel': 'events',
60-
'href': '{}/events'.format(self.href_prefix),
61-
},
61+
'op': "writemultipleproperties",
62+
'href': '{}/properties'.format(self.href_prefix),
63+
'contentType': 'application/json',
64+
}
6265
],
66+
'securityDefinitions': self.securityDefinitions,
67+
'security': self.security
6368
}
6469

6570
for name, action in self.available_actions.items():
6671
thing['actions'][name] = action['metadata']
72+
thing['actions'][name]['synchronous'] = True
6773
thing['actions'][name]['forms'] = [
6874
{
6975
'op': ['invokeaction'],

0 commit comments

Comments
 (0)