Debian Codegraph @

aodhclient/client.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from keystoneauth1 import adapter
14 from oslo_utils import importutils
15 from osprofiler import web
16
17 from aodhclient import exceptions
18
19
20 def Client(version, *args, **kwargs):
21 module = 'aodhclient.v%s.client' % version
22 module = importutils.import_module(module)
23 client_class = getattr(module, 'Client')
24 return client_class(*args, **kwargs)
25
26
27 class SessionClient(adapter.Adapter):
28 def request(self, url, method, **kwargs):
29 kwargs.setdefault('headers', kwargs.get('headers', {}))
30 # NOTE(sileht): The standard call raises errors from
31 # keystoneauth, where we need to raise the aodhclient errors.
32 raise_exc = kwargs.pop('raise_exc', True)
33 kwargs['headers'].update(web.get_trace_id_headers())
34 resp = super().request(url, method, raise_exc=False, **kwargs)
35
36 if raise_exc and resp.status_code >= 400:
37 raise exceptions.from_response(resp, url, method)
38 return resp