<NME> rest_client.py
<BEF> # Copyright 2012 OpenStack Foundation
# Copyright 2013 IBM Corp.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from collections import abc
import email.utils
import re
import time
import urllib

import jsonschema
from oslo_log import log as logging
from oslo_log import versionutils
from oslo_serialization import jsonutils as json

from tempest.lib.common import http
from tempest.lib.common import jsonschema_validator
from tempest.lib.common import profiler
from tempest.lib.common.utils import test_utils
from tempest.lib import exceptions

# redrive rate limited calls at most twice
MAX_RECURSION_DEPTH = 2

# All the successful HTTP status codes from RFC 7231 & 4918
HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206, 207)

# All the redirection HTTP status codes from RFC 7231 & 4918
HTTP_REDIRECTION = (300, 301, 302, 303, 304, 305, 306, 307)

# JSON Schema validator and format checker used for JSON Schema validation
JSONSCHEMA_VALIDATOR = jsonschema_validator.JSONSCHEMA_VALIDATOR
FORMAT_CHECKER = jsonschema_validator.FORMAT_CHECKER


class RestClient(object):
    """Unified OpenStack RestClient class

    This class is used for building openstack api clients on top of. It is
    intended to provide a base layer for wrapping outgoing http requests in
    keystone auth as well as providing response code checking and error
    handling.

    :param auth_provider: an auth provider object used to wrap requests in auth
    :param str service: The service name to use for the catalog lookup
    :param str region: The region to use for the catalog lookup
    :param str name: The endpoint name to use for the catalog lookup; this
                     returns only if the service exists
    :param str endpoint_type: The endpoint type to use for the catalog lookup
    :param int build_interval: Time in seconds between to status checks in
                               wait loops
    :param int build_timeout: Timeout in seconds to wait for a wait operation.
    :param bool disable_ssl_certificate_validation: Set to true to disable ssl
                                                    certificate validation
    :param str ca_certs: File containing the CA Bundle to use in verifying a
                         TLS server cert
    :param str trace_requests: Regex to use for specifying logging the entirety
    :param str http_timeout: Timeout in seconds to wait for the http request to
                             return
    """
    TYPE = "json"

    # The version of the API this client implements
    api_version = None

    # The version of the API this client implements
    api_version = None

    LOG = logging.getLogger(__name__)

    def __init__(self, auth_provider, service, region,
                 endpoint_type='publicURL',
                 build_interval=1, build_timeout=60,
                 disable_ssl_certificate_validation=False, ca_certs=None,
                 trace_requests='', name=None, http_timeout=None,
                 proxy_url=None, follow_redirects=True):
        self.auth_provider = auth_provider
        self.service = service
        self.region = region
        self.name = name
        self.endpoint_type = endpoint_type
        self.build_interval = build_interval
        self.build_timeout = build_timeout
        self.trace_requests = trace_requests

        self._skip_path = False
        self.general_header_lc = set(('cache-control', 'connection',
                                      'date', 'pragma', 'trailer',
                                      'transfer-encoding', 'via',
                                      'warning'))
        self.response_header_lc = set(('accept-ranges', 'age', 'etag',
                                       'location', 'proxy-authenticate',
            disable_ssl_certificate_validation=dscv, ca_certs=ca_certs,
            timeout=http_timeout)

    def _get_type(self):
        if self.TYPE != "json":
            self.LOG.warning("Tempest has dropped XML support and the TYPE "
                             "became meaningless")
        return self.TYPE

    def get_headers(self, accept_type=None, send_type=None):
        """Return the default headers which will be used with outgoing requests

                proxy_url,
                disable_ssl_certificate_validation=self.dscv,
                ca_certs=ca_certs,
                timeout=http_timeout, follow_redirects=follow_redirects)
        else:
            self.http_obj = http.ClosingHttp(
                disable_ssl_certificate_validation=self.dscv,
                ca_certs=ca_certs,
                 dict for outgoing request
        """
        if accept_type is None:
            accept_type = self._get_type()
        if send_type is None:
            send_type = self._get_type()
        return {'Content-Type': 'application/%s' % send_type,
                'Accept': 'application/%s' % accept_type}

                              header, if one isn't provided the object var
                              TYPE will be used
        :rtype: dict
        :return: The dictionary of headers which can be used in the headers
                 dict for outgoing request
        """
        if accept_type is None:
            accept_type = 'json'
        if send_type is None:
            send_type = 'json'
        headers = {'Content-Type': 'application/%s' % send_type,
                   'Accept': 'application/%s' % accept_type}
        headers.update(profiler.serialize_as_http_headers())
        return headers

    def __str__(self):
        STRING_LIMIT = 80
        str_format = ("service:%s, base_url:%s, "
                      "filters: %s, build_interval:%s, build_timeout:%s"
                      "\ntoken:%s..., \nheaders:%s...")
        return str_format % (self.service, self.base_url,
                             self.filters, self.build_interval,
                             self.build_timeout,
                             str(self.token)[0:STRING_LIMIT],
                             str(self.get_headers())[0:STRING_LIMIT])

    @property
    def user(self):
        """The username used for requests

        :rtype: string
        :return: The username being used for requests
        """

        return self.auth_provider.credentials.username

    @property
    def user_id(self):
        """The user_id used for requests

        :rtype: string
        :return: The user id being used for requests
        """
        return self.auth_provider.credentials.user_id

    @property
    def tenant_name(self):
        """The tenant/project being used for requests

        :rtype: string
        :return: The tenant/project name being used for requests
        """
        return self.auth_provider.credentials.tenant_name

    @property
    def project_id(self):
        """The project id being used for requests

        :rtype: string
        :return: The project id being used for requests
        """
        return self.auth_provider.credentials.tenant_id

    @property
    def tenant_id(self):
        """The tenant/project id being used for requests

        :rtype: string
        :return: The tenant/project id being used for requests
        """
        # NOTE(ralonsoh): this property should be deprecated, reference
        # blueprint adopt-oslo-versioned-objects-for-db.
        versionutils.report_deprecated_feature(
            self.LOG, '"tenant_id" property is deprecated for removal, use '
                      '"project_id" instead')
        return self.project_id

    @property
    def password(self):
        """The password being used for requests

        :rtype: string
        :return: The password being used for requests
        """
        return self.auth_provider.credentials.password

    @property
    def base_url(self):
        return self.auth_provider.base_url(filters=self.filters)

    @property
    def token(self):
        return self.auth_provider.get_token()

    @property
    def filters(self):
        _filters = dict(
            service=self.service,
            endpoint_type=self.endpoint_type,
            region=self.region,
            name=self.name
        )
        if self.api_version is not None:
            _filters['api_version'] = self.api_version
        if self._skip_path:
            _filters['skip_path'] = self._skip_path
        return _filters

    def skip_path(self):
        """When set, ignore the path part of the base URL from the catalog"""
        self._skip_path = True

    def reset_path(self):
        """When reset, use the base URL from the catalog as-is"""
        self._skip_path = False

    @classmethod
    def expected_success(cls, expected_code, read_code):
        """Check expected success response code against the http response

        :param int expected_code: The response code that is expected.
                                  Optionally a list of integers can be used
                                  to specify multiple valid success codes
        :param int read_code: The response code which was returned in the
                              response
        :raises AssertionError: if the expected_code isn't a valid http success
                                response code
        :raises exceptions.InvalidHttpSuccessCode: if the read code isn't an
                                                   expected http success code
        """
        if not isinstance(read_code, int):
            raise TypeError("'read_code' must be an int instead of (%s)"
                            % type(read_code))

        assert_msg = ("This function only allowed to use for HTTP status "
                      "codes which explicitly defined in the RFC 7231 & 4918. "
                      "{0} is not a defined Success Code!"
                      ).format(expected_code)
        if isinstance(expected_code, list):
            for code in expected_code:
                assert code in HTTP_SUCCESS + HTTP_REDIRECTION, assert_msg
        else:
            assert expected_code in HTTP_SUCCESS + HTTP_REDIRECTION, assert_msg

        # NOTE(afazekas): the http status code above 400 is processed by
        # the _error_checker method
        if read_code < 400:
            pattern = ("Unexpected http success status code {0}, "
                       "The expected status code is {1}")
            if ((not isinstance(expected_code, list) and
                 (read_code != expected_code)) or
                (isinstance(expected_code, list) and
                 (read_code not in expected_code))):
                details = pattern.format(read_code, expected_code)
                raise exceptions.InvalidHttpSuccessCode(details)

    def post(self, url, body, headers=None, extra_headers=False,
             chunked=False):
        """Send a HTTP POST request using keystone auth

        :param str url: the relative url to send the post request to
        :param dict body: the request body
        :param dict headers: The headers to use for the request
        :param bool extra_headers: Boolean value than indicates if the headers
                                   returned by the get_headers() method are to
                                   be used but additional headers are needed in
                                   the request pass them in as a dict.
        :param bool chunked: sends the body with chunked encoding
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        :rtype: tuple
        """
        return self.request('POST', url, extra_headers, headers, body, chunked)

    def get(self, url, headers=None, extra_headers=False):
        """Send a HTTP GET request using keystone service catalog and auth

        :param str url: the relative url to send the get request to
        :param dict headers: The headers to use for the request
        :param bool extra_headers: Boolean value than indicates if the headers
                                   returned by the get_headers() method are to
                                   be used but additional headers are needed in
                                   the request pass them in as a dict.
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        :rtype: tuple
        """
        return self.request('GET', url, extra_headers, headers)

    def delete(self, url, headers=None, body=None, extra_headers=False):
        """Send a HTTP DELETE request using keystone service catalog and auth

        :param str url: the relative url to send the delete request to
        :param dict headers: The headers to use for the request
        :param dict body: the request body
        :param bool extra_headers: Boolean value than indicates if the headers
                                   returned by the get_headers() method are to
                                   be used but additional headers are needed in
                                   the request pass them in as a dict.
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        :rtype: tuple
        """
        return self.request('DELETE', url, extra_headers, headers, body)

    def patch(self, url, body, headers=None, extra_headers=False):
        """Send a HTTP PATCH request using keystone service catalog and auth

        :param str url: the relative url to send the patch request to
        :param dict body: the request body
        :param dict headers: The headers to use for the request
        :param bool extra_headers: Boolean value than indicates if the headers
                                   returned by the get_headers() method are to
                                   be used but additional headers are needed in
                                   the request pass them in as a dict.
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        :rtype: tuple
        """
        return self.request('PATCH', url, extra_headers, headers, body)

    def put(self, url, body, headers=None, extra_headers=False, chunked=False):
        """Send a HTTP PUT request using keystone service catalog and auth

        :param str url: the relative url to send the put request to
        :param dict body: the request body
        :param dict headers: The headers to use for the request
        :param bool extra_headers: Boolean value than indicates if the headers
                                   returned by the get_headers() method are to
                                   be used but additional headers are needed in
                                   the request pass them in as a dict.
        :param bool chunked: sends the body with chunked encoding
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        :rtype: tuple
        """
        return self.request('PUT', url, extra_headers, headers, body, chunked)

    def head(self, url, headers=None, extra_headers=False):
        """Send a HTTP HEAD request using keystone service catalog and auth

        :param str url: the relative url to send the head request to
        :param dict headers: The headers to use for the request
        :param bool extra_headers: Boolean value than indicates if the headers
                                   returned by the get_headers() method are to
                                   be used but additional headers are needed in
                                   the request pass them in as a dict.
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        :rtype: tuple
        """
        return self.request('HEAD', url, extra_headers, headers)

    def copy(self, url, headers=None, extra_headers=False):
        """Send a HTTP COPY request using keystone service catalog and auth

        :param str url: the relative url to send the copy request to
        :param dict headers: The headers to use for the request
        :param bool extra_headers: Boolean value than indicates if the headers
                                   returned by the get_headers() method are to
                                   be used but additional headers are needed in
                                   the request pass them in as a dict.
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        :rtype: tuple
        """
        return self.request('COPY', url, extra_headers, headers)

    def get_versions(self):
        """Get the versions on an endpoint from the keystone catalog

        This method will make a GET request on the baseurl from the keystone
        catalog to return a list of API versions. It is expected that a GET
        on the endpoint in the catalog will return a list of supported API
        versions.

        :return: tuple with response headers and list of version numbers
        :rtype: tuple
        """
        resp, body = self.get('')
        body = self._parse_resp(body)
        versions = map(lambda x: x['id'], body)
        return resp, versions

    def _get_request_id(self, resp):
        for i in ('x-openstack-request-id', 'x-compute-request-id'):
            if i in resp:
                return resp[i]
        return ""

    def _get_global_request_id(self, resp):
        if 'x-openstack-request-id' in resp:
            return resp['x-openstack-request-id']
        return ''

    def _safe_body(self, body, maxlen=4096):
        # convert a structure into a string safely
        try:
            text = str(body)
        except UnicodeDecodeError:
            # if this isn't actually text, return marker that
            return "<BinaryData: removed>"
        if len(text) > maxlen:
            return text[:maxlen]
        else:
            return text

    def _log_request_start(self, method, req_url):
        caller_name = test_utils.find_test_caller()
        if self.trace_requests and re.search(self.trace_requests, caller_name):
            self.LOG.debug('Starting Request (%s): %s %s', caller_name,
                           method, req_url)

    def _log_request_full(self, resp, req_headers=None, req_body=None,
                          resp_body=None, extra=None):
        if 'X-Auth-Token' in req_headers:
            req_headers['X-Auth-Token'] = '<omitted>'
        if 'X-Subject-Token' in req_headers:
            req_headers['X-Subject-Token'] = '<omitted>'
        # A shallow copy is sufficient
        resp_log = resp.copy()
        if 'x-subject-token' in resp_log:
            resp_log['x-subject-token'] = '<omitted>'
        log_fmt = """Request - Headers: %s
        Body: %s
    Response - Headers: %s
        Body: %s"""

        self.LOG.debug(
            log_fmt,
            str(req_headers),
            self._safe_body(req_body),
            str(resp_log),
            self._safe_body(resp_body),
            extra=extra)

    def _log_request(self, method, req_url, resp,
                     secs="", req_headers=None,
                     req_body=None, resp_body=None):
        if req_headers is None:
            req_headers = {}
        # if we have the request id, put it in the right part of the log
        extra = {
            'request_id': self._get_request_id(resp),
            'global_request_id': self._get_global_request_id(resp),
        }
        # NOTE(sdague): while we still have 6 callers to this function
        # we're going to just provide work around on who is actually
        # providing timings by gracefully adding no content if they don't.
        # Once we're down to 1 caller, clean this up.
        caller_name = test_utils.find_test_caller()
        if secs:
            secs = " %.3fs" % secs
        self.LOG.info(
            'Request (%s): %s %s %s%s',
            caller_name,
            resp['status'],
            method,
            req_url,
            secs,
            extra=extra)

        # Also look everything at DEBUG if you want to filter this
        # out, don't run at debug.
        if self.LOG.isEnabledFor(logging.DEBUG):
            self._log_request_full(resp, req_headers, req_body,
                                   resp_body, extra)

    def _parse_resp(self, body, top_key_to_verify=None):
        try:
            body = json.loads(body)
        except ValueError:
            return body

        # We assume, that if the first value of the deserialized body's
        # item set is a dict or a list, that we just return the first value
        # of deserialized body.
        # Essentially "cutting out" the first placeholder element in a body
        # that looks like this:
        #
        #  {
        #    "users": [
        #      ...
        #    ]
        #  }
        try:
            # Ensure there are not more than one top-level keys
            # NOTE(freerunner): Ensure, that JSON is not nullable to
            # to prevent StopIteration Exception
            if not hasattr(body, "keys") or len(body.keys()) != 1:
                return body
            # Just return the "wrapped" element
            first_key, first_item = tuple(body.items())[0]
            if isinstance(first_item, (dict, list)):
                if top_key_to_verify is not None:
                    msg_args = {
                        'top_key': top_key_to_verify,
                        'actual_key': first_key,
                    }
                    assert_msg = ("The expected top level key is "
                                  "'%(top_key)s' but we found "
                                  "'%(actual_key)s'." % msg_args)
                    assert top_key_to_verify == first_key, assert_msg
                return first_item
        except (ValueError, IndexError):
            pass
        return body

    def response_checker(self, method, resp, resp_body):
        """A sanity check on the response from a HTTP request

        This method does a sanity check on whether the response from an HTTP
        request conforms the HTTP RFC.

        :param str method: The HTTP verb of the request associated with the
                           response being passed in.
        :param resp: The response headers
        :param resp_body: The body of the response
        :raises ResponseWithNonEmptyBody: If the response with the status code
                                          is not supposed to have a body
        :raises ResponseWithEntity: If the response code is 205 but has an
                                    entity
        """
        if (resp.status in set((204, 205, 304)) or resp.status < 200 or
                method.upper() == 'HEAD') and resp_body:
            raise exceptions.ResponseWithNonEmptyBody(status=resp.status)
        # NOTE(afazekas):
        # If the HTTP Status Code is 205
        #   'The response MUST NOT include an entity.'
        # A HTTP entity has an entity-body and an 'entity-header'.
        # In the HTTP response specification (Section 6) the 'entity-header'
        # 'generic-header' and 'response-header' are in OR relation.
        # All headers not in the above two group are considered as entity
        # header in every interpretation.

        if (resp.status == 205 and
            0 != len(set(resp.keys()) - set(('status',)) -
                     self.response_header_lc - self.general_header_lc)):
            raise exceptions.ResponseWithEntity()
        # NOTE(afazekas)
        # Now the swift sometimes (delete not empty container)
        # returns with non json error response, we can create new rest class
        # for swift.
        # Usually RFC2616 says error responses SHOULD contain an explanation.
        # The warning is normal for SHOULD/SHOULD NOT case

        # Likely it will cause an error
        if method != 'HEAD' and not resp_body and resp.status >= 400:
            self.LOG.warning("status >= 400 response with empty body")

    def _request(self, method, url, headers=None, body=None, chunked=False):
        """A simple HTTP request interface."""
        # Authenticate the request with the auth provider
        req_url, req_headers, req_body = self.auth_provider.auth_request(
            method, url, headers, body, self.filters)

        resp, resp_body = self.raw_request(
            req_url, method, headers=req_headers, body=req_body,
            chunked=chunked
        )
        # Verify HTTP response codes
        self.response_checker(method, resp, resp_body)

        return resp, resp_body

    def raw_request(self, url, method, headers=None, body=None, chunked=False,
                    log_req_body=None):
        """Send a raw HTTP request without the keystone catalog or auth

        This method sends a HTTP request in the same manner as the request()
        method, however it does so without using keystone auth or the catalog
        to determine the base url. Additionally no response handling is done
        the results from the request are just returned.

        :param str url: Full url to send the request
        :param str method: The HTTP verb to use for the request
        :param dict headers: Headers to use for the request. If none are
                             specified, then the headers returned from the
                             get_headers() method are used. If the request
                             explicitly requires no headers use an empty dict.
        :param str body: Body to send with the request
        :param bool chunked: sends the body with chunked encoding
        :param str log_req_body: Whether to log the request body or not.
                                 It is default to None which means request
                                 body is safe to log otherwise pass any string
                                 you want to log in place of request body.
                                 For example: '<omitted>'
        :rtype: tuple
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        """
        if headers is None:
            headers = self.get_headers()
        # Do the actual request, and time it
        start = time.time()
        self._log_request_start(method, url)
        resp, resp_body = self.http_obj.request(
            url, method, headers=headers,
            body=body, chunked=chunked)
        end = time.time()
        req_body = body if log_req_body is None else log_req_body
        self._log_request(method, url, resp, secs=(end - start),
                          req_headers=headers, req_body=req_body,
                          resp_body=resp_body)
        return resp, resp_body

    def request(self, method, url, extra_headers=False, headers=None,
                body=None, chunked=False):
        """Send a HTTP request with keystone auth and using the catalog

        This method will send an HTTP request using keystone auth in the
        headers and the catalog to determine the endpoint to use for the
        baseurl to send the request to. Additionally

        When a response is received it will check it to see if an error
        response was received. If it was an exception will be raised to enable
        it to be handled quickly.

        This method will also handle rate-limiting, if a 413 response code is
        received it will retry the request after waiting the 'retry-after'
        duration from the header.

        :param str method: The HTTP verb to use for the request
        :param str url: Relative url to send the request to
        :param bool extra_headers: Boolean value than indicates if the headers
                                   returned by the get_headers() method are to
                                   be used but additional headers are needed in
                                   the request pass them in as a dict.
        :param dict headers: Headers to use for the request. If none are
                             specified, then the headers returned from the
                             get_headers() method are used. If the request
                             explicitly requires no headers use an empty dict.
        :param str body: Body to send with the request
        :param bool chunked: sends the body with chunked encoding
        :rtype: tuple
        :return: a tuple with the first entry containing the response headers
                 and the second the response body
        :raises UnexpectedContentType: If the content-type of the response
                                       isn't an expect type
        :raises Unauthorized: If a 401 response code is received
        :raises Forbidden: If a 403 response code is received
        :raises NotFound: If a 404 response code is received
        :raises BadRequest: If a 400 response code is received
        :raises Gone: If a 410 response code is received
        :raises Conflict: If a 409 response code is received
        :raises PreconditionFailed: If a 412 response code is received
        :raises OverLimit: If a 413 response code is received and retry-after
                           is not in the response body or its retry operation
                           exceeds the limits defined by the server
        :raises RateLimitExceeded: If a 413 response code is received and
                                   retry-after is in the response body and
                                   its retry operation does not exceeds the
                                   limits defined by the server
        :raises InvalidContentType: If a 415 response code is received
        :raises UnprocessableEntity: If a 422 response code is received
        :raises InvalidHTTPResponseBody: The response body wasn't valid JSON
                                         and couldn't be parsed
        :raises NotImplemented: If a 501 response code is received
        :raises ServerFault: If a 500 response code is received
        :raises UnexpectedResponseCode: If a response code above 400 is
                                        received and it doesn't fall into any
                                        of the handled checks
        """
        # if extra_headers is True
        # default headers would be added to headers
        retry = 0

        if headers is None:
            # NOTE(vponomaryov): if some client do not need headers,
            # it should explicitly pass empty dict
            headers = self.get_headers()
        elif extra_headers:
            try:
                headers.update(self.get_headers())
            except (ValueError, TypeError):
                headers = self.get_headers()

        resp, resp_body = self._request(method, url, headers=headers,
                                        body=body, chunked=chunked)

        while (resp.status == 413 and
               'retry-after' in resp and
                not self.is_absolute_limit(
                    resp, self._parse_resp(resp_body)) and
                retry < MAX_RECURSION_DEPTH):
            retry += 1
            delay = self._get_retry_after_delay(resp)
            self.LOG.debug(
                "Sleeping %s seconds based on retry-after header", delay
            )
            time.sleep(delay)
            resp, resp_body = self._request(method, url,
                                            headers=headers, body=body)
        self._error_checker(resp, resp_body)
        return resp, resp_body

    def _get_retry_after_delay(self, resp):
        """Extract the delay from the retry-after header.

        This supports both integer and HTTP date formatted retry-after headers
        per RFC 2616.

        :param resp: The response containing the retry-after headers
        :rtype: int
        :return: The delay in seconds, clamped to be at least 1 second
        :raises ValueError: On failing to parse the delay
        """
        delay = None
        try:
            delay = int(resp['retry-after'])
        except (ValueError, KeyError):
            pass

        try:
            retry_timestamp = self._parse_http_date(resp['retry-after'])
            date_timestamp = self._parse_http_date(resp['date'])
            delay = int(retry_timestamp - date_timestamp)
        except (ValueError, OverflowError, KeyError):
            pass

        if delay is None:
            raise ValueError(
                "Failed to parse retry-after header %r as either int or "
                "HTTP-date." % resp.get('retry-after')
            )

        # Retry-after headers do not have sub-second precision. Clients may
        # receive a delay of 0. After sleeping 0 seconds, we would (likely) hit
        # another 413. To avoid this, always sleep at least 1 second.
        return max(1, delay)

    def _parse_http_date(self, val):
        """Parse an HTTP date, like 'Fri, 31 Dec 1999 23:59:59 GMT'.

        Return an epoch timestamp (float), as returned by time.mktime().
        """
        parts = email.utils.parsedate(val)
        if not parts:
            raise ValueError("Failed to parse date %s" % val)
        return time.mktime(parts)

    def _error_checker(self, 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

        # NOTE(zhipengh): There is a purposefully duplicate of content-type
        # with the only difference is with or without spaces, as specified
        # in RFC7231.
        JSON_ENC = ['application/json', 'application/json; charset=utf-8',
                    '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.
        # NOTE(zhipengh): There is a purposefully duplicate of content-type
        # with the only difference is with or without spaces, as specified
        # in RFC7231.
        TXT_ENC = ['text/plain', 'text/html', 'text/html; charset=utf-8',
                   'text/plain; charset=utf-8', 'text/html;charset=utf-8',
                   'text/plain;charset=utf-8']

        if ctype.lower() in JSON_ENC:
            parse_resp = True
        elif ctype.lower() in TXT_ENC:
            parse_resp = False
        else:
            raise exceptions.UnexpectedContentType(str(resp.status),
                                                   resp=resp)

        if resp.status == 401:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.Unauthorized(resp_body, resp=resp)

        if resp.status == 403:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.Forbidden(resp_body, resp=resp)

        if resp.status == 404:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.NotFound(resp_body, resp=resp)

        if resp.status == 400:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.BadRequest(resp_body, resp=resp)

        if resp.status == 410:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.Gone(resp_body, resp=resp)

        if resp.status == 409:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.Conflict(resp_body, resp=resp)

        if resp.status == 412:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.PreconditionFailed(resp_body, resp=resp)

        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, resp=resp)
            else:
                raise exceptions.RateLimitExceeded(resp_body, resp=resp)

        if resp.status == 415:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.InvalidContentType(resp_body, resp=resp)

        if resp.status == 422:
            if parse_resp:
                resp_body = self._parse_resp(resp_body)
            raise exceptions.UnprocessableEntity(resp_body, resp=resp)

        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:
                            message = resp_body['error']['message']
                        elif 'message' in resp_body:
                            message = resp_body['message']
                    else:
                        message = resp_body

            if resp.status == 501:
                raise exceptions.NotImplemented(resp_body, resp=resp,
                                                message=message)
            else:
                raise exceptions.ServerFault(resp_body, resp=resp,
                                             message=message)

        if resp.status >= 400:
            raise exceptions.UnexpectedResponseCode(str(resp.status),
                                                    resp=resp)

    def is_absolute_limit(self, resp, resp_body):
        if (not isinstance(resp_body, abc.Mapping) or
                'retry-after' not in resp):
            return True
        return 'exceed' in resp_body.get('message', 'blabla')

    def wait_for_resource_deletion(self, id, *args, **kwargs):
        """Waits for a resource to be deleted

        This method will loop over is_resource_deleted until either
        is_resource_deleted returns True or the build timeout is reached. This
        depends on is_resource_deleted being implemented

        :param str id: The id of the resource to check
        :raises TimeoutException: If the build_timeout has elapsed and the
                                  resource still hasn't been deleted
        """
        start_time = int(time.time())
        while True:
            if self.is_resource_deleted(id, *args, **kwargs):
                return
            if int(time.time()) - start_time >= self.build_timeout:
                message = ('Failed to delete %(resource_type)s %(id)s within '
                           'the required time (%(timeout)s s). Timer started '
                           'at %(start_time)s. Timer ended at %(end_time)s. '
                           'Waited for %(wait_time)s s.' %
                           {'resource_type': self.resource_type, 'id': id,
                            'timeout': self.build_timeout,
                            'start_time': start_time,
                            'end_time': int(time.time()),
                            'wait_time': int(time.time()) - start_time})
                caller = test_utils.find_test_caller()
                if caller:
                    message = '(%s) %s' % (caller, message)
                raise exceptions.TimeoutException(message)
            time.sleep(self.build_interval)

    def wait_for_resource_activation(self, id):
        """Waits for a resource to become active

        This method will loop over is_resource_active until either
        is_resource_active returns True or the build timeout is reached. This
        depends on is_resource_active being implemented

        :param str id: The id of the resource to check
        :raises TimeoutException: If the build_timeout has elapsed and the
                                  resource still hasn't been active
        """
        start_time = int(time.time())
        while True:
            if self.is_resource_active(id):
                return
            if int(time.time()) - start_time >= self.build_timeout:
                message = ('Failed to reach active state %(resource_type)s '
                           '%(id)s within the required time (%(timeout)s s).' %
                           {'resource_type': self.resource_type, 'id': id,
                            'timeout': self.build_timeout})
                caller = test_utils.find_test_caller()
                if caller:
                    message = '(%s) %s' % (caller, message)
                raise exceptions.TimeoutException(message)
            time.sleep(self.build_interval)

    def is_resource_deleted(self, id):
        """Subclasses override with specific deletion detection."""
        message = ('"%s" does not implement is_resource_deleted'
                   % self.__class__.__name__)
        raise NotImplementedError(message)

    def is_resource_active(self, id):
        """Subclasses override with specific active detection."""
        message = ('"%s" does not implement is_resource_active'
                   % self.__class__.__name__)
        raise NotImplementedError(message)

    @property
    def resource_type(self):
        """Returns the primary type of resource this client works with."""
        return 'resource'

    @classmethod
    def validate_response(cls, schema, resp, body):
        # Only check the response if the status code is a success code
        # TODO(cyeoh): Eventually we should be able to verify that a failure
        # code if it exists is something that we expect. This is explicitly
        # declared in the V3 API and so we should be able to export this in
        # the response schema. For now we'll ignore it.
        if resp.status in HTTP_SUCCESS + HTTP_REDIRECTION:
            cls.expected_success(schema['status_code'], resp.status)

            # Check the body of a response
            body_schema = schema.get('response_body')
            if body_schema:
                try:
                    jsonschema.validate(body, body_schema,
                                        cls=JSONSCHEMA_VALIDATOR,
                                        format_checker=FORMAT_CHECKER)
                except jsonschema.ValidationError as ex:
                    msg = ("HTTP response body is invalid (%s)" % ex)
                    raise exceptions.InvalidHTTPResponseBody(msg)
            else:
                if body:
                    msg = ("HTTP response body should not exist (%s)" % body)
                    raise exceptions.InvalidHTTPResponseBody(msg)

            # Check the header of a response
            header_schema = schema.get('response_header')
            if header_schema:
                try:
                    jsonschema.validate(resp, header_schema,
                                        cls=JSONSCHEMA_VALIDATOR,
                                        format_checker=FORMAT_CHECKER)
                except jsonschema.ValidationError as ex:
                    msg = ("HTTP response header is invalid (%s)" % ex)
                    raise exceptions.InvalidHTTPResponseHeader(msg)

    def _get_base_version_url(self):
        # TODO(oomichi): This method can be used for auth's replace_version().
        # So it is nice to have common logic for the maintenance.
        endpoint = self.base_url
        url = urllib.parse.urlsplit(endpoint)
        new_path = re.split(r'(^|/)+v\d+(\.\d+)?', url.path)[0]
        url = list(url)
        url[2] = new_path + '/'
        return urllib.parse.urlunsplit(url)


class ResponseBody(dict):
    """Class that wraps an http response and dict body into a single value.

    Callers that receive this object will normally use it as a dict but
    can extract the response if needed.
    """

    def __init__(self, response, body=None):
        body_data = body or {}
        self.update(body_data)
        self.response = response

    def __str__(self):
        body = super(ResponseBody, self).__str__()
        return "response: %s\nBody: %s" % (self.response, body)


class ResponseBodyData(object):
    """Class that wraps an http response and string data into a single value.

    """

    def __init__(self, response, data):
        self.response = response
        self.data = data

    def __str__(self):
        return "response: %s\nBody: %s" % (self.response, self.data)


class ResponseBodyList(list):
    """Class that wraps an http response and list body into a single value.

    Callers that receive this object will normally use it as a list but
    can extract the response if needed.
    """

    def __init__(self, response, body=None):
        body_data = body or []
        self.extend(body_data)
        self.response = response

    def __str__(self):
        body = super(ResponseBodyList, self).__str__()
        return "response: %s\nBody: %s" % (self.response, body)

<MSG> Merge "Remove deprecated TYPE in rest_client"
<DFF> @@ -70,7 +70,6 @@ class RestClient(object):
     :param str http_timeout: Timeout in seconds to wait for the http request to
                              return
     """
-    TYPE = "json"
 
     # The version of the API this client implements
     api_version = None
@@ -105,12 +104,6 @@ class RestClient(object):
             disable_ssl_certificate_validation=dscv, ca_certs=ca_certs,
             timeout=http_timeout)
 
