Skip to content
This repository was archived by the owner on Jun 7, 2021. It is now read-only.

Commit ff90caa

Browse files
authored
Add id, base, security, and securityDefinitions. (#56)
1 parent 7d6ec61 commit ff90caa

6 files changed

Lines changed: 70 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# webthing Changelog
22

33
## [Unreleased]
4-
### Added
5-
- Ability to set a base URL path on server.
64
### Changed
75
- Things now use `title` rather than `name`.
6+
- Things now require a unique ID in the form of a URI.
7+
### Added
8+
- Ability to set a base URL path on server.
9+
- Support for `id`, `base`, `security`, and `securityDefinitions` keys in thing description.
810

911
## [0.11.3] - 2019-04-10
1012
### Changed

README.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ First we create a new Thing:
5454

5555
.. code:: python
5656
57-
light = Thing('My Lamp', ['OnOffSwitch', 'Light'], 'A web connected lamp')
57+
light = Thing(
58+
'urn:dev:ops:my-lamp-1234',
59+
'My Lamp',
60+
['OnOffSwitch', 'Light'],
61+
'A web connected lamp'
62+
)
5863
5964
Now we can add the required properties.
6065

@@ -122,9 +127,12 @@ First we create a new Thing:
122127

123128
.. code:: python
124129
125-
sensor = Thing('My Humidity Sensor',
126-
['MultiLevelSensor'],
127-
'A web connected humidity sensor')
130+
sensor = Thing(
131+
'urn:dev:ops:my-humidity-sensor-1234',
132+
'My Humidity Sensor',
133+
['MultiLevelSensor'],
134+
'A web connected humidity sensor'
135+
)
128136
129137
Then we create and add the appropriate property:
130138

example/multiple-things.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ class ExampleDimmableLight(Thing):
2929
"""A dimmable light that logs received commands to stdout."""
3030

3131
def __init__(self):
32-
Thing.__init__(self,
33-
'My Lamp',
34-
['OnOffSwitch', 'Light'],
35-
'A web connected lamp')
32+
Thing.__init__(
33+
self,
34+
'urn:dev:ops:my-lamp-1234',
35+
'My Lamp',
36+
['OnOffSwitch', 'Light'],
37+
'A web connected lamp'
38+
)
3639

3740
self.add_property(
3841
Property(self,
@@ -101,10 +104,13 @@ class FakeGpioHumiditySensor(Thing):
101104
"""A humidity sensor which updates its measurement every few seconds."""
102105

103106
def __init__(self):
104-
Thing.__init__(self,
105-
'My Humidity Sensor',
106-
['MultiLevelSensor'],
107-
'A web connected humidity sensor')
107+
Thing.__init__(
108+
self,
109+
'urn:dev:ops:my-humidity-sensor-1234',
110+
'My Humidity Sensor',
111+
['MultiLevelSensor'],
112+
'A web connected humidity sensor'
113+
)
108114

109115
self.level = Value(0.0)
110116
self.add_property(

example/single-thing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ def perform_action(self):
2424

2525

2626
def make_thing():
27-
thing = Thing('My Lamp', ['OnOffSwitch', 'Light'], 'A web connected lamp')
27+
thing = Thing(
28+
'urn:dev:ops:my-lamp-1234',
29+
'My Lamp',
30+
['OnOffSwitch', 'Light'],
31+
'A web connected lamp'
32+
)
2833

2934
thing.add_property(
3035
Property(thing,

webthing/server.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ def get(self):
144144
'rel': 'alternate',
145145
'href': '{}{}'.format(ws_href, thing.get_href()),
146146
})
147+
description['base'] = '{}://{}{}'.format(
148+
self.request.protocol,
149+
self.request.headers.get('Host', ''),
150+
thing.get_href()
151+
)
152+
description['securityDefinitions'] = {
153+
'nosec_sc': {
154+
'scheme': 'nosec',
155+
},
156+
}
157+
description['security'] = 'nosec_sc'
147158
descriptions.append(description)
148159

149160
self.write(json.dumps(descriptions))
@@ -216,6 +227,17 @@ def get(self, thing_id='0'):
216227
'rel': 'alternate',
217228
'href': '{}{}'.format(ws_href, self.thing.get_href()),
218229
})
230+
description['base'] = '{}://{}{}'.format(
231+
self.request.protocol,
232+
self.request.headers.get('Host', ''),
233+
self.thing.get_href()
234+
)
235+
description['securityDefinitions'] = {
236+
'nosec_sc': {
237+
'scheme': 'nosec',
238+
},
239+
}
240+
description['security'] = 'nosec_sc'
219241

220242
self.write(json.dumps(description))
221243
self.finish()

webthing/thing.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
class Thing:
1010
"""A Web Thing."""
1111

12-
def __init__(self, title, type_=[], description=''):
12+
def __init__(self, id_, title, type_=[], description=''):
1313
"""
1414
Initialize the object.
1515
16+
id_ -- the thing's unique ID - must be a URI
1617
title -- the thing's title
1718
type_ -- the thing's type(s)
1819
description -- description of the thing
1920
"""
2021
if not isinstance(type_, list):
2122
type_ = [type_]
2223

24+
self.id = id_
2325
self.context = 'https://iot.mozilla.org/schemas'
2426
self.type = type_
2527
self.title = title
@@ -40,8 +42,8 @@ def as_thing_description(self):
4042
Returns the state as a dictionary.
4143
"""
4244
thing = {
45+
'id': self.id,
4346
'title': self.title,
44-
'href': self.href_prefix if self.href_prefix else '/',
4547
'@context': self.context,
4648
'@type': self.type,
4749
'properties': self.get_property_descriptions(),
@@ -127,6 +129,14 @@ def set_ui_href(self, href):
127129
"""
128130
self.ui_href = href
129131

132+
def get_id(self):
133+
"""
134+
Get the ID of the thing.
135+
136+
Returns the ID as a string.
137+
"""
138+
return self.id
139+
130140
def get_title(self):
131141
"""
132142
Get the title of the thing.

0 commit comments

Comments
 (0)