|
| 1 | +# |
| 2 | +# CDDL HEADER START |
| 3 | +# |
| 4 | +# The contents of this file are subject to the terms of the |
| 5 | +# Common Development and Distribution License (the "License"). |
| 6 | +# You may not use this file except in compliance with the License. |
| 7 | +# |
| 8 | +# See LICENSE.txt included in this distribution for the specific |
| 9 | +# language governing permissions and limitations under the License. |
| 10 | +# |
| 11 | +# When distributing Covered Code, include this CDDL HEADER in each |
| 12 | +# file and include the License file at LICENSE.txt. |
| 13 | +# If applicable, add the following below this CDDL HEADER, with the |
| 14 | +# fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | +# information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | +# |
| 17 | +# CDDL HEADER END |
| 18 | +# |
| 19 | + |
| 20 | +# |
| 21 | +# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 22 | +# |
| 23 | + |
| 24 | +import requests |
| 25 | +import traceback |
| 26 | +from urllib.parse import urlparse |
| 27 | + |
| 28 | + |
| 29 | +def get(logger, uri, params=None, headers=None): |
| 30 | + try: |
| 31 | + proxies = get_proxies(uri) |
| 32 | + return requests.get(uri, params=params, proxies=proxies) |
| 33 | + except Exception: |
| 34 | + logger.debug(traceback.format_exc()) |
| 35 | + return None |
| 36 | + |
| 37 | + |
| 38 | +def delete(logger, uri, params=None, headers=None): |
| 39 | + try: |
| 40 | + proxies = get_proxies(uri) |
| 41 | + return requests.delete(uri, params=params, proxies=proxies) |
| 42 | + except Exception: |
| 43 | + logger.debug(traceback.format_exc()) |
| 44 | + return None |
| 45 | + |
| 46 | + |
| 47 | +def post(logger, uri, headers=None, data=None): |
| 48 | + rv = None |
| 49 | + try: |
| 50 | + proxies = get_proxies(uri) |
| 51 | + rv = requests.post(uri, data=data, headers=headers, proxies=proxies) |
| 52 | + except Exception: |
| 53 | + logger.debug(traceback.format_exc()) |
| 54 | + return None |
| 55 | + |
| 56 | + return rv |
| 57 | + |
| 58 | + |
| 59 | +def put(logger, uri, headers=None, data=None): |
| 60 | + rv = None |
| 61 | + try: |
| 62 | + proxies = get_proxies(uri) |
| 63 | + rv = requests.put(uri, data=data, headers=headers, proxies=proxies) |
| 64 | + except Exception: |
| 65 | + logger.debug(traceback.format_exc()) |
| 66 | + return None |
| 67 | + |
| 68 | + return rv |
| 69 | + |
| 70 | + |
| 71 | +def get_uri(*uri_parts): |
| 72 | + return '/'.join(s.strip('/') for s in uri_parts) |
| 73 | + |
| 74 | + |
| 75 | +def is_localhost_url(url): |
| 76 | + """ |
| 77 | + Check if given URL is based on localhost. |
| 78 | + """ |
| 79 | + |
| 80 | + o = urlparse(url) |
| 81 | + return o.hostname in ['localhost', '127.0.0.1', '::1'] |
| 82 | + |
| 83 | + |
| 84 | +def get_proxies(url): |
| 85 | + """ |
| 86 | + For localhost based requests it is undesirable to use proxies. |
| 87 | + """ |
| 88 | + if is_localhost_url(url): |
| 89 | + return {'http': None, 'https': None} |
| 90 | + else: |
| 91 | + return None |
0 commit comments