1 from ws4py.client.threadedclient import WebSocketClient
11 class EventClient(WebSocketClient):
12 def __init__(self, url, filters, on_event):
14 if re.match(r'(?i)^(true|1|yes)$',
15 config.get('ARVADOS_API_HOST_INSECURE', 'no')):
16 ssl_options={'cert_reqs': ssl.CERT_NONE}
18 ssl_options={'cert_reqs': ssl.CERT_REQUIRED}
20 super(EventClient, self).__init__(url, ssl_options)
21 self.filters = filters
22 self.on_event = on_event
25 self.send(json.dumps({"method": "subscribe", "filters": self.filters}))
27 def received_message(self, m):
28 self.on_event(json.loads(str(m)))
30 def close_connection(self):
32 self.sock.shutdown(socket.SHUT_RDWR)
37 def subscribe(api, filters, on_event):
40 url = "{}?api_token={}".format(api._rootDesc['websocketUrl'], config.get('ARVADOS_API_TOKEN'))
41 ws = EventClient(url, filters, on_event)