def request(self, method, url, extra_headers=False, headers=None, body=None): # TODO(oomichi): This translation is just for avoiding a single # huge patch to migrate rest_client module to tempest-lib. # Ideally(in the future), we need to remove this translation and # replace each API tests with tempest-lib's exceptions. try: return super(ServiceClient, self).request( method, url, extra_headers=extra_headers, headers=headers, body=body) except lib_exceptions.Unauthorized as ex: raise exceptions.Unauthorized(ex) except lib_exceptions.NotFound as ex: raise exceptions.NotFound(ex) except lib_exceptions.BadRequest as ex: raise exceptions.BadRequest(ex) except lib_exceptions.Conflict as ex: raise exceptions.Conflict(ex) except lib_exceptions.OverLimit as ex: raise exceptions.OverLimit(ex) # TODO(oomichi): This is just a workaround for failing gate tests # when separating Forbidden from Unauthorized in tempest-lib. # We will need to remove this translation and replace negative tests # with lib_exceptions.Forbidden in the future. except lib_exceptions.Forbidden as ex: raise exceptions.Unauthorized(ex)
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" self.http_obj = http.ClosingHttp() if headers is None: headers = {} # Authorize the request req_url, req_headers, req_body = self.auth_provider.auth_request( method=method, url=url, headers=headers, body=body, filters=self.filters) self._log_request(method, req_url, headers, body) # use original body resp, resp_body = self.http_obj.request(req_url, method, headers=req_headers, body=req_body) self._log_response(resp, resp_body) if resp.status == 401 or resp.status == 403: raise exceptions.Unauthorized() return resp, resp_body
def request(self, method, url, extra_headers=False, headers=None, body=None): """A simple HTTP request interface.""" if headers is None: headers = self.get_headers(accept_type="json") elif extra_headers: try: headers.update(self.get_headers(accept_type="json")) except (ValueError, TypeError): headers = self.get_headers(accept_type="json") resp, resp_body = self.raw_request(url, method, headers=headers, body=body) self._log_request(method, url, resp) if resp.status in [401, 403]: resp_body = json.loads(resp_body) raise exceptions.Unauthorized(resp_body['error']['message']) elif resp.status not in [200, 201]: raise exceptions.IdentityError('Unexpected status code {0}'.format( resp.status)) if isinstance(resp_body, str): resp_body = json.loads(resp_body) return resp, resp_body
def request(self, method, url, extra_headers=False, headers=None, body=None): """A simple HTTP request interface.""" self.http_obj = http.ClosingHttp() if headers is None: headers = {} elif extra_headers: try: headers.update(self.get_headers()) except (ValueError, TypeError): headers = {} # Authorize the request req_url, req_headers, req_body = self.auth_provider.auth_request( method=method, url=url, headers=headers, body=body, filters=self.filters) # use original body resp, resp_body = self.http_obj.request(req_url, method, headers=req_headers, body=req_body) self._log_request(method, req_url, resp) if resp.status == 401 or resp.status == 403: raise exceptions.Unauthorized() return resp, resp_body
def request(self, method, url, extra_headers=False, headers=None, body=None): """A simple HTTP request interface.""" if headers is None: # Always accept 'json', for xml token client too. # Because XML response is not easily # converted to the corresponding JSON one headers = self.get_headers(accept_type="json") elif extra_headers: try: headers.update(self.get_headers(accept_type="json")) except (ValueError, TypeError): headers = self.get_headers(accept_type="json") resp, resp_body = self.http_obj.request(url, method, headers=headers, body=body) self._log_request(method, url, resp) if resp.status in [401, 403]: resp_body = json.loads(resp_body) raise exceptions.Unauthorized(resp_body['error']['message']) elif resp.status not in [200, 201, 204]: raise exceptions.IdentityError('Unexpected status code {0}'.format( resp.status)) return resp, json.loads(resp_body)
def request(self, method, url, headers=None, body=None, depth=0): """A simple HTTP request interface.""" self.http_obj = httplib2.Http() if headers == None: headers = {} if (self.token == None): return None, None headers['X-Auth-Token'] = self.token req_url = url resp, resp_body = self.http_obj.request(req_url, method, headers=headers, body=body) if resp.status == 401: raise exceptions.Unauthorized() if resp.status == 404: raise exceptions.NotFound(resp_body) if resp.status == 400: resp_body = json.loads(resp_body) raise exceptions.BadRequest(resp_body['badRequest']['message']) if resp.status == 409: resp_body = json.loads(resp_body) raise exceptions.Duplicate(resp_body) if resp.status == 413: resp_body = json.loads(resp_body) if 'overLimit' in resp_body: raise exceptions.OverLimit(resp_body['overLimit']['message']) elif depth < MAX_RECURSION_DEPTH: delay = resp['Retry-After'] if 'Retry-After' in resp else 60 time.sleep(int(delay)) return self.request(method, url, headers, body, depth + 1) else: raise exceptions.RateLimitExceeded( message=resp_body['overLimitFault']['message'], details=resp_body['overLimitFault']['details']) if resp.status in (500, 501): resp_body = json.loads(resp_body) #I'm seeing both computeFault and cloudServersFault come back. #Will file a bug to fix, but leave as is for now. if 'cloudServersFault' in resp_body: message = resp_body['cloudServersFault']['message'] else: message = resp_body['computeFault']['message'] raise exceptions.ComputeFault(message) if resp.status >= 400: resp_body = json.loads(resp_body) raise exceptions.TempestException(str(resp.status)) return resp, resp_body
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" dscv = self.config.identity.disable_ssl_certificate_validation self.http_obj = httplib2.Http(disable_ssl_certificate_validation=dscv) if headers is None: headers = {} self._log_request(method, url, headers, body) resp, resp_body = self.http_obj.request(url, method, headers=headers, body=body) self._log_response(resp, resp_body) if resp.status in (401, 403): resp_body = json.loads(resp_body) raise exceptions.Unauthorized(resp_body['error']['message']) return resp, resp_body
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" self.http_obj = httplib2.Http() if headers is None: headers = {} resp, resp_body = self.http_obj.request(url, method, headers=headers, body=body) if resp.status in (401, 403): resp_body = json.loads(resp_body) raise exceptions.Unauthorized(resp_body['error']['message']) return resp, resp_body
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" self._log_request(method, url, headers, body) resp, resp_body = self.http_obj.request(url, method, headers=headers, body=body) self._log_response(resp, resp_body) if resp.status in [401, 403]: resp_body = json.loads(resp_body) raise exceptions.Unauthorized(resp_body['error']['message']) elif resp.status not in [200, 201, 204]: raise exceptions.IdentityError('Unexpected status code {0}'.format( resp.status)) return resp, json.loads(resp_body)
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" self.http_obj = httplib2.Http() if headers is None: headers = {} if self.base_url is None: self._set_auth() req_url = "%s/%s" % (self.base_url, url) self._log_request(method, req_url, headers, body) resp, resp_body = self.http_obj.request(req_url, method, headers=headers, body=body) self._log_response(resp, resp_body) if resp.status == 401 or resp.status == 403: raise exceptions.Unauthorized() return resp, resp_body
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" dscv = self.config.identity.disable_ssl_certificate_validation self.http_obj = httplib2.Http(disable_ssl_certificate_validation=dscv) if headers is None: headers = {} if self.base_url is None: self._set_auth() req_url = "%s/%s" % (self.base_url, url) self._log_request(method, req_url, headers, body) resp, resp_body = self.http_obj.request(req_url, method, headers=headers, body=body) self._log_response(resp, resp_body) if resp.status == 401 or resp.status == 403: raise exceptions.Unauthorized() return resp, resp_body
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" # Send XML, accept JSON. XML response is not easily # converted to the corresponding JSON one headers['Accept'] = 'application/json' self._log_request(method, url, headers, body) resp, resp_body = self.http_obj.request(url, method, headers=headers, body=body) self._log_response(resp, resp_body) if resp.status in [401, 403]: resp_body = json.loads(resp_body) raise exceptions.Unauthorized(resp_body['error']['message']) elif resp.status not in [200, 201, 204]: raise exceptions.IdentityError('Unexpected status code {0}'.format( resp.status)) return resp, json.loads(resp_body)
def request(self, method, url, extra_headers=False, headers=None, body=None): # TODO(oomichi): This translation is just for avoiding a single # huge patch to migrate rest_client module to tempest-lib. # Ideally(in the future), we need to remove this translation and # replace each API tests with tempest-lib's exceptions. try: return super(ServiceClient, self).request(method, url, extra_headers=extra_headers, headers=headers, body=body) except lib_exceptions.Unauthorized as ex: raise exceptions.Unauthorized(ex) except lib_exceptions.NotFound as ex: raise exceptions.NotFound(ex) except lib_exceptions.BadRequest as ex: raise exceptions.BadRequest(ex) except lib_exceptions.Conflict as ex: raise exceptions.Conflict(ex) except lib_exceptions.OverLimit as ex: raise exceptions.OverLimit(ex) except lib_exceptions.RateLimitExceeded as ex: raise exceptions.RateLimitExceeded(ex) except lib_exceptions.InvalidContentType as ex: raise exceptions.InvalidContentType(ex) except lib_exceptions.UnprocessableEntity as ex: raise exceptions.UnprocessableEntity(ex) except lib_exceptions.InvalidHTTPResponseBody as ex: raise exceptions.InvalidHTTPResponseBody(ex) except lib_exceptions.NotImplemented as ex: raise exceptions.NotImplemented(ex) except lib_exceptions.ServerFault as ex: raise exceptions.ServerFault(ex) except lib_exceptions.UnexpectedResponseCode as ex: raise exceptions.UnexpectedResponseCode(ex)
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" dscv = CONF.identity.disable_ssl_certificate_validation self.http_obj = http.ClosingHttp( disable_ssl_certificate_validation=dscv) if headers is None: headers = {} # Authorize the request req_url, req_headers, req_body = self.auth_provider.auth_request( method=method, url=url, headers=headers, body=body, filters=self.filters ) # Use original method self._log_request(method, req_url, headers, body) resp, resp_body = self.http_obj.request(req_url, method, headers=req_headers, body=req_body) self._log_response(resp, resp_body) if resp.status == 401 or resp.status == 403: raise exceptions.Unauthorized() return resp, resp_body
def request(self, method, url, headers=None, body=None): """A simple HTTP request interface.""" if headers is None: # Always accept 'json', for TokenClientXML too. # Because XML response is not easily # converted to the corresponding JSON one headers = self.get_headers(accept_type="json") self._log_request(method, url, headers, body) resp, resp_body = self.http_obj.request(url, method, headers=headers, body=body) self._log_response(resp, resp_body) if resp.status in [401, 403]: resp_body = json.loads(resp_body) raise exceptions.Unauthorized(resp_body['error']['message']) elif resp.status not in [200, 201]: raise exceptions.IdentityError('Unexpected status code {0}'.format( resp.status)) if isinstance(resp_body, str): resp_body = json.loads(resp_body) return resp, resp_body
def _error_checker(self, method, url, headers, body, resp, resp_body): # NOTE(mtreinish): Check for httplib response from glance_http. The # object can't be used here because importing httplib breaks httplib2. # If another object from a class not imported were passed here as # resp this could possibly fail if str(type(resp)) == "<type 'instance'>": ctype = resp.getheader('content-type') else: try: ctype = resp['content-type'] # NOTE(mtreinish): Keystone delete user responses doesn't have a # content-type header. (They don't have a body) So just pretend it # is set. except KeyError: ctype = 'application/json' # It is not an error response if resp.status < 400: return JSON_ENC = ['application/json', 'application/json; charset=utf-8'] # NOTE(mtreinish): This is for compatibility with Glance and swift # APIs. These are the return content types that Glance api v1 # (and occasionally swift) are using. TXT_ENC = [ 'text/plain', 'text/html', 'text/html; charset=utf-8', 'text/plain; charset=utf-8' ] XML_ENC = ['application/xml', 'application/xml; charset=utf-8'] if ctype.lower() in JSON_ENC or ctype.lower() in XML_ENC: parse_resp = True elif ctype.lower() in TXT_ENC: parse_resp = False else: raise exceptions.InvalidContentType(str(resp.status)) if resp.status == 401 or resp.status == 403: raise exceptions.Unauthorized(resp_body) if resp.status == 404: raise exceptions.NotFound(resp_body) if resp.status == 400: if parse_resp: resp_body = self._parse_resp(resp_body) raise exceptions.BadRequest(resp_body) if resp.status == 409: if parse_resp: resp_body = self._parse_resp(resp_body) raise exceptions.Conflict(resp_body) if resp.status == 413: if parse_resp: resp_body = self._parse_resp(resp_body) if self.is_absolute_limit(resp, resp_body): raise exceptions.OverLimit(resp_body) else: raise exceptions.RateLimitExceeded(resp_body) if resp.status == 422: if parse_resp: resp_body = self._parse_resp(resp_body) raise exceptions.UnprocessableEntity(resp_body) if resp.status in (500, 501): message = resp_body if parse_resp: try: resp_body = self._parse_resp(resp_body) except ValueError: # If response body is a non-json string message. # Use resp_body as is and raise InvalidResponseBody # exception. raise exceptions.InvalidHTTPResponseBody(message) else: if isinstance(resp_body, dict): # I'm seeing both computeFault # and cloudServersFault come back. # Will file a bug to fix, but leave as is for now. if 'cloudServersFault' in resp_body: message = resp_body['cloudServersFault']['message'] elif 'computeFault' in resp_body: message = resp_body['computeFault']['message'] elif 'error' in resp_body: # Keystone errors message = resp_body['error']['message'] raise exceptions.IdentityError(message) elif 'message' in resp_body: message = resp_body['message'] else: message = resp_body raise exceptions.ServerFault(message) if resp.status >= 400: raise exceptions.UnexpectedResponseCode(str(resp.status))
def request(self, method, url, headers=None, body=None, depth=0): """A simple HTTP request interface.""" if (self.token is None) or (self.base_url is None): self._set_auth() self.http_obj = httplib2.Http() if headers == None: headers = {} headers['X-Auth-Token'] = self.token req_url = "%s/%s" % (self.base_url, url) resp, resp_body = self.http_obj.request(req_url, method, headers=headers, body=body) if resp.status == 401 or resp.status == 403: self._log(req_url, body, resp, resp_body) raise exceptions.Unauthorized() if resp.status == 404: self._log(req_url, body, resp, resp_body) raise exceptions.NotFound(resp_body) if resp.status == 400: resp_body = self._parse_resp(resp_body) self._log(req_url, body, resp, resp_body) raise exceptions.BadRequest(resp_body) if resp.status == 409: resp_body = self._parse_resp(resp_body) self._log(req_url, body, resp, resp_body) raise exceptions.Duplicate(resp_body) if resp.status == 413: resp_body = self._parse_resp(resp_body) self._log(req_url, body, resp, resp_body) if 'overLimit' in resp_body: raise exceptions.OverLimit(resp_body['overLimit']['message']) elif 'limit' in resp_body['message']: raise exceptions.OverLimit(resp_body['message']) elif depth < MAX_RECURSION_DEPTH: delay = resp['Retry-After'] if 'Retry-After' in resp else 60 time.sleep(int(delay)) return self.request(method, url, headers, body, depth + 1) else: raise exceptions.RateLimitExceeded( message=resp_body['overLimitFault']['message'], details=resp_body['overLimitFault']['details']) if resp.status in (500, 501): resp_body = self._parse_resp(resp_body) self._log(req_url, body, resp, resp_body) #I'm seeing both computeFault and cloudServersFault come back. #Will file a bug to fix, but leave as is for now. if 'cloudServersFault' in resp_body: message = resp_body['cloudServersFault']['message'] elif 'computeFault' in resp_body: message = resp_body['computeFault']['message'] elif 'error' in resp_body: # Keystone errors message = resp_body['error']['message'] raise exceptions.IdentityError(message) raise exceptions.ComputeFault(message) if resp.status >= 400: resp_body = self._parse_resp(resp_body) self._log(req_url, body, resp, resp_body) raise exceptions.TempestException(str(resp.status)) return resp, resp_body