X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=juju%2Fclient%2Fconnection.py;h=9e8cb8fa01f362683a1b0b09eb65a212ccba33db;hb=5fef7503b13f145adc7cd4ee31c2d684e09a6a85;hp=5ed073f8d61283428af72f61cf45e9886b571b91;hpb=fe19bc72a387b03fb157c873313ecac58b65e338;p=osm%2FN2VC.git diff --git a/juju/client/connection.py b/juju/client/connection.py index 5ed073f..9e8cb8f 100644 --- a/juju/client/connection.py +++ b/juju/client/connection.py @@ -9,9 +9,11 @@ import ssl import string import subprocess import websockets +from http.client import HTTPSConnection import yaml +from juju import tag from juju.errors import JujuAPIError, JujuConnectionError log = logging.getLogger("websocket") @@ -93,6 +95,52 @@ class Connection: raise JujuAPIError(result) return result + def http_headers(self): + """Return dictionary of http headers necessary for making an http + connection to the endpoint of this Connection. + + :return: Dictionary of headers + + """ + if not self.username: + return {} + + creds = u'{}:{}'.format( + tag.user(self.username), + self.password or '' + ) + token = base64.b64encode(creds.encode()) + return { + 'Authorization': 'Basic {}'.format(token.decode()) + } + + def https_connection(self): + """Return an https connection to this Connection's endpoint. + + Returns a 3-tuple containing:: + + 1. The :class:`HTTPSConnection` instance + 2. Dictionary of auth headers to be used with the connection + 3. The root url path (str) to be used for requests. + + """ + endpoint = self.endpoint + host, remainder = endpoint.split(':', 1) + port = remainder + if '/' in remainder: + port, _ = remainder.split('/', 1) + + conn = HTTPSConnection( + host, int(port), + context=self._get_ssl(self.cacert), + ) + + path = ( + "/model/{}".format(self.uuid) + if self.uuid else "" + ) + return conn, self.http_headers(), path + async def clone(self): """Return a new Connection, connected to the same websocket endpoint as this one.