def remove(self): self._ensure_exists() remove_subscription_api_request = RemoveSubscriptionApiRequest() remove_subscription_api_request.subscription_id(self._id) api_request = ApiRequest(self._api) api_request.action('%s/unsubscribe' % self._get_subscription_type()) api_request.set('subscriptionId', self._id) api_request.remove_subscription_request( remove_subscription_api_request) api_request.execute('Unsubscribe failure.') self._id = None
def unsubscribe_notifications(self): self._ensure_exists() action = 'notification/insert' self._ensure_subscription_exists(action) subscription_id = self._subscription_id(action) remove_subscription_api_request = RemoveSubscriptionApiRequest() remove_subscription_api_request.subscription_id(subscription_id) api_request = ApiRequest(self._api) api_request.action('notification/unsubscribe') api_request.set('subscriptionId', subscription_id) api_request.remove_subscription_request( remove_subscription_api_request) api_request.execute('Unsubscribe notifications failure.') self._remove_subscription(action, subscription_id)
def unsubscribe_update_commands(self): self._ensure_exists() action = 'command/update' self._ensure_subscription_exists(action) subscription_id = self._subscription_id(action) remove_subscription_api_request = RemoveSubscriptionApiRequest() remove_subscription_api_request.subscription_id(subscription_id) api_request = ApiRequest(self._api) api_request.action('command/unsubscribe') api_request.set('subscriptionId', subscription_id) api_request.remove_subscription_request( remove_subscription_api_request) api_request.execute('Unsubscribe update commands failure.') self._remove_subscription(action, subscription_id)
def _auth(self): api_request = ApiRequest(self._api) if not api_request.websocket_transport: return api_request.action('authenticate') api_request.set('token', self._access_token) api_request.execute('Authentication failure.')
def get_info(self): api_request = ApiRequest(self) api_request.url('info') api_request.action('server/info') api_request.response_key('info') info = api_request.execute('Info get failure.') return { 'api_version': info['apiVersion'], 'server_timestamp': info['serverTimestamp'], 'rest_server_url': info.get('restServerUrl'), 'websocket_server_url': info.get('webSocketServerUrl') }
def unsubscribe_notifications(self, device_ids): action = 'notification/insert' self.ensure_subscription_exists(action, device_ids) subscription_ids, subscription_calls = self._unsubscribe( action, device_ids) for subscription_id in subscription_ids: remove_subscription_api_request = RemoveSubscriptionApiRequest() remove_subscription_api_request.subscription_id(subscription_id) api_request = ApiRequest(self) api_request.action('notification/unsubscribe') api_request.set('subscriptionId', subscription_id) api_request.remove_subscription_request( remove_subscription_api_request) api_request.execute('Unsubscribe notifications failure.') self.remove_subscription(action, subscription_id) timestamp = self.get_info()['server_timestamp'] for subscription_call in subscription_calls: subscription_call['timestamp'] = timestamp self.subscribe_notifications(**subscription_call)
def _subscribe_update_commands(self, device_id=None, network_ids=(), device_type_ids=(), names=(), timestamp=None): action = 'command/update' join_names = ','.join(map(str, names)) join_network_ids = ','.join(map(str, network_ids)) join_device_type_ids = ','.join(map(str, device_type_ids)) if not timestamp: timestamp = self.server_timestamp auth_subscription_api_request = AuthSubscriptionApiRequest(self) auth_subscription_api_request.action(action) auth_subscription_api_request.url('device/command/poll') auth_subscription_api_request.param('returnUpdatedCommands', True) auth_subscription_api_request.param('deviceId', device_id) auth_subscription_api_request.param('networkIds', join_network_ids) auth_subscription_api_request.param('deviceTypeIds', join_device_type_ids) auth_subscription_api_request.param('names', join_names) auth_subscription_api_request.param('timestamp', timestamp) auth_subscription_api_request.response_timestamp_key('lastUpdated') auth_subscription_api_request.response_key('command') api_request = ApiRequest(self) api_request.action('command/subscribe') api_request.set('returnUpdatedCommands', True) api_request.set('deviceId', device_id) api_request.set('networkIds', network_ids) api_request.set('deviceTypeIds', device_type_ids) api_request.set('names', names) api_request.set('timestamp', timestamp) api_request.subscription_request(auth_subscription_api_request) return api_request.execute('Subscribe update commands failure.')
def get_cluster_info(self): api_request = ApiRequest(self) api_request.url('info/config/cluster') api_request.action('cluster/info') api_request.response_key('clusterInfo') return api_request.execute('Cluster info get failure.')
def _subscribe_notifications(self, device_id=None, network_ids=(), device_type_ids=(), names=(), timestamp=None): action = 'notification/insert' join_names = ','.join(map(str, names)) join_network_ids = ','.join(map(str, network_ids)) join_device_type_ids = ','.join(map(str, device_type_ids)) if not timestamp: timestamp = self.server_timestamp auth_subscription_api_request = AuthSubscriptionApiRequest(self) auth_subscription_api_request.action(action) auth_subscription_api_request.url('device/notification/poll') auth_subscription_api_request.param('deviceId', device_id) auth_subscription_api_request.param('networkIds', join_network_ids) auth_subscription_api_request.param('deviceTypeIds', join_device_type_ids) auth_subscription_api_request.param('names', join_names) auth_subscription_api_request.param('timestamp', timestamp) auth_subscription_api_request.response_key('notification') api_request = ApiRequest(self) api_request.action('notification/subscribe') api_request.set('deviceId', device_id) api_request.set('networkIds', network_ids) api_request.set('deviceTypeIds', device_type_ids) api_request.set('names', names) api_request.set('timestamp', timestamp) api_request.subscription_request(auth_subscription_api_request) return api_request.execute('Subscribe notifications failure.')
def subscribe_notifications(self, names=None, timestamp=None): self._ensure_exists() action = 'notification/insert' self._ensure_subscription_not_exist(action) join_names = ','.join(names) if names else None if not timestamp: timestamp = self._api.server_timestamp auth_subscription_api_request = AuthSubscriptionApiRequest(self._api) auth_subscription_api_request.action(action) auth_subscription_api_request.url( 'device/{deviceId}/notification/poll', deviceId=self._id) auth_subscription_api_request.param('names', join_names) auth_subscription_api_request.param('timestamp', timestamp) auth_subscription_api_request.response_key('notification') api_request = ApiRequest(self._api) api_request.action('notification/subscribe') api_request.set('deviceId', self._id) api_request.set('names', names) api_request.set('timestamp', timestamp) api_request.subscription_request(auth_subscription_api_request) subscription = api_request.execute('Subscribe notifications failure.') self._subscription(action, subscription['subscriptionId'], names)
def subscribe_update_commands(self, names=None, timestamp=None): self._ensure_exists() action = 'command/update' self._ensure_subscription_not_exist(action) join_names = ','.join(names) if names else None if not timestamp: timestamp = self._api.server_timestamp auth_subscription_api_request = AuthSubscriptionApiRequest(self._api) auth_subscription_api_request.action(action) auth_subscription_api_request.url('device/{deviceId}/command/poll', deviceId=self._id) auth_subscription_api_request.param('returnUpdatedCommands', True) auth_subscription_api_request.param('names', join_names) auth_subscription_api_request.param('timestamp', timestamp) auth_subscription_api_request.response_timestamp_key('lastUpdated') auth_subscription_api_request.response_key('command') api_request = ApiRequest(self._api) api_request.action('command/subscribe') api_request.set('returnUpdatedCommands', True) api_request.set('deviceId', self._id) api_request.set('names', names) api_request.set('timestamp', timestamp) api_request.subscription_request(auth_subscription_api_request) subscription = api_request.execute( 'Subscribe update commands failure.') self._subscription(action, subscription['subscriptionId'], names)
def refresh(self): if not self._refresh_token: raise TokenError('Can\'t refresh token without "refresh_token"') api_request = ApiRequest(self._api) api_request.method('POST') api_request.url('token/refresh') api_request.action('token/refresh') api_request.set('refreshToken', self._refresh_token) tokens = api_request.execute('Token refresh failure.') self._access_token = tokens['accessToken']
def _tokens(self): api_request = ApiRequest(self._api) api_request.method('POST') api_request.url('token') api_request.action('token') api_request.set('login', self._login) api_request.set('password', self._password) tokens = api_request.execute('Login failure.') self._refresh_token = tokens['refreshToken'] self._access_token = tokens['accessToken']
def subscribe_insert_commands(self, device_ids, names=None, timestamp=None): action = 'command/insert' self.ensure_subscription_not_exist(action, device_ids) join_device_ids = ','.join(device_ids) join_names = ','.join(names) if names else None if not timestamp: timestamp = self.server_timestamp auth_subscription_api_request = AuthSubscriptionApiRequest(self) auth_subscription_api_request.action(action) auth_subscription_api_request.url('device/command/poll') auth_subscription_api_request.param('deviceIds', join_device_ids) auth_subscription_api_request.param('names', join_names) auth_subscription_api_request.param('timestamp', timestamp) auth_subscription_api_request.response_key('command') api_request = ApiRequest(self) api_request.action('command/subscribe') api_request.set('deviceIds', device_ids) api_request.set('names', names) api_request.set('timestamp', timestamp) api_request.subscription_request(auth_subscription_api_request) subscription = api_request.execute( 'Subscribe insert commands failure.') subscription_id = subscription['subscriptionId'] self.subscription(action, subscription_id, device_ids, names)
def refresh(self): api_request = ApiRequest(self._api) api_request.method('POST') api_request.url('token/refresh') api_request.action('token/refresh') api_request.set('refreshToken', self._refresh_token) tokens = api_request.execute('Token refresh failure.') self._access_token = tokens['accessToken']