def _cs_request(self, *args, **kwargs): kargs = {} kargs.setdefault('headers', kwargs.get('headers', {})) kargs['headers']['User-Agent'] = self.USER_AGENT if 'body' in kwargs: kargs['body'] = kwargs['body'] args = utils.safe_encode_list(args) kargs = utils.safe_encode_dict(kargs) if self.log_credentials: log_kargs = kargs else: log_kargs = self._strip_credentials(kargs) utils.http_log_req(_logger, args, log_kargs) try: resp, body = self.request(*args, **kargs) except requests.exceptions.SSLError as e: raise exceptions.SslCertificateValidationError(reason=e) except Exception as e: # Wrap the low-level connection error (socket timeout, redirect # limit, decompression error, etc) into our custom high-level # connection exception (it is excepted in the upper layers of code) _logger.debug("throwing ConnectionFailed : %s", e) raise exceptions.ConnectionFailed(reason=e) utils.http_log_resp(_logger, resp, body) if resp.status_code == 401: raise exceptions.Unauthorized(message=body) return resp, body
def _cs_request(self, *args, **kwargs): kargs = {} kargs.setdefault("headers", kwargs.get("headers", {})) kargs["headers"]["User-Agent"] = self.USER_AGENT if "body" in kwargs: kargs["body"] = kwargs["body"] args = utils.safe_encode_list(args) kargs = utils.safe_encode_dict(kargs) if self.log_credentials: log_kargs = kargs else: log_kargs = self._strip_credentials(kargs) utils.http_log_req(_logger, args, log_kargs) try: resp, body = self.request(*args, **kargs) except requests.exceptions.SSLError as e: raise exceptions.SslCertificateValidationError(reason=e) except Exception as e: # Wrap the low-level connection error (socket timeout, redirect # limit, decompression error, etc) into our custom high-level # connection exception (it is excepted in the upper layers of code) _logger.debug("throwing ConnectionFailed : %s", e) raise exceptions.ConnectionFailed(reason=e) utils.http_log_resp(_logger, resp, body) if resp.status_code == 401: raise exceptions.Unauthorized(message=body) return resp, body
def _cs_request(self, *args, **kwargs): kargs = {} kargs.setdefault('headers', kwargs.get('headers', {})) kargs['headers']['User-Agent'] = self.USER_AGENT if 'content_type' in kwargs: kargs['headers']['Content-Type'] = kwargs['content_type'] kargs['headers']['Accept'] = kwargs['content_type'] else: kargs['headers']['Content-Type'] = self.content_type kargs['headers']['Accept'] = self.content_type if 'body' in kwargs: kargs['body'] = kwargs['body'] args = utils.safe_encode_list(args) kargs = utils.safe_encode_dict(kargs) utils.http_log_req(_logger, args, kargs) try: resp, body = self.request(*args, **kargs) except Exception as e: # Wrap the low-level connection error (socket timeout, redirect # limit, decompression error, etc) into our custom high-level # connection exception (it is excepted in the upper layers of code) raise exceptions.ConnectionFailed(reason=e) utils.http_log_resp(_logger, resp, body) status_code = self.get_status_code(resp) if status_code == 401: raise exceptions.Unauthorized(message=body) elif status_code == 403: raise exceptions.Forbidden(message=body) return resp, body
def test_safe_encode_list(self): o = object() unicode_text = u'\u7f51\u7edc' l = ['abc', unicode_text, unicode_text.encode('utf-8'), o] expected = [ 'abc', unicode_text.encode('utf-8'), unicode_text.encode('utf-8'), o ] self.assertEqual(utils.safe_encode_list(l), expected)
def _cs_request(self, *args, **kwargs): kargs = {} kargs.setdefault('headers', kwargs.get('headers', {})) kargs['headers']['User-Agent'] = self.USER_AGENT if 'content_type' in kwargs: kargs['headers']['Content-Type'] = kwargs['content_type'] kargs['headers']['Accept'] = kwargs['content_type'] else: kargs['headers']['Content-Type'] = self.content_type kargs['headers']['Accept'] = self.content_type if 'body' in kwargs: kargs['body'] = kwargs['body'] args = utils.safe_encode_list(args) kargs = utils.safe_encode_dict(kargs) if self.log_credentials: log_kargs = kargs else: log_kargs = self._strip_credentials(kargs) utils.http_log_req(_logger, args, log_kargs) try: resp, body = self.request(*args, **kargs) except httplib2.SSLHandshakeError as e: if not _logger.isEnabledFor(logging.DEBUG): utils.http_log_req(_logger, args, log_kargs, force_logging=True) raise exceptions.SslCertificateValidationError(reason=e) except Exception as e: if not _logger.isEnabledFor(logging.DEBUG): utils.http_log_req(_logger, args, log_kargs, force_logging=True) # Wrap the low-level connection error (socket timeout, redirect # limit, decompression error, etc) into our custom high-level # connection exception (it is excepted in the upper layers of code) _logger.debug("throwing ConnectionFailed : %s", e) raise exceptions.ConnectionFailed(reason=e) finally: # Temporary Fix for gate failures. RPC calls and HTTP requests # seem to be stepping on each other resulting in bogus fd's being # picked up for making http requests self.connections.clear() utils.http_log_resp(_logger, resp, body) status_code = self.get_status_code(resp) if status_code == 401: raise exceptions.Unauthorized(message=body) elif status_code == 403: raise exceptions.Forbidden(message=body) return resp, body
def _cs_request(self, *args, **kwargs): kargs = {} kargs.setdefault('headers', kwargs.get('headers', {})) if 'content_type' in kwargs: kargs['headers']['Content-Type'] = kwargs['content_type'] kargs['headers']['Accept'] = kwargs['content_type'] else: kargs['headers']['Content-Type'] = self.content_type kargs['headers']['Accept'] = self.content_type if self.auth_token: kargs['headers']['X-Auth-Token'] = self.auth_token if 'body' in kwargs: kargs['body'] = kwargs['body'] args = utils.safe_encode_list(args) kargs = utils.safe_encode_dict(kargs) if self.log_credentials: log_kargs = kargs else: log_kargs = self._strip_credentials(kargs) utils.http_log_req(_logger, args, log_kargs) try: resp, body = self.request(*args, **kargs) except httplib2.SSLHandshakeError as e: raise exceptions.SslCertificateValidationError(reason=e) except Exception as e: # Wrap the low-level connection error (socket timeout, redirect # limit, decompression error, etc) into our custom high-level # connection exception (it is excepted in the upper layers of code) _logger.debug("throwing ConnectionFailed : %s", e) raise exceptions.CommunicationError(e) finally: # Temporary Fix for gate failures. RPC calls and HTTP requests # seem to be stepping on each other resulting in bogus fd's being # picked up for making http requests self.connections.clear() # Read body into string if it isn't obviously image data body_str = None if 'content-type' in resp and resp['content-type'] != 'application/octet-stream': body_str = ''.join([chunk for chunk in body]) self.http_log_resp(_logger, resp, body_str) body = body_str else: self.http_log_resp(_logger, resp, body) status_code = self.get_status_code(resp) if status_code == 401: raise exceptions.HTTPUnauthorized(body) elif status_code == 403: error_json = self._extract_error_json(body_str) raise exceptions.Forbidden(error_json.get('faultstring')) elif 400 <= status_code < 600: _logger.warn("Request returned failure status.") error_json = self._extract_error_json(body_str) raise exceptions.from_response( resp, error_json.get('faultstring'), error_json.get('debuginfo'), *args) elif status_code in (301, 302, 305): # Redirected. Reissue the request to the new location. return self._cs_request(resp['location'], args[1], **kwargs) elif status_code == 300: raise exceptions.from_response(resp, *args) return resp, body
def test_safe_encode_list(self): o = object() unicode_text = u"\u7f51\u7edc" l = ["abc", unicode_text, unicode_text.encode("utf-8"), o] expected = ["abc", unicode_text.encode("utf-8"), unicode_text.encode("utf-8"), o] self.assertEqual(utils.safe_encode_list(l), expected)