Merge "Log VM OS version, Sample VNF branch/commit ID"
[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                 response.raise_for_status()
30                 result = response.json()
31                 logger.debug('The result is: %s', result)
32                 return result
33             except Exception: # pylint: disable=broad-except
34                 if time.time() > t_end:
35                     logger.exception('')
36                     raise
37             time.sleep(1)
38
39     def get(self, url):
40         response = requests.get(url)
41         response.raise_for_status()
42         return response.json()