Moshe | 0bb532c | 2018-02-26 13:39:57 +0200 | [diff] [blame] | 1 | ############################################################################## |
| 2 | # Copyright 2018 EuropeanSoftwareMarketingLtd. |
| 3 | # =================================================================== |
| 4 | # Licensed under the ApacheLicense, Version2.0 (the"License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # software distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | # See the License for the specific language governing permissions and limitations under |
| 12 | # the License |
| 13 | ############################################################################## |
| 14 | # vnftest comment: this is a modified copy of |
| 15 | # yardstick/common/httpClient.py |
| 16 | |
| 17 | from __future__ import absolute_import |
| 18 | |
| 19 | import logging |
| 20 | import time |
| 21 | |
| 22 | from oslo_serialization import jsonutils |
| 23 | import requests |
| 24 | |
| 25 | logger = logging.getLogger(__name__) |
| 26 | |
| 27 | |
| 28 | class HttpClient(object): |
| 29 | |
| 30 | def post(self, url, data, timeout=0): |
| 31 | data = jsonutils.dump_as_bytes(data) |
| 32 | headers = {'Content-Type': 'application/json'} |
| 33 | t_end = time.time() + timeout |
| 34 | while True: |
| 35 | try: |
| 36 | response = requests.post(url, data=data, headers=headers) |
| 37 | result = response.json() |
| 38 | logger.debug('The result is: %s', result) |
| 39 | return result |
| 40 | except Exception: |
| 41 | if time.time() > t_end: |
| 42 | logger.exception('') |
| 43 | raise |
| 44 | time.sleep(1) |
| 45 | |
| 46 | def get(self, url): |
| 47 | response = requests.get(url) |
| 48 | return response.json() |