1 from ws4py.client.threadedclient import WebSocketClient
11 _logger = logging.getLogger('arvados.events')
13 class EventClient(WebSocketClient):
14 def __init__(self, url, filters, on_event):
16 if re.match(r'(?i)^(true|1|yes)$',
17 config.get('ARVADOS_API_HOST_INSECURE', 'no')):
18 ssl_options={'cert_reqs': ssl.CERT_NONE}
20 ssl_options={'cert_reqs': ssl.CERT_REQUIRED}
22 super(EventClient, self).__init__(url, ssl_options)
23 self.filters = filters
24 self.on_event = on_event
27 self.send(json.dumps({"method": "subscribe", "filters": self.filters}))
29 def received_message(self, m):
30 self.on_event(json.loads(str(m)))
32 def close_connection(self):
34 self.sock.shutdown(socket.SHUT_RDWR)
39 def subscribe(api, filters, on_event):
42 url = "{}?api_token={}".format(api._rootDesc['websocketUrl'], config.get('ARVADOS_API_TOKEN'))
43 ws = EventClient(url, filters, on_event)