1 # Copyright 2018 Red Hat, Inc. and others. All Rights Reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from requests import sessions
18 logger = logging.getLogger('common.odl_client')
21 class OpenDaylightRestClient(object):
23 def __init__(self, url, username, password, timeout):
24 super(OpenDaylightRestClient, self).__init__()
26 self.timeout = timeout
27 self.session = sessions.Session()
28 self.session.auth = (username, password)
30 def get(self, urlpath='', data=None):
31 return self.request('get', urlpath, data)
33 def put(self, urlpath='', data=None):
34 return self.request('put', urlpath, data)
36 def delete(self, urlpath='', data=None):
37 return self.request('delete', urlpath, data)
39 def request(self, method, urlpath='', data=None):
40 headers = {'Content-Type': 'application/json'}
41 url = '/'.join([self.url, urlpath])
43 "Sending METHOD (%(method)s) URL (%(url)s) JSON (%(data)s)",
44 {'method': method, 'url': url, 'data': data})
45 return self.session.request(
46 method, url=url, headers=headers, data=data, timeout=self.timeout)