Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if "namespace" not in data:
raise RequestHandlerException('invalid namespace')
if "service" not in data:
raise RequestHandlerException('invalid service')
else:
service = data['service']
if "domain" not in data:
d, s = service.split("/")
if d and s:
domain = d
service = s
else:
raise RequestHandlerException('invalid domain')
else:
domain = data['domain']
if "data" not in data:
service_data = {}
else:
service_data = data['data']
return await self.AD.services.call_service(data['namespace'], domain, service, service_data)
async def call_service(self, data):
if not self.authed:
raise RequestHandlerException('unauthorized')
if "namespace" not in data:
raise RequestHandlerException('invalid namespace')
if "service" not in data:
raise RequestHandlerException('invalid service')
else:
service = data['service']
if "domain" not in data:
d, s = service.split("/")
if d and s:
domain = d
service = s
else:
raise RequestHandlerException('invalid domain')
else:
domain = data['domain']
if "data" not in data:
service_data = {}
else:
async def listen_event(self, data):
if not self.authed:
raise RequestHandlerException('unauthorized')
if "namespace" not in data:
raise RequestHandlerException('invalid namespace')
if "event" not in data:
raise RequestHandlerException('invalid event')
handle = data.get('handle', uuid.uuid4().hex)
if handle in self.subscriptions['event']:
raise RequestHandlerException('handle already exists')
self.subscriptions['event'][handle] = {
"namespace": data['namespace'],
"event": data['event']
}
return handle
async def hello(self, data):
if "client_name" not in data:
raise RequestHandlerException('client_name required')
if self.AD.http.password is None:
self.authed = True
if not self.authed:
await self._auth_data(data)
if not self.authed:
raise RequestHandlerException('authorization failed')
self.access.info("New client %s connected", data["client_name"])
response_data = {
"version": utils.__version__
}
return response_data
async def get_state(self, data):
if not self.authed:
raise RequestHandlerException('unauthorized')
namespace = data.get('namespace', None)
entity_id = data.get('entity_id', None)
if entity_id is not None and namespace is None:
raise RequestHandlerException('entity_id cannoy be set without namespace')
return self.AD.state.get_entity(namespace, entity_id)
async def fire_event(self, data):
if not self.authed:
raise RequestHandlerException('unauthorized')
if "namespace" not in data:
raise RequestHandlerException('invalid namespace')
if "event" not in data:
raise RequestHandlerException('invalid event')
event_data = data.get('data', {})
return await self.AD.events.fire_event(data['namespace'], data['event'], **event_data)
async def listen_event(self, data):
if not self.authed:
raise RequestHandlerException('unauthorized')
if "namespace" not in data:
raise RequestHandlerException('invalid namespace')
if "event" not in data:
raise RequestHandlerException('invalid event')
handle = data.get('handle', uuid.uuid4().hex)
if handle in self.subscriptions['event']:
raise RequestHandlerException('handle already exists')
self.subscriptions['event'][handle] = {
"namespace": data['namespace'],
"event": data['event']
}
return handle
if not hasattr(self, msg['request_type']):
return await self._response_error(msg, 'unavailable request')
fn = getattr(self, msg['request_type'])
if not callable(fn):
return await self._response_error(msg, 'uncallable request')
request_data = msg.get('data', {})
request_id = msg.get('request_id', None)
try:
data = await fn(request_data)
if data is not None or request_id is not None:
return await self._response_success(msg, data)
except RequestHandlerException as e:
return await self._response_error(msg, str(e))
except Exception as e:
await self._response_error(msg, "Unknown error occured, check AppDaemon logs: {}".format(str(e)))
raise
async def cancel_listen_state(self, data):
if not self.authed:
raise RequestHandlerException('unauthorized')
if "handle" not in data:
raise RequestHandlerException('invalid handle')
if data['handle'] not in self.subscriptions['state']:
raise RequestHandlerException('invalid handle')
del self.subscriptions['state'][data['handle']]
return True
async def listen_state(self, data):
if not self.authed:
raise RequestHandlerException('unauthorized')
if "namespace" not in data:
raise RequestHandlerException('invalid namespace')
if "entity_id" not in data:
raise RequestHandlerException('invalid entity_id')
handle = data.get('handle', uuid.uuid4().hex)
if handle in self.subscriptions['state']:
raise RequestHandlerException('handle already exists')
self.subscriptions['state'][handle] = {
"namespace": data['namespace'],
"entity_id": data['entity_id']
}
return handle