-    def _get_type(self):
-        if self.TYPE != "json":
-            self.LOG.warning("Tempest has dropped XML support and the TYPE "
-                             "became meaningless")
-        return self.TYPE
-
     def get_headers(self, accept_type=None, send_type=None):
         """Return the default headers which will be used with outgoing requests
 
@@ -125,9 +118,9 @@ class RestClient(object):
                  dict for outgoing request
         """
         if accept_type is None:
-            accept_type = self._get_type()
+            accept_type = 'json'
         if send_type is None:
-            send_type = self._get_type()
+            send_type = 'json'
         return {'Content-Type': 'application/%s' % send_type,
                 'Accept': 'application/%s' % accept_type}
 

{'commit': 'diff --git a/tempest/lib/common/rest_client.py b/tempest/lib/common/rest_client.py\nindex 99ba6ab9e..d72b4ddb6 100644\n--- a/tempest/lib/common/rest_client.py\n+++ b/tempest/lib/common/rest_client.py\n@@ -70,7 +70,6 @@ class RestClient(object):\n     :param str http_timeout: Timeout in seconds to wait for the http request to\n                              return\n     """\n-    TYPE = "json"\n \n     # The version of the API this client implements\n     api_version = None\n@@ -105,12 +104,6 @@ class RestClient(object):\n             disable_ssl_certificate_validation=dscv, ca_certs=ca_certs,\n             timeout=http_timeout)\n \n-    def _get_type(self):\n-        if self.TYPE != "json":\n-            self.LOG.warning("Tempest has dropped XML support and the TYPE "\n-                             "became meaningless")\n-        return self.TYPE\n-\n     def get_headers(self, accept_type=None, send_type=None):\n         """Return the default headers which will be used with outgoing requests\n \n@@ -125,9 +118,9 @@ class RestClient(object):\n                  dict for outgoing request\n         """\n         if accept_type is None:\n-            accept_type = self._get_type()\n+            accept_type = \'json\'\n         if send_type is None:\n-            send_type = self._get_type()\n+            send_type = \'json\'\n         return {\'Content-Type\': \'application/%s\' % send_type,\n                 \'Accept\': \'application/%s\' % accept_type}\n \ndiff --git a/tempest/tests/lib/test_rest_client.py b/tempest/tests/lib/test_rest_client.py\nindex 4a8363123..43bb6d080 100644\n--- a/tempest/tests/lib/test_rest_client.py\n+++ b/tempest/tests/lib/test_rest_client.py\n@@ -91,18 +91,15 @@ class TestRestClientNotFoundHandling(BaseRestClientTestClass):\n \n \n class TestRestClientHeadersJSON(TestRestClientHTTPMethods):\n-    TYPE = "json"\n \n     def _verify_headers(self, resp):\n-        self.assertEqual(self.rest_client._get_type(), self.TYPE)\n         resp = dict((k.lower(), v) for k, v in six.iteritems(resp))\n         self.assertEqual(self.header_value, resp[\'accept\'])\n         self.assertEqual(self.header_value, resp[\'content-type\'])\n \n     def setUp(self):\n         super(TestRestClientHeadersJSON, self).setUp()\n-        self.rest_client.TYPE = self.TYPE\n-        self.header_value = \'application/%s\' % self.rest_client._get_type()\n+        self.header_value = \'application/json\'\n \n     def test_post(self):\n         resp, __ = self.rest_client.post(self.url, {})\n', 'commit_subject': 'Merge "Remove deprecated TYPE in rest_client"', 'repo_name': 'openstack/tempest', 'committer': {'name': 'Gerrit Code Review', 'email': '61e62b213a1a56f7695845df4fc372a10cb0a73e@openstack.org', 'time_sec': '1493135392', 'tz_offset': '0', 'date': {'seconds': '1493135392'}}, 'commit_tree': '8fdacc430bd7adf2f455d479fcbfcedc303ecb76', 'commit_message': 'Merge "Remove deprecated TYPE in rest_client"', 'language_name': 'Shell', 'license': 'apache-2.0', 'hunks': '@@ -70,7 +70,6 @@ class RestClient(object):\n     :param str http_timeout: Timeout in seconds to wait for the http request to\n                              return\n     """\n-    TYPE = "json"\n \n     # The version of the API this client implements\n     api_version = None\n@@ -105,12 +104,6 @@ class RestClient(object):\n             disable_ssl_certificate_validation=dscv, ca_certs=ca_certs,\n             timeout=http_timeout)\n \n-    def _get_type(self):\n-        if self.TYPE != "json":\n-            self.LOG.warning("Tempest has dropped XML support and the TYPE "\n-                             "became meaningless")\n-        return self.TYPE\n-\n     def get_headers(self, accept_type=None, send_type=None):\n         """Return the default headers which will be used with outgoing requests\n \n@@ -125,9 +118,9 @@ class RestClient(object):\n                  dict for outgoing request\n         """\n         if accept_type is None:\n-            accept_type = self._get_type()\n+            accept_type = \'json\'\n         if send_type is None:\n-            send_type = self._get_type()\n+            send_type = \'json\'\n         return {\'Content-Type\': \'application/%s\' % send_type,\n                 \'Accept\': \'application/%s\' % accept_type}\n \n', 'addition_count': 2, 'deletion_count': 9, 'src_file': 'a/tempest/lib/common/rest_client.py', 'tgt_file': 'b/tempest/lib/common/rest_client.py', 'file_extension': '.py', 'before_file': '# Copyright 2012 OpenStack Foundation\n# Copyright 2013 IBM Corp.\n# All Rights Reserved.\n#\n#    Licensed under the Apache License, Version 2.0 (the "License"); you may\n#    not use this file except in compliance with the License. You may obtain\n#    a copy of the License at\n#\n#         http://www.apache.org/licenses/LICENSE-2.0\n#\n#    Unless required by applicable law or agreed to in writing, software\n#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\n#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n#    License for the specific language governing permissions and limitations\n#    under the License.\n\nfrom collections import abc\nimport email.utils\nimport re\nimport time\nimport urllib\n\nimport jsonschema\nfrom oslo_log import log as logging\nfrom oslo_log import versionutils\nfrom oslo_serialization import jsonutils as json\n\nfrom tempest.lib.common import http\nfrom tempest.lib.common import jsonschema_validator\nfrom tempest.lib.common import profiler\nfrom tempest.lib.common.utils import test_utils\nfrom tempest.lib import exceptions\n\n# redrive rate limited calls at most twice\nMAX_RECURSION_DEPTH = 2\n\n# All the successful HTTP status codes from RFC 7231 & 4918\nHTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206, 207)\n\n# All the redirection HTTP status codes from RFC 7231 & 4918\nHTTP_REDIRECTION = (300, 301, 302, 303, 304, 305, 306, 307)\n\n# JSON Schema validator and format checker used for JSON Schema validation\nJSONSCHEMA_VALIDATOR = jsonschema_validator.JSONSCHEMA_VALIDATOR\nFORMAT_CHECKER = jsonschema_validator.FORMAT_CHECKER\n\n\nclass RestClient(object):\n    """Unified OpenStack RestClient class\n\n    This class is used for building openstack api clients on top of. It is\n    intended to provide a base layer for wrapping outgoing http requests in\n    keystone auth as well as providing response code checking and error\n    handling.\n\n    :param auth_provider: an auth provider object used to wrap requests in auth\n    :param str service: The service name to use for the catalog lookup\n    :param str region: The region to use for the catalog lookup\n    :param str name: The endpoint name to use for the catalog lookup; this\n                     returns only if the service exists\n    :param str endpoint_type: The endpoint type to use for the catalog lookup\n    :param int build_interval: Time in seconds between to status checks in\n                               wait loops\n    :param int build_timeout: Timeout in seconds to wait for a wait operation.\n    :param bool disable_ssl_certificate_validation: Set to true to disable ssl\n                                                    certificate validation\n    :param str ca_certs: File containing the CA Bundle to use in verifying a\n                         TLS server cert\n    :param str trace_requests: Regex to use for specifying logging the entirety\n    :param str http_timeout: Timeout in seconds to wait for the http request to\n                             return\n    """\n    TYPE = "json"\n\n    # The version of the API this client implements\n    api_version = None\n\n    # The version of the API this client implements\n    api_version = None\n\n    LOG = logging.getLogger(__name__)\n\n    def __init__(self, auth_provider, service, region,\n                 endpoint_type=\'publicURL\',\n                 build_interval=1, build_timeout=60,\n                 disable_ssl_certificate_validation=False, ca_certs=None,\n                 trace_requests=\'\', name=None, http_timeout=None,\n                 proxy_url=None, follow_redirects=True):\n        self.auth_provider = auth_provider\n        self.service = service\n        self.region = region\n        self.name = name\n        self.endpoint_type = endpoint_type\n        self.build_interval = build_interval\n        self.build_timeout = build_timeout\n        self.trace_requests = trace_requests\n\n        self._skip_path = False\n        self.general_header_lc = set((\'cache-control\', \'connection\',\n                                      \'date\', \'pragma\', \'trailer\',\n                                      \'transfer-encoding\', \'via\',\n                                      \'warning\'))\n        self.response_header_lc = set((\'accept-ranges\', \'age\', \'etag\',\n                                       \'location\', \'proxy-authenticate\',\n            disable_ssl_certificate_validation=dscv, ca_certs=ca_certs,\n            timeout=http_timeout)\n\n    def _get_type(self):\n        if self.TYPE != "json":\n            self.LOG.warning("Tempest has dropped XML support and the TYPE "\n                             "became meaningless")\n        return self.TYPE\n\n    def get_headers(self, accept_type=None, send_type=None):\n        """Return the default headers which will be used with outgoing requests\n\n                proxy_url,\n                disable_ssl_certificate_validation=self.dscv,\n                ca_certs=ca_certs,\n                timeout=http_timeout, follow_redirects=follow_redirects)\n        else:\n            self.http_obj = http.ClosingHttp(\n                disable_ssl_certificate_validation=self.dscv,\n                ca_certs=ca_certs,\n                 dict for outgoing request\n        """\n        if accept_type is None:\n            accept_type = self._get_type()\n        if send_type is None:\n            send_type = self._get_type()\n        return {\'Content-Type\': \'application/%s\' % send_type,\n                \'Accept\': \'application/%s\' % accept_type}\n\n                              header, if one isn\'t provided the object var\n                              TYPE will be used\n        :rtype: dict\n        :return: The dictionary of headers which can be used in the headers\n                 dict for outgoing request\n        """\n        if accept_type is None:\n            accept_type = \'json\'\n        if send_type is None:\n            send_type = \'json\'\n        headers = {\'Content-Type\': \'application/%s\' % send_type,\n                   \'Accept\': \'application/%s\' % accept_type}\n        headers.update(profiler.serialize_as_http_headers())\n        return headers\n\n    def __str__(self):\n        STRING_LIMIT = 80\n        str_format = ("service:%s, base_url:%s, "\n                      "filters: %s, build_interval:%s, build_timeout:%s"\n                      "\\ntoken:%s..., \\nheaders:%s...")\n        return str_format % (self.service, self.base_url,\n                             self.filters, self.build_interval,\n                             self.build_timeout,\n                             str(self.token)[0:STRING_LIMIT],\n                             str(self.get_headers())[0:STRING_LIMIT])\n\n    @property\n    def user(self):\n        """The username used for requests\n\n        :rtype: string\n        :return: The username being used for requests\n        """\n\n        return self.auth_provider.credentials.username\n\n    @property\n    def user_id(self):\n        """The user_id used for requests\n\n        :rtype: string\n        :return: The user id being used for requests\n        """\n        return self.auth_provider.credentials.user_id\n\n    @property\n    def tenant_name(self):\n        """The tenant/project being used for requests\n\n        :rtype: string\n        :return: The tenant/project name being used for requests\n        """\n        return self.auth_provider.credentials.tenant_name\n\n    @property\n    def project_id(self):\n        """The project id being used for requests\n\n        :rtype: string\n        :return: The project id being used for requests\n        """\n        return self.auth_provider.credentials.tenant_id\n\n    @property\n    def tenant_id(self):\n        """The tenant/project id being used for requests\n\n        :rtype: string\n        :return: The tenant/project id being used for requests\n        """\n        # NOTE(ralonsoh): this property should be deprecated, reference\n        # blueprint adopt-oslo-versioned-objects-for-db.\n        versionutils.report_deprecated_feature(\n            self.LOG, \'"tenant_id" property is deprecated for removal, use \'\n                      \'"project_id" instead\')\n        return self.project_id\n\n    @property\n    def password(self):\n        """The password being used for requests\n\n        :rtype: string\n        :return: The password being used for requests\n        """\n        return self.auth_provider.credentials.password\n\n    @property\n    def base_url(self):\n        return self.auth_provider.base_url(filters=self.filters)\n\n    @property\n    def token(self):\n        return self.auth_provider.get_token()\n\n    @property\n    def filters(self):\n        _filters = dict(\n            service=self.service,\n            endpoint_type=self.endpoint_type,\n            region=self.region,\n            name=self.name\n        )\n        if self.api_version is not None:\n            _filters[\'api_version\'] = self.api_version\n        if self._skip_path:\n            _filters[\'skip_path\'] = self._skip_path\n        return _filters\n\n    def skip_path(self):\n        """When set, ignore the path part of the base URL from the catalog"""\n        self._skip_path = True\n\n    def reset_path(self):\n        """When reset, use the base URL from the catalog as-is"""\n        self._skip_path = False\n\n    @classmethod\n    def expected_success(cls, expected_code, read_code):\n        """Check expected success response code against the http response\n\n        :param int expected_code: The response code that is expected.\n                                  Optionally a list of integers can be used\n                                  to specify multiple valid success codes\n        :param int read_code: The response code which was returned in the\n                              response\n        :raises AssertionError: if the expected_code isn\'t a valid http success\n                                response code\n        :raises exceptions.InvalidHttpSuccessCode: if the read code isn\'t an\n                                                   expected http success code\n        """\n        if not isinstance(read_code, int):\n            raise TypeError("\'read_code\' must be an int instead of (%s)"\n                            % type(read_code))\n\n        assert_msg = ("This function only allowed to use for HTTP status "\n                      "codes which explicitly defined in the RFC 7231 & 4918. "\n                      "{0} is not a defined Success Code!"\n                      ).format(expected_code)\n        if isinstance(expected_code, list):\n            for code in expected_code:\n                assert code in HTTP_SUCCESS + HTTP_REDIRECTION, assert_msg\n        else:\n            assert expected_code in HTTP_SUCCESS + HTTP_REDIRECTION, assert_msg\n\n        # NOTE(afazekas): the http status code above 400 is processed by\n        # the _error_checker method\n        if read_code < 400:\n            pattern = ("Unexpected http success status code {0}, "\n                       "The expected status code is {1}")\n            if ((not isinstance(expected_code, list) and\n                 (read_code != expected_code)) or\n                (isinstance(expected_code, list) and\n                 (read_code not in expected_code))):\n                details = pattern.format(read_code, expected_code)\n                raise exceptions.InvalidHttpSuccessCode(details)\n\n    def post(self, url, body, headers=None, extra_headers=False,\n             chunked=False):\n        """Send a HTTP POST request using keystone auth\n\n        :param str url: the relative url to send the post request to\n        :param dict body: the request body\n        :param dict headers: The headers to use for the request\n        :param bool extra_headers: Boolean value than indicates if the headers\n                                   returned by the get_headers() method are to\n                                   be used but additional headers are needed in\n                                   the request pass them in as a dict.\n        :param bool chunked: sends the body with chunked encoding\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        :rtype: tuple\n        """\n        return self.request(\'POST\', url, extra_headers, headers, body, chunked)\n\n    def get(self, url, headers=None, extra_headers=False):\n        """Send a HTTP GET request using keystone service catalog and auth\n\n        :param str url: the relative url to send the get request to\n        :param dict headers: The headers to use for the request\n        :param bool extra_headers: Boolean value than indicates if the headers\n                                   returned by the get_headers() method are to\n                                   be used but additional headers are needed in\n                                   the request pass them in as a dict.\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        :rtype: tuple\n        """\n        return self.request(\'GET\', url, extra_headers, headers)\n\n    def delete(self, url, headers=None, body=None, extra_headers=False):\n        """Send a HTTP DELETE request using keystone service catalog and auth\n\n        :param str url: the relative url to send the delete request to\n        :param dict headers: The headers to use for the request\n        :param dict body: the request body\n        :param bool extra_headers: Boolean value than indicates if the headers\n                                   returned by the get_headers() method are to\n                                   be used but additional headers are needed in\n                                   the request pass them in as a dict.\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        :rtype: tuple\n        """\n        return self.request(\'DELETE\', url, extra_headers, headers, body)\n\n    def patch(self, url, body, headers=None, extra_headers=False):\n        """Send a HTTP PATCH request using keystone service catalog and auth\n\n        :param str url: the relative url to send the patch request to\n        :param dict body: the request body\n        :param dict headers: The headers to use for the request\n        :param bool extra_headers: Boolean value than indicates if the headers\n                                   returned by the get_headers() method are to\n                                   be used but additional headers are needed in\n                                   the request pass them in as a dict.\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        :rtype: tuple\n        """\n        return self.request(\'PATCH\', url, extra_headers, headers, body)\n\n    def put(self, url, body, headers=None, extra_headers=False, chunked=False):\n        """Send a HTTP PUT request using keystone service catalog and auth\n\n        :param str url: the relative url to send the put request to\n        :param dict body: the request body\n        :param dict headers: The headers to use for the request\n        :param bool extra_headers: Boolean value than indicates if the headers\n                                   returned by the get_headers() method are to\n                                   be used but additional headers are needed in\n                                   the request pass them in as a dict.\n        :param bool chunked: sends the body with chunked encoding\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        :rtype: tuple\n        """\n        return self.request(\'PUT\', url, extra_headers, headers, body, chunked)\n\n    def head(self, url, headers=None, extra_headers=False):\n        """Send a HTTP HEAD request using keystone service catalog and auth\n\n        :param str url: the relative url to send the head request to\n        :param dict headers: The headers to use for the request\n        :param bool extra_headers: Boolean value than indicates if the headers\n                                   returned by the get_headers() method are to\n                                   be used but additional headers are needed in\n                                   the request pass them in as a dict.\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        :rtype: tuple\n        """\n        return self.request(\'HEAD\', url, extra_headers, headers)\n\n    def copy(self, url, headers=None, extra_headers=False):\n        """Send a HTTP COPY request using keystone service catalog and auth\n\n        :param str url: the relative url to send the copy request to\n        :param dict headers: The headers to use for the request\n        :param bool extra_headers: Boolean value than indicates if the headers\n                                   returned by the get_headers() method are to\n                                   be used but additional headers are needed in\n                                   the request pass them in as a dict.\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        :rtype: tuple\n        """\n        return self.request(\'COPY\', url, extra_headers, headers)\n\n    def get_versions(self):\n        """Get the versions on an endpoint from the keystone catalog\n\n        This method will make a GET request on the baseurl from the keystone\n        catalog to return a list of API versions. It is expected that a GET\n        on the endpoint in the catalog will return a list of supported API\n        versions.\n\n        :return: tuple with response headers and list of version numbers\n        :rtype: tuple\n        """\n        resp, body = self.get(\'\')\n        body = self._parse_resp(body)\n        versions = map(lambda x: x[\'id\'], body)\n        return resp, versions\n\n    def _get_request_id(self, resp):\n        for i in (\'x-openstack-request-id\', \'x-compute-request-id\'):\n            if i in resp:\n                return resp[i]\n        return ""\n\n    def _get_global_request_id(self, resp):\n        if \'x-openstack-request-id\' in resp:\n            return resp[\'x-openstack-request-id\']\n        return \'\'\n\n    def _safe_body(self, body, maxlen=4096):\n        # convert a structure into a string safely\n        try:\n            text = str(body)\n        except UnicodeDecodeError:\n            # if this isn\'t actually text, return marker that\n            return "<BinaryData: removed>"\n        if len(text) > maxlen:\n            return text[:maxlen]\n        else:\n            return text\n\n    def _log_request_start(self, method, req_url):\n        caller_name = test_utils.find_test_caller()\n        if self.trace_requests and re.search(self.trace_requests, caller_name):\n            self.LOG.debug(\'Starting Request (%s): %s %s\', caller_name,\n                           method, req_url)\n\n    def _log_request_full(self, resp, req_headers=None, req_body=None,\n                          resp_body=None, extra=None):\n        if \'X-Auth-Token\' in req_headers:\n            req_headers[\'X-Auth-Token\'] = \'<omitted>\'\n        if \'X-Subject-Token\' in req_headers:\n            req_headers[\'X-Subject-Token\'] = \'<omitted>\'\n        # A shallow copy is sufficient\n        resp_log = resp.copy()\n        if \'x-subject-token\' in resp_log:\n            resp_log[\'x-subject-token\'] = \'<omitted>\'\n        log_fmt = """Request - Headers: %s\n        Body: %s\n    Response - Headers: %s\n        Body: %s"""\n\n        self.LOG.debug(\n            log_fmt,\n            str(req_headers),\n            self._safe_body(req_body),\n            str(resp_log),\n            self._safe_body(resp_body),\n            extra=extra)\n\n    def _log_request(self, method, req_url, resp,\n                     secs="", req_headers=None,\n                     req_body=None, resp_body=None):\n        if req_headers is None:\n            req_headers = {}\n        # if we have the request id, put it in the right part of the log\n        extra = {\n            \'request_id\': self._get_request_id(resp),\n            \'global_request_id\': self._get_global_request_id(resp),\n        }\n        # NOTE(sdague): while we still have 6 callers to this function\n        # we\'re going to just provide work around on who is actually\n        # providing timings by gracefully adding no content if they don\'t.\n        # Once we\'re down to 1 caller, clean this up.\n        caller_name = test_utils.find_test_caller()\n        if secs:\n            secs = " %.3fs" % secs\n        self.LOG.info(\n            \'Request (%s): %s %s %s%s\',\n            caller_name,\n            resp[\'status\'],\n            method,\n            req_url,\n            secs,\n            extra=extra)\n\n        # Also look everything at DEBUG if you want to filter this\n        # out, don\'t run at debug.\n        if self.LOG.isEnabledFor(logging.DEBUG):\n            self._log_request_full(resp, req_headers, req_body,\n                                   resp_body, extra)\n\n    def _parse_resp(self, body, top_key_to_verify=None):\n        try:\n            body = json.loads(body)\n        except ValueError:\n            return body\n\n        # We assume, that if the first value of the deserialized body\'s\n        # item set is a dict or a list, that we just return the first value\n        # of deserialized body.\n        # Essentially "cutting out" the first placeholder element in a body\n        # that looks like this:\n        #\n        #  {\n        #    "users": [\n        #      ...\n        #    ]\n        #  }\n        try:\n            # Ensure there are not more than one top-level keys\n            # NOTE(freerunner): Ensure, that JSON is not nullable to\n            # to prevent StopIteration Exception\n            if not hasattr(body, "keys") or len(body.keys()) != 1:\n                return body\n            # Just return the "wrapped" element\n            first_key, first_item = tuple(body.items())[0]\n            if isinstance(first_item, (dict, list)):\n                if top_key_to_verify is not None:\n                    msg_args = {\n                        \'top_key\': top_key_to_verify,\n                        \'actual_key\': first_key,\n                    }\n                    assert_msg = ("The expected top level key is "\n                                  "\'%(top_key)s\' but we found "\n                                  "\'%(actual_key)s\'." % msg_args)\n                    assert top_key_to_verify == first_key, assert_msg\n                return first_item\n        except (ValueError, IndexError):\n            pass\n        return body\n\n    def response_checker(self, method, resp, resp_body):\n        """A sanity check on the response from a HTTP request\n\n        This method does a sanity check on whether the response from an HTTP\n        request conforms the HTTP RFC.\n\n        :param str method: The HTTP verb of the request associated with the\n                           response being passed in.\n        :param resp: The response headers\n        :param resp_body: The body of the response\n        :raises ResponseWithNonEmptyBody: If the response with the status code\n                                          is not supposed to have a body\n        :raises ResponseWithEntity: If the response code is 205 but has an\n                                    entity\n        """\n        if (resp.status in set((204, 205, 304)) or resp.status < 200 or\n                method.upper() == \'HEAD\') and resp_body:\n            raise exceptions.ResponseWithNonEmptyBody(status=resp.status)\n        # NOTE(afazekas):\n        # If the HTTP Status Code is 205\n        #   \'The response MUST NOT include an entity.\'\n        # A HTTP entity has an entity-body and an \'entity-header\'.\n        # In the HTTP response specification (Section 6) the \'entity-header\'\n        # \'generic-header\' and \'response-header\' are in OR relation.\n        # All headers not in the above two group are considered as entity\n        # header in every interpretation.\n\n        if (resp.status == 205 and\n            0 != len(set(resp.keys()) - set((\'status\',)) -\n                     self.response_header_lc - self.general_header_lc)):\n            raise exceptions.ResponseWithEntity()\n        # NOTE(afazekas)\n        # Now the swift sometimes (delete not empty container)\n        # returns with non json error response, we can create new rest class\n        # for swift.\n        # Usually RFC2616 says error responses SHOULD contain an explanation.\n        # The warning is normal for SHOULD/SHOULD NOT case\n\n        # Likely it will cause an error\n        if method != \'HEAD\' and not resp_body and resp.status >= 400:\n            self.LOG.warning("status >= 400 response with empty body")\n\n    def _request(self, method, url, headers=None, body=None, chunked=False):\n        """A simple HTTP request interface."""\n        # Authenticate the request with the auth provider\n        req_url, req_headers, req_body = self.auth_provider.auth_request(\n            method, url, headers, body, self.filters)\n\n        resp, resp_body = self.raw_request(\n            req_url, method, headers=req_headers, body=req_body,\n            chunked=chunked\n        )\n        # Verify HTTP response codes\n        self.response_checker(method, resp, resp_body)\n\n        return resp, resp_body\n\n    def raw_request(self, url, method, headers=None, body=None, chunked=False,\n                    log_req_body=None):\n        """Send a raw HTTP request without the keystone catalog or auth\n\n        This method sends a HTTP request in the same manner as the request()\n        method, however it does so without using keystone auth or the catalog\n        to determine the base url. Additionally no response handling is done\n        the results from the request are just returned.\n\n        :param str url: Full url to send the request\n        :param str method: The HTTP verb to use for the request\n        :param dict headers: Headers to use for the request. If none are\n                             specified, then the headers returned from the\n                             get_headers() method are used. If the request\n                             explicitly requires no headers use an empty dict.\n        :param str body: Body to send with the request\n        :param bool chunked: sends the body with chunked encoding\n        :param str log_req_body: Whether to log the request body or not.\n                                 It is default to None which means request\n                                 body is safe to log otherwise pass any string\n                                 you want to log in place of request body.\n                                 For example: \'<omitted>\'\n        :rtype: tuple\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        """\n        if headers is None:\n            headers = self.get_headers()\n        # Do the actual request, and time it\n        start = time.time()\n        self._log_request_start(method, url)\n        resp, resp_body = self.http_obj.request(\n            url, method, headers=headers,\n            body=body, chunked=chunked)\n        end = time.time()\n        req_body = body if log_req_body is None else log_req_body\n        self._log_request(method, url, resp, secs=(end - start),\n                          req_headers=headers, req_body=req_body,\n                          resp_body=resp_body)\n        return resp, resp_body\n\n    def request(self, method, url, extra_headers=False, headers=None,\n                body=None, chunked=False):\n        """Send a HTTP request with keystone auth and using the catalog\n\n        This method will send an HTTP request using keystone auth in the\n        headers and the catalog to determine the endpoint to use for the\n        baseurl to send the request to. Additionally\n\n        When a response is received it will check it to see if an error\n        response was received. If it was an exception will be raised to enable\n        it to be handled quickly.\n\n        This method will also handle rate-limiting, if a 413 response code is\n        received it will retry the request after waiting the \'retry-after\'\n        duration from the header.\n\n        :param str method: The HTTP verb to use for the request\n        :param str url: Relative url to send the request to\n        :param bool extra_headers: Boolean value than indicates if the headers\n                                   returned by the get_headers() method are to\n                                   be used but additional headers are needed in\n                                   the request pass them in as a dict.\n        :param dict headers: Headers to use for the request. If none are\n                             specified, then the headers returned from the\n                             get_headers() method are used. If the request\n                             explicitly requires no headers use an empty dict.\n        :param str body: Body to send with the request\n        :param bool chunked: sends the body with chunked encoding\n        :rtype: tuple\n        :return: a tuple with the first entry containing the response headers\n                 and the second the response body\n        :raises UnexpectedContentType: If the content-type of the response\n                                       isn\'t an expect type\n        :raises Unauthorized: If a 401 response code is received\n        :raises Forbidden: If a 403 response code is received\n        :raises NotFound: If a 404 response code is received\n        :raises BadRequest: If a 400 response code is received\n        :raises Gone: If a 410 response code is received\n        :raises Conflict: If a 409 response code is received\n        :raises PreconditionFailed: If a 412 response code is received\n        :raises OverLimit: If a 413 response code is received and retry-after\n                           is not in the response body or its retry operation\n                           exceeds the limits defined by the server\n        :raises RateLimitExceeded: If a 413 response code is received and\n                                   retry-after is in the response body and\n                                   its retry operation does not exceeds the\n                                   limits defined by the server\n        :raises InvalidContentType: If a 415 response code is received\n        :raises UnprocessableEntity: If a 422 response code is received\n        :raises InvalidHTTPResponseBody: The response body wasn\'t valid JSON\n                                         and couldn\'t be parsed\n        :raises NotImplemented: If a 501 response code is received\n        :raises ServerFault: If a 500 response code is received\n        :raises UnexpectedResponseCode: If a response code above 400 is\n                                        received and it doesn\'t fall into any\n                                        of the handled checks\n        """\n        # if extra_headers is True\n        # default headers would be added to headers\n        retry = 0\n\n        if headers is None:\n            # NOTE(vponomaryov): if some client do not need headers,\n            # it should explicitly pass empty dict\n            headers = self.get_headers()\n        elif extra_headers:\n            try:\n                headers.update(self.get_headers())\n            except (ValueError, TypeError):\n                headers = self.get_headers()\n\n        resp, resp_body = self._request(method, url, headers=headers,\n                                        body=body, chunked=chunked)\n\n        while (resp.status == 413 and\n               \'retry-after\' in resp and\n                not self.is_absolute_limit(\n                    resp, self._parse_resp(resp_body)) and\n                retry < MAX_RECURSION_DEPTH):\n            retry += 1\n            delay = self._get_retry_after_delay(resp)\n            self.LOG.debug(\n                "Sleeping %s seconds based on retry-after header", delay\n            )\n            time.sleep(delay)\n            resp, resp_body = self._request(method, url,\n                                            headers=headers, body=body)\n        self._error_checker(resp, resp_body)\n        return resp, resp_body\n\n    def _get_retry_after_delay(self, resp):\n        """Extract the delay from the retry-after header.\n\n        This supports both integer and HTTP date formatted retry-after headers\n        per RFC 2616.\n\n        :param resp: The response containing the retry-after headers\n        :rtype: int\n        :return: The delay in seconds, clamped to be at least 1 second\n        :raises ValueError: On failing to parse the delay\n        """\n        delay = None\n        try:\n            delay = int(resp[\'retry-after\'])\n        except (ValueError, KeyError):\n            pass\n\n        try:\n            retry_timestamp = self._parse_http_date(resp[\'retry-after\'])\n            date_timestamp = self._parse_http_date(resp[\'date\'])\n            delay = int(retry_timestamp - date_timestamp)\n        except (ValueError, OverflowError, KeyError):\n            pass\n\n        if delay is None:\n            raise ValueError(\n                "Failed to parse retry-after header %r as either int or "\n                "HTTP-date." % resp.get(\'retry-after\')\n            )\n\n        # Retry-after headers do not have sub-second precision. Clients may\n        # receive a delay of 0. After sleeping 0 seconds, we would (likely) hit\n        # another 413. To avoid this, always sleep at least 1 second.\n        return max(1, delay)\n\n    def _parse_http_date(self, val):\n        """Parse an HTTP date, like \'Fri, 31 Dec 1999 23:59:59 GMT\'.\n\n        Return an epoch timestamp (float), as returned by time.mktime().\n        """\n        parts = email.utils.parsedate(val)\n        if not parts:\n            raise ValueError("Failed to parse date %s" % val)\n        return time.mktime(parts)\n\n    def _error_checker(self, resp, resp_body):\n\n        # NOTE(mtreinish): Check for httplib response from glance_http. The\n        # object can\'t be used here because importing httplib breaks httplib2.\n        # If another object from a class not imported were passed here as\n        # resp this could possibly fail\n        if str(type(resp)) == "<type \'instance\'>":\n            ctype = resp.getheader(\'content-type\')\n        else:\n            try:\n                ctype = resp[\'content-type\']\n            # NOTE(mtreinish): Keystone delete user responses doesn\'t have a\n            # content-type header. (They don\'t have a body) So just pretend it\n            # is set.\n            except KeyError:\n                ctype = \'application/json\'\n\n        # It is not an error response\n        if resp.status < 400:\n            return\n\n        # NOTE(zhipengh): There is a purposefully duplicate of content-type\n        # with the only difference is with or without spaces, as specified\n        # in RFC7231.\n        JSON_ENC = [\'application/json\', \'application/json; charset=utf-8\',\n                    \'application/json;charset=utf-8\']\n\n        # NOTE(mtreinish): This is for compatibility with Glance and swift\n        # APIs. These are the return content types that Glance api v1\n        # (and occasionally swift) are using.\n        # NOTE(zhipengh): There is a purposefully duplicate of content-type\n        # with the only difference is with or without spaces, as specified\n        # in RFC7231.\n        TXT_ENC = [\'text/plain\', \'text/html\', \'text/html; charset=utf-8\',\n                   \'text/plain; charset=utf-8\', \'text/html;charset=utf-8\',\n                   \'text/plain;charset=utf-8\']\n\n        if ctype.lower() in JSON_ENC:\n            parse_resp = True\n        elif ctype.lower() in TXT_ENC:\n            parse_resp = False\n        else:\n            raise exceptions.UnexpectedContentType(str(resp.status),\n                                                   resp=resp)\n\n        if resp.status == 401:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.Unauthorized(resp_body, resp=resp)\n\n        if resp.status == 403:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.Forbidden(resp_body, resp=resp)\n\n        if resp.status == 404:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.NotFound(resp_body, resp=resp)\n\n        if resp.status == 400:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.BadRequest(resp_body, resp=resp)\n\n        if resp.status == 410:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.Gone(resp_body, resp=resp)\n\n        if resp.status == 409:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.Conflict(resp_body, resp=resp)\n\n        if resp.status == 412:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.PreconditionFailed(resp_body, resp=resp)\n\n        if resp.status == 413:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            if self.is_absolute_limit(resp, resp_body):\n                raise exceptions.OverLimit(resp_body, resp=resp)\n            else:\n                raise exceptions.RateLimitExceeded(resp_body, resp=resp)\n\n        if resp.status == 415:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.InvalidContentType(resp_body, resp=resp)\n\n        if resp.status == 422:\n            if parse_resp:\n                resp_body = self._parse_resp(resp_body)\n            raise exceptions.UnprocessableEntity(resp_body, resp=resp)\n\n        if resp.status in (500, 501):\n            message = resp_body\n            if parse_resp:\n                try:\n                    resp_body = self._parse_resp(resp_body)\n                except ValueError:\n                    # If response body is a non-json string message.\n                    # Use resp_body as is and raise InvalidResponseBody\n                    # exception.\n                    raise exceptions.InvalidHTTPResponseBody(message)\n                else:\n                    if isinstance(resp_body, dict):\n                        # I\'m seeing both computeFault\n                        # and cloudServersFault come back.\n                        # Will file a bug to fix, but leave as is for now.\n                        if \'cloudServersFault\' in resp_body:\n                            message = resp_body[\'cloudServersFault\'][\'message\']\n                        elif \'computeFault\' in resp_body:\n                            message = resp_body[\'computeFault\'][\'message\']\n                        elif \'error\' in resp_body:\n                            message = resp_body[\'error\'][\'message\']\n                        elif \'message\' in resp_body:\n                            message = resp_body[\'message\']\n                    else:\n                        message = resp_body\n\n            if resp.status == 501:\n                raise exceptions.NotImplemented(resp_body, resp=resp,\n                                                message=message)\n            else:\n                raise exceptions.ServerFault(resp_body, resp=resp,\n                                             message=message)\n\n        if resp.status >= 400:\n            raise exceptions.UnexpectedResponseCode(str(resp.status),\n                                                    resp=resp)\n\n    def is_absolute_limit(self, resp, resp_body):\n        if (not isinstance(resp_body, abc.Mapping) or\n                \'retry-after\' not in resp):\n            return True\n        return \'exceed\' in resp_body.get(\'message\', \'blabla\')\n\n    def wait_for_resource_deletion(self, id, *args, **kwargs):\n        """Waits for a resource to be deleted\n\n        This method will loop over is_resource_deleted until either\n        is_resource_deleted returns True or the build timeout is reached. This\n        depends on is_resource_deleted being implemented\n\n        :param str id: The id of the resource to check\n        :raises TimeoutException: If the build_timeout has elapsed and the\n                                  resource still hasn\'t been deleted\n        """\n        start_time = int(time.time())\n        while True:\n            if self.is_resource_deleted(id, *args, **kwargs):\n                return\n            if int(time.time()) - start_time >= self.build_timeout:\n                message = (\'Failed to delete %(resource_type)s %(id)s within \'\n                           \'the required time (%(timeout)s s). Timer started \'\n                           \'at %(start_time)s. Timer ended at %(end_time)s. \'\n                           \'Waited for %(wait_time)s s.\' %\n                           {\'resource_type\': self.resource_type, \'id\': id,\n                            \'timeout\': self.build_timeout,\n                            \'start_time\': start_time,\n                            \'end_time\': int(time.time()),\n                            \'wait_time\': int(time.time()) - start_time})\n                caller = test_utils.find_test_caller()\n                if caller:\n                    message = \'(%s) %s\' % (caller, message)\n                raise exceptions.TimeoutException(message)\n            time.sleep(self.build_interval)\n\n    def wait_for_resource_activation(self, id):\n        """Waits for a resource to become active\n\n        This method will loop over is_resource_active until either\n        is_resource_active returns True or the build timeout is reached. This\n        depends on is_resource_active being implemented\n\n        :param str id: The id of the resource to check\n        :raises TimeoutException: If the build_timeout has elapsed and the\n                                  resource still hasn\'t been active\n        """\n        start_time = int(time.time())\n        while True:\n            if self.is_resource_active(id):\n                return\n            if int(time.time()) - start_time >= self.build_timeout:\n                message = (\'Failed to reach active state %(resource_type)s \'\n                           \'%(id)s within the required time (%(timeout)s s).\' %\n                           {\'resource_type\': self.resource_type, \'id\': id,\n                            \'timeout\': self.build_timeout})\n                caller = test_utils.find_test_caller()\n                if caller:\n                    message = \'(%s) %s\' % (caller, message)\n                raise exceptions.TimeoutException(message)\n            time.sleep(self.build_interval)\n\n    def is_resource_deleted(self, id):\n        """Subclasses override with specific deletion detection."""\n        message = (\'"%s" does not implement is_resource_deleted\'\n                   % self.__class__.__name__)\n        raise NotImplementedError(message)\n\n    def is_resource_active(self, id):\n        """Subclasses override with specific active detection."""\n        message = (\'"%s" does not implement is_resource_active\'\n                   % self.__class__.__name__)\n        raise NotImplementedError(message)\n\n    @property\n    def resource_type(self):\n        """Returns the primary type of resource this client works with."""\n        return \'resource\'\n\n    @classmethod\n    def validate_response(cls, schema, resp, body):\n        # Only check the response if the status code is a success code\n        # TODO(cyeoh): Eventually we should be able to verify that a failure\n        # code if it exists is something that we expect. This is explicitly\n        # declared in the V3 API and so we should be able to export this in\n        # the response schema. For now we\'ll ignore it.\n        if resp.status in HTTP_SUCCESS + HTTP_REDIRECTION:\n            cls.expected_success(schema[\'status_code\'], resp.status)\n\n            # Check the body of a response\n            body_schema = schema.get(\'response_body\')\n            if body_schema:\n                try:\n                    jsonschema.validate(body, body_schema,\n                                        cls=JSONSCHEMA_VALIDATOR,\n                                        format_checker=FORMAT_CHECKER)\n                except jsonschema.ValidationError as ex:\n                    msg = ("HTTP response body is invalid (%s)" % ex)\n                    raise exceptions.InvalidHTTPResponseBody(msg)\n            else:\n                if body:\n                    msg = ("HTTP response body should not exist (%s)" % body)\n                    raise exceptions.InvalidHTTPResponseBody(msg)\n\n            # Check the header of a response\n            header_schema = schema.get(\'response_header\')\n            if header_schema:\n                try:\n                    jsonschema.validate(resp, header_schema,\n                                        cls=JSONSCHEMA_VALIDATOR,\n                                        format_checker=FORMAT_CHECKER)\n                except jsonschema.ValidationError as ex:\n                    msg = ("HTTP response header is invalid (%s)" % ex)\n                    raise exceptions.InvalidHTTPResponseHeader(msg)\n\n    def _get_base_version_url(self):\n        # TODO(oomichi): This method can be used for auth\'s replace_version().\n        # So it is nice to have common logic for the maintenance.\n        endpoint = self.base_url\n        url = urllib.parse.urlsplit(endpoint)\n        new_path = re.split(r\'(^|/)+v\\d+(\\.\\d+)?\', url.path)[0]\n        url = list(url)\n        url[2] = new_path + \'/\'\n        return urllib.parse.urlunsplit(url)\n\n\nclass ResponseBody(dict):\n    """Class that wraps an http response and dict body into a single value.\n\n    Callers that receive this object will normally use it as a dict but\n    can extract the response if needed.\n    """\n\n    def __init__(self, response, body=None):\n        body_data = body or {}\n        self.update(body_data)\n        self.response = response\n\n    def __str__(self):\n        body = super(ResponseBody, self).__str__()\n        return "response: %s\\nBody: %s" % (self.response, body)\n\n\nclass ResponseBodyData(object):\n    """Class that wraps an http response and string data into a single value.\n\n    """\n\n    def __init__(self, response, data):\n        self.response = response\n        self.data = data\n\n    def __str__(self):\n        return "response: %s\\nBody: %s" % (self.response, self.data)\n\n\nclass ResponseBodyList(list):\n    """Class that wraps an http response and list body into a single value.\n\n    Callers that receive this object will normally use it as a list but\n    can extract the response if needed.\n    """\n\n    def __init__(self, response, body=None):\n        body_data = body or []\n        self.extend(body_data)\n        self.response = response\n\n    def __str__(self):\n        body = super(ResponseBodyList, self).__str__()\n        return "response: %s\\nBody: %s" % (self.response, body)\n', 'message': 'Merge "Remove deprecated TYPE in rest_client"', 'author_name': 'Gerrit Code Review', 'lang': 'py'}