kw['loop'] = self.loop
self.addr = url
self.ws = await websockets.connect(url, **kw)
+ self.loop.create_task(self.receiver())
log.info("Driver connected to juju %s", url)
return self
await self.ws.close()
async def recv(self, request_id):
+ if not self.is_open:
+ raise websockets.exceptions.ConnectionClosed(0, 'websocket closed')
return await self.messages.get(request_id)
async def receiver(self):
while self.is_open:
- result = await self.ws.recv()
- if result is not None:
- result = json.loads(result)
- await self.messages.put(result['request-id'], result)
+ try:
+ result = await self.ws.recv()
+ if result is not None:
+ result = json.loads(result)
+ await self.messages.put(result['request-id'], result)
+ except Exception as e:
+ await self.messages.put_all(e)
+ if isinstance(e, websockets.ConnectionClosed):
+ # ConnectionClosed is not really exceptional for us,
+ # but it may be for any pending message listeners
+ return
+ raise
async def rpc(self, msg, encoder=None):
self.__request_id__ += 1
# API Error Response
raise JujuAPIError(result)
- if not 'response' in result:
+ if 'response' not in result:
# This may never happen
return result
client = cls(endpoint, uuid, username, password, cacert, macaroons,
loop)
await client.open()
- client.loop.create_task(client.receiver)
redirect_info = await client.redirect_info()
if not redirect_info: