diff options
author | Jenkins <jenkins@review.openstack.org> | 2013-12-02 14:57:51 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2013-12-02 14:57:51 +0000 |
commit | 35e20cbfb27b7df86a88ca6f106ee4d690fb2679 (patch) | |
tree | b1824d28d1e1ab5b17ee2e469a3f2c9f8ec23406 | |
parent | 125745366085b0072ad99bddc58f978978c2f96d (diff) | |
parent | a587581e02d6f0c77cf1f38d1948d7fdb1424405 (diff) | |
download | python-ceilometerclient-35e20cbfb27b7df86a88ca6f106ee4d690fb2679.tar.gz |
Merge "Add HTTP proxy support to ceilometer client"1.0.8
-rw-r--r-- | ceilometerclient/common/http.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/ceilometerclient/common/http.py b/ceilometerclient/common/http.py index 1c6f29b..a02c881 100644 --- a/ceilometerclient/common/http.py +++ b/ceilometerclient/common/http.py @@ -47,6 +47,7 @@ class HTTPClient(object): self.endpoint = endpoint self.auth_token = kwargs.get('token') self.connection_params = self.get_connection_params(endpoint, **kwargs) + self.proxy_url = self.get_proxy_url() @staticmethod def get_connection_params(endpoint, **kwargs): @@ -73,8 +74,13 @@ class HTTPClient(object): def get_connection(self): _class = self.connection_params[0] try: - return _class(*self.connection_params[1][0:2], - **self.connection_params[2]) + if self.proxy_url: + proxy_parts = urlutils.urlparse(self.proxy_url) + return _class(proxy_parts.hostname, proxy_parts.port, + **self.connection_params[2]) + else: + return _class(*self.connection_params[1][0:2], + **self.connection_params[2]) except httplib.InvalidURL: raise exc.InvalidEndpoint() @@ -136,7 +142,10 @@ class HTTPClient(object): conn = self.get_connection() try: - conn_url = self._make_connection_url(url) + if self.proxy_url: + conn_url = self.endpoint + self._make_connection_url(url) + else: + conn_url = self._make_connection_url(url) conn.request(method, conn_url, **kwargs) resp = conn.getresponse() except socket.gaierror as e: @@ -201,6 +210,15 @@ class HTTPClient(object): 'application/octet-stream') return self._http_request(url, method, **kwargs) + def get_proxy_url(self): + scheme = urlutils.urlparse(self.endpoint).scheme + if scheme == 'https': + return os.environ.get('https_proxy') + elif scheme == 'http': + return os.environ.get('http_proxy') + msg = 'Unsupported scheme: %s' % scheme + raise exc.InvalidEndpoint(msg) + class VerifiedHTTPSConnection(httplib.HTTPSConnection): """httplib-compatibile connection using client-side SSL authentication |