~avishai-ish-shalom/cloud-init/chef-refactor

« back to all changes in this revision

Viewing changes to cloudinit/url_helper.py

  • Committer: Scott Moser
  • Date: 2013-04-25 18:03:16 UTC
  • mfrom: (811.1.2 trunk.lp1172742)
  • Revision ID: smoser@ubuntu.com-20130425180316-ekroe007n90vxuwf
Fix issue in readurl with requests, support passing through headers_cb

This adds the 'headers_cb' option to pass all the way through to readurl.
It also makes requests for user-data not retry (as a 404 indicates "no
user-data").

Show diffs side-by-side

added added

removed removed

Lines of Context:
102
102
 
103
103
 
104
104
def readurl(url, data=None, timeout=None, retries=0, sec_between=1,
105
 
            headers=None, ssl_details=None, check_status=True,
106
 
            allow_redirects=True):
 
105
            headers=None, headers_cb=None, ssl_details=None,
 
106
            check_status=True, allow_redirects=True):
107
107
    url = _cleanurl(url)
108
108
    req_args = {
109
109
        'url': url,
149
149
        headers = {
150
150
            'User-Agent': 'Cloud-Init/%s' % (version.version_string()),
151
151
        }
152
 
    req_args['headers'] = headers
153
 
    LOG.debug("Attempting to open '%s' with %s configuration", url, req_args)
 
152
    if not headers_cb:
 
153
        def _cb(url):
 
154
            return headers
 
155
        headers_cb = _cb
 
156
 
154
157
    if data:
155
158
        # Do this after the log (it might be large)
156
159
        req_args['data'] = data
161
164
    # doesn't handle sleeping between tries...
162
165
    for i in range(0, manual_tries):
163
166
        try:
 
167
            req_args['headers'] = headers_cb(url)
 
168
            LOG.debug("[%s/%s] open '%s' with %s configuration", i,
 
169
                      manual_tries, url,
 
170
                      {k: req_args[k] for k in req_args if k != 'data'})
 
171
 
164
172
            r = requests.request(**req_args)
165
173
            if check_status:
166
174
                r.raise_for_status()  # pylint: disable=E1103
174
182
        except exceptions.RequestException as e:
175
183
            if (isinstance(e, (exceptions.HTTPError))
176
184
                and hasattr(e, 'response')  # This appeared in v 0.10.8
177
 
                and e.response):
 
185
                and hasattr(e.response, 'status_code')):
178
186
                excps.append(UrlError(e, code=e.response.status_code,
179
187
                                      headers=e.response.headers))
180
188
            else: