blob: 5453141cef5dde07ad1d8a10eea120d19bc9c4a8 [file] [log] [blame]
Tatyana Leontovich09b7b012017-07-10 12:53:45 +03001import logging
2import urlparse
3
4import requests
5
6from tcp_tests import logger
7
8LOG = logger.logger
9
10logger = logging.getLogger(__name__)
11
12
13class HttpClient(object):
14 def __init__(self, base_url=None, user=None, password=None):
15 self.base_url = base_url
16 self.kwargs = {}
17 if user and password:
18 self.kwargs.update({"auth": (user, password)})
19
20 def set_base_url(self, base_url):
21 self.base_url = base_url
22
23 def request(self, url, method, headers=None, body=None, **kwargs):
24 logger.debug(
25 "Sending request to: {}, body: {}, headers: {}, kwargs: {}".format(
26 url, body, headers, kwargs))
27 if headers is None:
28 headers = {'Content-Type': 'application/json'}
29
30 kwargs.update(self.kwargs)
31 r = requests.request(method, urlparse.urljoin(self.base_url, url),
32 headers=headers, data=body, **kwargs)
33
34 if not r.ok:
35 raise requests.HTTPError(r.content)
36 logger.debug(r.content)
37 return r.headers, r.content
38
39 def post(self, url, body=None, **kwargs):
40 return self.request(url, "POST", body=body, **kwargs)
41
42 def get(self, url, **kwargs):
43 return self.request(url, "GET", **kwargs)
44
45 def put(self, url, body=None, **kwargs):
46 return self.request(url, "PUT", body=body, **kwargs)
47
48 def delete(self, url, **kwargs):
49 return self.request(url, "DELETE", **kwargs)