summaryrefslogtreecommitdiff
path: root/cloudinit/url_helper.py
diff options
context:
space:
mode:
authorharlowja <harlowja@virtualbox.rhel>2013-02-19 22:51:49 -0800
committerharlowja <harlowja@virtualbox.rhel>2013-02-19 22:51:49 -0800
commit6d40d5b2e3da9577d4a2686444d47125e62817fe (patch)
tree3dc28b3e6157513aedb4a21fcabf35aaefaf5e1f /cloudinit/url_helper.py
parent6a6a2f7c337e18d84d7031ba60d5adf6a93256fc (diff)
downloadvyos-cloud-init-6d40d5b2e3da9577d4a2686444d47125e62817fe.tar.gz
vyos-cloud-init-6d40d5b2e3da9577d4a2686444d47125e62817fe.zip
Continue working on integrating requests.
Diffstat (limited to 'cloudinit/url_helper.py')
-rw-r--r--cloudinit/url_helper.py50
1 files changed, 43 insertions, 7 deletions
diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py
index e8330e24..0839e63b 100644
--- a/cloudinit/url_helper.py
+++ b/cloudinit/url_helper.py
@@ -58,6 +58,44 @@ def _cleanurl(url):
return urlunparse(parsed_url)
+class UrlResponse(object):
+ def __init__(self, response):
+ self._response = response
+
+ @property
+ def contents(self):
+ return self._response.content
+
+ @property
+ def url(self):
+ return self._response.url
+
+ @property
+ def ok(self):
+ return self._response.ok
+
+ @property
+ def headers(self):
+ return self._response.headers
+
+ @property
+ def code(self):
+ return self._response.status_code
+
+ def __str__(self):
+ return self.contents
+
+
+class UrlError(IOError):
+ def __init__(self, cause):
+ IOError.__init__(self, str(cause))
+ self.cause = cause
+ if isinstance(cause, exceptions.HTTPError) and cause.response:
+ self.code = cause.response.status_code
+ else:
+ self.code = None
+
+
def readurl(url, data=None, timeout=None, retries=0, sec_between=1,
headers=None, ssl_details=None, check_status=True,
allow_redirects=False):
@@ -76,6 +114,8 @@ def readurl(url, data=None, timeout=None, retries=0, sec_between=1,
if 'cert_file' in ssl_details and 'key_file' in ssl_details:
req_args['cert'] = [ssl_details['cert_file'],
ssl_details['key_file']]
+ elif 'cert_file' in ssl_details:
+ req_args['cert'] = str(ssl_details['cert_file'])
req_args['allow_redirects'] = allow_redirects
req_args['method'] = 'GET'
@@ -126,13 +166,9 @@ def readurl(url, data=None, timeout=None, retries=0, sec_between=1,
# Doesn't seem like we can make it use a different
# subclass for responses, so add our own backward-compat
# attrs
- if not hasattr(r, 'code'):
- setattr(r, 'code', status)
- if not hasattr(r, 'contents'):
- setattr(r, 'contents', contents)
- return r
+ return UrlResponse(r)
except exceptions.RequestException as e:
- excps.append(e)
+ excps.append(UrlError(e))
if i + 1 < manual_tries and sec_between > 0:
LOG.debug("Please wait %s seconds while we wait to try again",
sec_between)
@@ -213,7 +249,7 @@ def wait_for_url(urls, max_wait=None, timeout=None,
e = ValueError(reason)
else:
return url
- except exceptions.RequestException as e:
+ except UrlError as e:
reason = "request error [%s]" % e
except Exception as e:
reason = "unexpected error [%s]" % e