# Path: vitrageclient/exceptions.py
# class CommandError(Exception):
# class ClientException(Exception):
#     def __init__(self, code, message=None, request_id=None,
#                  url=None, method=None):
#     def __str__(self):
# def from_response(resp, url, method):


from vitrageclient import exceptions as exc
from keystoneauth1 import adapter as keystoneauth
from oslo_utils import importutils
# under the License.



profiler_web = importutils.try_import('osprofiler.web')


# noinspection PyPep8Naming
def Client(version, *args, **kwargs):
    module = importutils.import_versioned_module('vitrageclient',
                                                 version, 'client')
    client_class = getattr(module, 'Client')
    return client_class(*args, **kwargs)


class VitrageClient(keystoneauth.Adapter):
    def request(self, url, method, **kwargs):
        headers = kwargs.setdefault('headers', {})
        headers.setdefault('Accept', 'application/json')

        if profiler_web:
            # no header will be added if profiler is not initialized
            headers.update(profiler_web.get_trace_id_headers())

        raise_exc = kwargs.pop('raise_exc', True)
        resp = super(VitrageClient, self).request(url, method,
                                                  raise_exc=False,
                                                  **kwargs)

        if raise_exc and resp.status_code >= 400:

# Path: vitrageclient/exceptions.py
# class CommandError(Exception):
# class ClientException(Exception):
#     def __init__(self, code, message=None, request_id=None,
#                  url=None, method=None):
#     def __str__(self):
# def from_response(resp, url, method):


# Path: vitrageclient/client.py
from vitrageclient import exceptions as exc
from keystoneauth1 import adapter as keystoneauth
from oslo_utils import importutils

# under the License.



profiler_web = importutils.try_import('osprofiler.web')


# noinspection PyPep8Naming
def Client(version, *args, **kwargs):
    module = importutils.import_versioned_module('vitrageclient',
                                                 version, 'client')
    client_class = getattr(module, 'Client')
    return client_class(*args, **kwargs)


class VitrageClient(keystoneauth.Adapter):
    def request(self, url, method, **kwargs):
        headers = kwargs.setdefault('headers', {})
        headers.setdefault('Accept', 'application/json')

        if profiler_web:
            # no header will be added if profiler is not initialized
            headers.update(profiler_web.get_trace_id_headers())

        raise_exc = kwargs.pop('raise_exc', True)
        resp = super(VitrageClient, self).request(url, method,
                                                  raise_exc=False,
                                                  **kwargs)

        if raise_exc and resp.status_code >= 400:

            raise exc.from_response(resp, url, method)