nsb_installation: updates
[yardstick.git] / yardstick / common / httpClient.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10
11 import logging
12 import time
13
14 from oslo_serialization import jsonutils
15 import requests
16
17 logger = logging.getLogger(__name__)
18
19
20 class HttpClient(object):
21
22     def post(self, url, data, timeout=0):
23         data = jsonutils.dump_as_bytes(data)
24         headers = {'Content-Type': 'application/json'}
25         t_end = time.time() + timeout
26         while True:
27             try:
28                 response = requests.post(url, data=data, headers=headers)
29                 result = response.json()
30                 logger.debug('The result is: %s', result)
31                 return result
32             except Exception:
33                 if time.time() > t_end:
34                     logger.exception('')
35                     raise
36             time.sleep(1)
37
38     def get(self, url):
39         response = requests.get(url)
40         return response.json()