1"""
2Connection library for Amazon IAM
3
4:depends: requests
5"""
6
7import logging
8import pprint
9import time
10
11try:
12    import requests
13
14    HAS_REQUESTS = True  # pylint: disable=W0612
15except ImportError:
16    HAS_REQUESTS = False  # pylint: disable=W0612
17
18log = logging.getLogger(__name__)
19
20
21def _retry_get_url(url, num_retries=10, timeout=5):
22    """
23    Retry grabbing a URL.
24    Based heavily on boto.utils.retry_url
25    """
26    for i in range(0, num_retries):
27        try:
28            result = requests.get(url, timeout=timeout, proxies={"http": ""})
29            if hasattr(result, "text"):
30                return result.text
31            elif hasattr(result, "content"):
32                return result.content
33            else:
34                return ""
35        except requests.exceptions.HTTPError as exc:
36            return ""
37        except Exception as exc:  # pylint: disable=broad-except
38            pass
39
40        log.warning("Caught exception reading from URL. Retry no. %s", i)
41        log.warning(pprint.pformat(exc))
42        time.sleep(2 ** i)
43    log.error("Failed to read from URL for %s times. Giving up.", num_retries)
44    return ""
45
46
47def _convert_key_to_str(key):
48    """
49    Stolen completely from boto.providers
50    """
51    return key
52