X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=juju%2Fclient%2Fconnection.py;h=56f9e18fbb1e55c82671c7ac6e1a9f059d6223f7;hb=561a68b42d847d63606cd73a9bdf129590538a1b;hp=9a631ec4a1c98ade475dbac4e72145b95ae17e79;hpb=8a3ccd4c23c41b30126ae548a575233cba11cc4d;p=osm%2FN2VC.git diff --git a/juju/client/connection.py b/juju/client/connection.py index 9a631ec..56f9e18 100644 --- a/juju/client/connection.py +++ b/juju/client/connection.py @@ -28,7 +28,13 @@ class Connection: client = await Connection.connect_current() """ - def __init__(self): + def __init__(self, endpoint, uuid, username, password, cacert=None): + self.endpoint = endpoint + self.uuid = uuid + self.username = username + self.password = password + self.cacert = cacert + self.__request_id__ = 0 self.addr = None self.ws = None @@ -55,14 +61,14 @@ class Connection: result = json.loads(result) return result - async def rpc(self, msg): + async def rpc(self, msg, encoder=None): self.__request_id__ += 1 msg['RequestId'] = self.__request_id__ if'Params' not in msg: msg['Params'] = {} if "Version" not in msg: msg['Version'] = self.facades[msg['Type']] - outgoing = json.dumps(msg, indent=2) + outgoing = json.dumps(msg, indent=2, cls=encoder) await self.ws.send(outgoing) result = await self.recv() log.debug("send %s got %s", msg, result) @@ -70,14 +76,28 @@ class Connection: raise RuntimeError(result) return result + async def clone(self): + """Return a new Connection, connected to the same websocket endpoint + as this one. + + """ + return await Connection.connect( + self.endpoint, + self.uuid, + self.username, + self.password, + self.cacert, + ) + @classmethod async def connect(cls, endpoint, uuid, username, password, cacert=None): url = "wss://{}/model/{}/api".format(endpoint, uuid) - client = cls() + client = cls(endpoint, uuid, username, password, cacert) await client.open(url, cacert) server_info = await client.login(username, password) client.build_facades(server_info['facades']) log.info("Driver connected to juju %s", endpoint) + return client @classmethod