Skip to content

Commit c162233

Browse files
authored
Merge pull request #23 from devicehive/develop
New release 2.0.2
2 parents 35014a6 + 80d3d0e commit c162233

22 files changed

Lines changed: 176 additions & 90 deletions

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ install:
1010
- pip install -r test_requirements.txt
1111
before_install:
1212
- export DEVICE_HIVE_TRANSPORT_URLS='http://playground.dev.devicehive.com/api/rest,ws://playground.dev.devicehive.com/api/websocket'
13-
- openssl aes-256-cbc -K $encrypted_1ea9ebbf5537_key -iv $encrypted_1ea9ebbf5537_iv -in refresh_token.txt.enc -out refresh_token.txt -d
14-
- export DEVICE_HIVE_REFRESH_TOKEN=$(cat refresh_token.txt)
15-
script: pytest -xv tests --transport-urls=$DEVICE_HIVE_TRANSPORT_URLS --refresh-token=$DEVICE_HIVE_REFRESH_TOKEN
13+
- openssl aes-256-cbc -K $encrypted_1ea9ebbf5537_key -iv $encrypted_1ea9ebbf5537_iv -in admin_refresh_token.txt.enc -out admin_refresh_token.txt -d
14+
- export DEVICE_HIVE_ADMIN_REFRESH_TOKEN=$(cat admin_refresh_token.txt)
15+
- openssl aes-256-cbc -K $encrypted_94dc46d8330a_key -iv $encrypted_94dc46d8330a_iv -in user_refresh_token.txt.enc -out user_refresh_token.txt -d
16+
- export DEVICE_HIVE_USER_REFRESH_TOKEN=$(cat user_refresh_token.txt)
17+
script: pytest -xv tests --transport-urls=$DEVICE_HIVE_TRANSPORT_URLS --admin-refresh-token=$DEVICE_HIVE_ADMIN_REFRESH_TOKEN --user-refresh-token=$DEVICE_HIVE_USER_REFRESH_TOKEN

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ If you want to use `Websocket` protocol you need only to specify the url:
117117
url = 'ws://playground.dev.devicehive.com/api/websocket'
118118
```
119119

120-
### Authentication.
120+
### Authentication
121121

122122
There are three ways of initial authentication:
123123

@@ -474,7 +474,7 @@ class SimpleHandler(Handler):
474474
self.api.disconnect()
475475
```
476476

477-
## Extended example:
477+
## Extended example
478478

479479
Here we will create one endpoint which sends notifications and other endpoint
480480
which receives these notifications.

devicehive/api_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ApiHandler(Handler):
1616

1717
def __init__(self, transport, auth, handler_class, handler_args,
1818
handler_kwargs):
19-
Handler.__init__(self, transport)
19+
super(ApiHandler, self).__init__(transport)
2020
self._api = Api(self._transport, auth)
2121
self._handler = handler_class(self._api, *handler_args,
2222
**handler_kwargs)

devicehive/api_request.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from devicehive.api_response import ApiResponseError
33
from devicehive.transports.transport import TransportError
44
import uuid
5+
import logging
6+
7+
8+
logger = logging.getLogger(__name__)
59

610

711
class ApiRequest(object):
@@ -81,10 +85,16 @@ def response_key(self, key):
8185
self._params['response_key'] = key
8286

8387
def execute(self, error_message):
84-
response = self._api.transport.request(self._uuid(), self._action,
85-
self._request.copy(),
88+
uuid = self._uuid()
89+
request = self._request.copy()
90+
logger.debug('Request id: %s. Action: %s. Request: %s.', uuid,
91+
self._action, request)
92+
response = self._api.transport.request(uuid, self._action, request,
8693
**self._params)
8794
api_response = ApiResponse(response, self._params['response_key'])
95+
logger.debug('Response id: %s. Action: %s. Success: %s. Response: %s.',
96+
api_response.id, api_response.action, api_response.success,
97+
api_response.response)
8898
if api_response.success:
8999
return api_response.response
90100
raise ApiResponseError(error_message, self._api.transport.name,
@@ -97,13 +107,13 @@ class AuthApiRequest(ApiRequest):
97107
def execute(self, error_message):
98108
self.header(*self._api.token.auth_header)
99109
try:
100-
return ApiRequest.execute(self, error_message)
110+
return super(AuthApiRequest, self).execute(error_message)
101111
except ApiResponseError as api_response_error:
102112
if api_response_error.code != 401:
103113
raise
104114
self._api.token.auth()
105115
self.header(*self._api.token.auth_header)
106-
return ApiRequest.execute(self, error_message)
116+
return super(AuthApiRequest, self).execute(error_message)
107117

108118

109119
class SubscriptionApiRequest(object):
@@ -170,7 +180,7 @@ class AuthSubscriptionApiRequest(SubscriptionApiRequest):
170180
"""Auth subscription api request class."""
171181

172182
def __init__(self, api):
173-
SubscriptionApiRequest.__init__(self)
183+
super(AuthSubscriptionApiRequest, self).__init__()
174184
auth_header_name, auth_header_value = api.token.auth_header
175185
self._params['headers'][auth_header_name] = auth_header_value
176186
self._params['response_error_handler'] = self.response_error_handler

devicehive/api_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, message, transport_name, code, error):
5454
message = '%s Transport: %s. Code: %s. Error: %s' % (message,
5555
transport_name,
5656
code, error)
57-
Exception.__init__(self, message)
57+
super(ApiResponseError, self).__init__(message)
5858
self._transport_name = transport_name
5959
self._code = code
6060
self._error = error

devicehive/data_formats/json_data_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class JsonDataFormat(DataFormat):
66
"""Json data format class."""
77

88
def __init__(self):
9-
DataFormat.__init__(self, 'json', self.TEXT_DATA_TYPE)
9+
super(JsonDataFormat, self).__init__('json', self.TEXT_DATA_TYPE)
1010

1111
def encode(self, data):
1212
return json.dumps(data)

devicehive/device_hive.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def connect(self, transport_url, **options):
3939
connect_timeout = options.pop('connect_timeout', 30)
4040
max_num_connect = options.pop('max_num_connect', 10)
4141
connect_interval = options.pop('connect_interval', 1)
42+
transport_alive_timeout = options.pop('transport_alive_timeout', 0.01)
4243
self._api_handler_options['auth'] = auth
4344
self._init_transport()
4445
connect_time = time.time()
@@ -47,7 +48,8 @@ def connect(self, transport_url, **options):
4748
if self._transport.connected:
4849
self._transport.disconnect()
4950
self._transport.connect(transport_url, **options)
50-
self._transport.join()
51+
while self._transport.is_alive():
52+
time.sleep(transport_alive_timeout)
5153
exception_info = self._transport.exception_info
5254
if exception_info and not isinstance(exception_info[1],
5355
self._transport.error):

devicehive/transports/http_transport.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ class HttpTransport(Transport):
1111

1212
def __init__(self, data_format_class, data_format_options, handler_class,
1313
handler_options):
14-
Transport.__init__(self, 'http', HttpTransportError, data_format_class,
15-
data_format_options, handler_class, handler_options)
14+
super(HttpTransport, self).__init__('http', HttpTransportError,
15+
data_format_class,
16+
data_format_options, handler_class,
17+
handler_options)
1618
self._url = None
1719
self._options = None
1820
self._events_queue_timeout = None

devicehive/transports/transport.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def disconnect(self):
108108
def join(self, timeout=None):
109109
self._connection_thread.join(timeout)
110110

111+
def is_alive(self):
112+
return self._connection_thread.is_alive()
113+
111114
def send_request(self, request_id, action, request, **params):
112115
raise NotImplementedError
113116

0 commit comments

Comments
 (0)