def _http_request(self, url, method, **kwargs): """ Send an http request with the specified characteristics. Wrapper around httplib2.Http.request to handle tasks such as setting headers, JSON encoding/decoding, and error handling. """ url = self.endpoint + url # Copy the kwargs so we can reuse the original in case of redirects kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {})) kwargs['headers'].setdefault('User-Agent', USER_AGENT) if self.auth_token: kwargs['headers'].setdefault('X-Auth-Token', self.auth_token) resp, body = super(HTTPClient, self).request(url, method, **kwargs) self.http_log((url, method,), kwargs, resp, body) if 400 <= resp.status < 600: logger.exception("Request returned failure status.") raise exceptions.from_response(resp, body) elif resp.status in (301, 302, 305): # Redirected. Reissue the request to the new location. return self._http_request(resp['location'], method, **kwargs) return resp, body
def _http_request(self, url, method, **kwargs): """ Send an http request with the specified characteristics. Wrapper around httplib2.Http.request to handle tasks such as setting headers, JSON encoding/decoding, and error handling. """ url = self.endpoint + url # Copy the kwargs so we can reuse the original in case of redirects kwargs['headers'] = copy.deepcopy(kwargs.get('headers', {})) kwargs['headers'].setdefault('User-Agent', USER_AGENT) if self.auth_token: kwargs['headers'].setdefault('X-Auth-Token', self.auth_token) resp, body = super(HTTPClient, self).request(url, method, **kwargs) self.http_log(( url, method, ), kwargs, resp, body) if 400 <= resp.status < 600: logger.exception("Request returned failure status.") raise exceptions.from_response(resp, body) elif resp.status in (301, 302, 305): # Redirected. Reissue the request to the new location. return self._http_request(resp['location'], method, **kwargs) return resp, body
def _http_request(self, url, method, **kwargs): """ Send an http request with the specified characteristics. Wrapper around httplib2.Http.request to handle tasks such as setting headers, JSON encoding/decoding, and error handling. """ # Copy the kwargs so we can reuse the original in case of redirects _kwargs = copy.copy(kwargs) _kwargs.setdefault('headers', kwargs.get('headers', {})) _kwargs['headers']['User-Agent'] = USER_AGENT if 'raw_body' in _kwargs: raw_body = _kwargs.pop('raw_body') if raw_body is not None: _kwargs['headers']['Content-Type'] = 'application/octet-stream' _kwargs['body'] = raw_body elif 'body' in kwargs and kwargs['body'] is not None: _kwargs['headers']['Content-Type'] = 'application/json' _kwargs['body'] = json.dumps(kwargs['body']) resp, body = super(HTTPClient, self).request(url, method, **_kwargs) self.http_log((url, method,), _kwargs, resp, body) if body: try: body = json.loads(body) except ValueError: logger.debug("Could not decode JSON from body: %s" % body) else: logger.debug("No body was returned.") body = None if 400 <= resp.status < 600: logger.exception("Request returned failure status.") raise exceptions.from_response(resp, body) elif resp.status in (301, 302, 305): # Redirected. Reissue the request to the new location. return self._http_request(resp['location'], method, **kwargs) return resp, body