FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add id, base, security, and securityDefinitions. by mrstegeman · Pull Request #56 · WebThingsIO/webthing-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .md  (1) .py  (4) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 4 additions & 2 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# webthing Changelog

## [Unreleased]
### Added
- Ability to set a base URL path on server.
### Changed
- Things now use `title` rather than `name`.
- Things now require a unique ID in the form of a URI.
### Added
- Ability to set a base URL path on server.
- Support for `id`, `base`, `security`, and `securityDefinitions` keys in thing description.

## [0.11.3] - 2019-04-10
### Changed
Expand Down
16 changes: 12 additions & 4 deletions README.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ First we create a new Thing:

.. code:: python

light = Thing('My Lamp', ['OnOffSwitch', 'Light'], 'A web connected lamp')
light = Thing(
'urn:dev:ops:my-lamp-1234',
'My Lamp',
['OnOffSwitch', 'Light'],
'A web connected lamp'
)

Now we can add the required properties.

Expand Down Expand Up @@ -122,9 +127,12 @@ First we create a new Thing:

.. code:: python

sensor = Thing('My Humidity Sensor',
['MultiLevelSensor'],
'A web connected humidity sensor')
sensor = Thing(
'urn:dev:ops:my-humidity-sensor-1234',
'My Humidity Sensor',
['MultiLevelSensor'],
'A web connected humidity sensor'
)

Then we create and add the appropriate property:

Expand Down
22 changes: 14 additions & 8 deletions example/multiple-things.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ class ExampleDimmableLight(Thing):
"""A dimmable light that logs received commands to stdout."""

def __init__(self):
Thing.__init__(self,
'My Lamp',
['OnOffSwitch', 'Light'],
'A web connected lamp')
Thing.__init__(
self,
'urn:dev:ops:my-lamp-1234',
'My Lamp',
['OnOffSwitch', 'Light'],
'A web connected lamp'
)

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

def __init__(self):
Thing.__init__(self,
'My Humidity Sensor',
['MultiLevelSensor'],
'A web connected humidity sensor')
Thing.__init__(
self,
'urn:dev:ops:my-humidity-sensor-1234',
'My Humidity Sensor',
['MultiLevelSensor'],
'A web connected humidity sensor'
)

self.level = Value(0.0)
self.add_property(
Expand Down
7 changes: 6 additions & 1 deletion example/single-thing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def perform_action(self):


def make_thing():
thing = Thing('My Lamp', ['OnOffSwitch', 'Light'], 'A web connected lamp')
thing = Thing(
'urn:dev:ops:my-lamp-1234',
'My Lamp',
['OnOffSwitch', 'Light'],
'A web connected lamp'
)

thing.add_property(
Property(thing,
Expand Down
22 changes: 22 additions & 0 deletions webthing/server.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ def get(self):
'rel': 'alternate',
'href': '{}{}'.format(ws_href, thing.get_href()),
})
description['base'] = '{}://{}{}'.format(
self.request.protocol,
self.request.headers.get('Host', ''),
thing.get_href()
)
description['securityDefinitions'] = {
'nosec_sc': {
'scheme': 'nosec',
},
}
description['security'] = 'nosec_sc'
descriptions.append(description)

self.write(json.dumps(descriptions))
Expand Down Expand Up @@ -216,6 +227,17 @@ def get(self, thing_id='0'):
'rel': 'alternate',
'href': '{}{}'.format(ws_href, self.thing.get_href()),
})
description['base'] = '{}://{}{}'.format(
self.request.protocol,
self.request.headers.get('Host', ''),
self.thing.get_href()
)
description['securityDefinitions'] = {
'nosec_sc': {
'scheme': 'nosec',
},
}
description['security'] = 'nosec_sc'

self.write(json.dumps(description))
self.finish()
Expand Down
14 changes: 12 additions & 2 deletions webthing/thing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
class Thing:
"""A Web Thing."""

def __init__(self, title, type_=[], description=''):
def __init__(self, id_, title, type_=[], description=''):
"""
Initialize the object.

id_ -- the thing's unique ID - must be a URI
title -- the thing's title
type_ -- the thing's type(s)
description -- description of the thing
"""
if not isinstance(type_, list):
type_ = [type_]

self.id = id_
self.context = 'https://iot.mozilla.org/schemas'
self.type = type_
self.title = title
Expand All @@ -40,8 +42,8 @@ def as_thing_description(self):
Returns the state as a dictionary.
"""
thing = {
'id': self.id,
'title': self.title,
'href': self.href_prefix if self.href_prefix else '/',
'@context': self.context,
'@type': self.type,
'properties': self.get_property_descriptions(),
Expand Down Expand Up @@ -127,6 +129,14 @@ def set_ui_href(self, href):
"""
self.ui_href = href

def get_id(self):
"""
Get the ID of the thing.

Returns the ID as a string.
"""
return self.id

def get_title(self):
"""
Get the title of the thing.
Expand Down

Back | FazBrowse Home | New Git URL