Skip to content

Commit

Permalink
Merge pull request #5 from devicehive/development
Browse files Browse the repository at this point in the history
Release 1.0.3
  • Loading branch information
igor-panteleev authored Apr 10, 2018
2 parents 5476d59 + 7f47b74 commit ad532a5
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 153 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
[![PyPI](https://img.shields.io/pypi/v/devicehive-plugin.svg)](https://pypi.python.org/pypi/devicehive-plugin)
[![Build Status](https://travis-ci.org/devicehive/devicehive-plugin-python.svg?branch=master)](https://travis-ci.org/devicehive/devicehive-plugin-python)

# Devicehive plugin
Expand Down
2 changes: 1 addition & 1 deletion devicehive_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
from .handler import Handler


__version__ = "1.0.2"
__version__ = "1.0.3"
5 changes: 5 additions & 0 deletions devicehive_plugin/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Command(object):
RESULT_KEY = 'result'

def __init__(self, command):
self._raw_data = command
self._device_id = command[self.DEVICE_ID_KEY]
self._id = command[self.ID_KEY]
self._user_id = command[self.USER_ID_KEY]
Expand Down Expand Up @@ -78,3 +79,7 @@ def status(self):
@property
def result(self):
return self._result

@property
def raw_data(self):
return self._raw_data
5 changes: 5 additions & 0 deletions devicehive_plugin/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Notification(object):
TIMESTAMP_KEY = 'timestamp'

def __init__(self, notification):
self._raw_data = notification
self._device_id = notification[self.DEVICE_ID_KEY]
self._id = notification[self.ID_KEY]
self._notification = notification[self.NOTIFICATION_KEY]
Expand All @@ -48,3 +49,7 @@ def parameters(self):
@property
def timestamp(self):
return self._timestamp

@property
def raw_data(self):
return self._raw_data
14 changes: 9 additions & 5 deletions devicehive_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ def connect(self, proxy_endpoint, topic_name, **options):
connect_timeout = options.pop('connect_timeout', 30)
max_num_connect = options.pop('max_num_connect', 10)
connect_interval = options.pop('connect_interval', 1)
credentials = {'login': options.pop('login', None),
'password': options.pop('password', None),
'refresh_token': options.pop('refresh_token', None),
'access_token': options.pop('access_token', None),
'auth_url': options.pop('auth_url', None)}
credentials = {
'login': options.pop('login', None),
'password': options.pop('password', None),
'access_token': options.pop('access_token', None),
'refresh_token': options.pop('refresh_token', None),
'plugin_access_token': options.pop('plugin_access_token', None),
'plugin_refresh_token': options.pop('plugin_refresh_token', None),
'auth_url': options.pop('auth_url', None)
}
api_init = options.pop('api_init', True)
self._api_handler_options['credentials'] = credentials
self._api_handler_options['topic_name'] = topic_name
Expand Down
23 changes: 10 additions & 13 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ def __init__(self, api, handle_connect, handle_event, handle_command_insert,
def handle_connect(self):
if not self._handle_connect:
self.disconnect()
# TODO: remove when race-condition on server side will be fixed
time.sleep(1)
self._handle_connect(self)

def handle_event(self, event):
Expand Down Expand Up @@ -187,27 +185,26 @@ def plugin_api(self):
api.auth()
return api

def run(self, proxy_endpoint, topic_name, handle_connect,
handle_event=None, handle_command_insert=None,
handle_command_update=None, handle_notification=None, data=None,
handle_timeout=60):
def run(self, plugin, handle_connect, handle_event=None,
handle_command_insert=None, handle_command_update=None,
handle_notification=None, data=None, handle_timeout=60):
proxy_endpoint = plugin['proxyEndpoint']
topic_name = plugin['topicName']
access_token = plugin['accessToken']
handler_kwargs = {'handle_connect': handle_connect,
'handle_event': handle_event,
'handle_command_insert': handle_command_insert,
'handle_command_update': handle_command_update,
'handle_notification': handle_notification,
'data': data}
plugin = Plugin(TestHandler, **handler_kwargs)
test_plugin = Plugin(TestHandler, **handler_kwargs)
timeout_timer = threading.Timer(handle_timeout, self._on_handle_timeout,
args=(plugin,))
args=(test_plugin,))
timeout_timer.setDaemon(True)
timeout_timer.start()
credentials = dict(self._credentials, auth_url=self._transport_url)
plugin.connect(proxy_endpoint, topic_name, **credentials)
test_plugin.connect(proxy_endpoint, topic_name,
plugin_access_token=access_token)
timeout_timer.cancel()

if self._is_handle_timeout:
raise TimeoutError('Waited too long for handle.')

# TODO: remove when race-condition on server side will be fixed
time.sleep(1)
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def test_update_plugin(test):
subscribe_notifications=False)

topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']

def handle_connect(handler):
try:
Expand All @@ -84,7 +83,8 @@ def handle_connect(handler):
assert 'command' in notification_filters
handler.disconnect()

test.run(proxy_endpoint, topic_name, handle_connect)
# TODO: uncomment when race condition on server side will be fixed
# test.run(plugin, handle_connect)

name = test.generate_id('u-p', test.PLUGIN_ENTITY)
description = '%s-description' % name
Expand Down
64 changes: 20 additions & 44 deletions tests/test_api_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ def handle_event(handler, event):
description = '%s-description' % name
plugin = plugin_api.create_plugin(name, description,
device_id=data['device'].id)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect, handle_event,
data=data)
plugin_api.remove_plugin(topic_name)
test.run(plugin, handle_connect, handle_event, data=data)
plugin_api.remove_plugin(plugin['topicName'])

# =========================================================================
def handle_connect(handler):
Expand All @@ -82,11 +79,8 @@ def handle_connect(handler):
plugin = plugin_api.create_plugin(name, description,
device_id=data['device'].id,
subscribe_notifications=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect, handle_event,
data=data)
plugin_api.remove_plugin(topic_name)
test.run(plugin, handle_connect, handle_event, data=data)
plugin_api.remove_plugin(plugin['topicName'])

# =========================================================================
def handle_connect(handler):
Expand All @@ -103,11 +97,8 @@ def handle_connect(handler):
plugin = plugin_api.create_plugin(name, description,
device_id=data['device'].id,
subscribe_update_commands=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect, handle_event,
data=data)
plugin_api.remove_plugin(topic_name)
test.run(plugin, handle_connect, handle_event, data=data)
plugin_api.remove_plugin(plugin['topicName'])

# =========================================================================
def handle_connect(handler):
Expand All @@ -124,11 +115,8 @@ def handle_connect(handler):
plugin = plugin_api.create_plugin(name, description,
device_id=data['device'].id,
subscribe_insert_commands=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect, handle_event,
data=data)
plugin_api.remove_plugin(topic_name)
test.run(plugin, handle_connect, handle_event, data=data)
plugin_api.remove_plugin(plugin['topicName'])


def test_subscribe_insert_commands(test):
Expand Down Expand Up @@ -164,11 +152,9 @@ def handle_command_insert(handler, command):
device_id=data['device'].id,
subscribe_update_commands=False,
subscribe_notifications=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect,
test.run(plugin, handle_connect,
handle_command_insert=handle_command_insert, data=data)
plugin_api.remove_plugin(topic_name)
plugin_api.remove_plugin(plugin['topicName'])

# =========================================================================
def handle_connect(handler):
Expand All @@ -183,11 +169,9 @@ def handle_connect(handler):
names=data['command_names'][-1:],
subscribe_update_commands=False,
subscribe_notifications=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect,
test.run(plugin, handle_connect,
handle_command_insert=handle_command_insert, data=data)
plugin_api.remove_plugin(topic_name)
plugin_api.remove_plugin(plugin['topicName'])


def test_subscribe_update_commands(test):
Expand Down Expand Up @@ -231,11 +215,9 @@ def handle_command_update(handler, command):
device_id=data['device'].id,
subscribe_insert_commands=False,
subscribe_notifications=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect,
test.run(plugin, handle_connect,
handle_command_update=handle_command_update, data=data)
plugin_api.remove_plugin(topic_name)
plugin_api.remove_plugin(plugin['topicName'])

# =========================================================================
def handle_connect(handler):
Expand All @@ -250,11 +232,9 @@ def handle_connect(handler):
names=data['command_names'][-1:],
subscribe_insert_commands=False,
subscribe_notifications=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect,
test.run(plugin, handle_connect,
handle_command_update=handle_command_update, data=data)
plugin_api.remove_plugin(topic_name)
plugin_api.remove_plugin(plugin['topicName'])


def test_subscribe_notifications(test):
Expand Down Expand Up @@ -291,11 +271,9 @@ def handle_notification(handler, notification):
device_id=data['device'].id,
subscribe_insert_commands=False,
subscribe_update_commands=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect,
test.run(plugin, handle_connect,
handle_notification=handle_notification, data=data)
plugin_api.remove_plugin(topic_name)
plugin_api.remove_plugin(plugin['topicName'])

# =========================================================================
def handle_connect(handler):
Expand All @@ -310,8 +288,6 @@ def handle_connect(handler):
names=data['notification_names'][-1:],
subscribe_insert_commands=False,
subscribe_update_commands=False)
topic_name = plugin['topicName']
proxy_endpoint = plugin['proxyEndpoint']
test.run(proxy_endpoint, topic_name, handle_connect,
test.run(plugin, handle_connect,
handle_notification=handle_notification, data=data)
plugin_api.remove_plugin(topic_name)
plugin_api.remove_plugin(plugin['topicName'])
Loading

0 comments on commit ad532a5

Please sign in to comment.