Add support for Python 3
[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
13 from oslo_serialization import jsonutils
14 import requests
15
16 logger = logging.getLogger(__name__)
17
18
19 class HttpClient(object):
20
21     def post(self, url, data):
22         data = jsonutils.dump_as_bytes(data)
23         headers = {'Content-Type': 'application/json'}
24         try:
25             response = requests.post(url, data=data, headers=headers)
26             result = response.json()
27             logger.debug('The result is: %s', result)
28
29             return result
30         except Exception as e:
31             logger.debug('Failed: %s', e)
32             raise
33
34     def get(self, url):
35         response = requests.get(url)
36         return response.json()