def get_api_result(url, params=None, data=None, auth=None, cache_valid=None): from platformio.app import ContentCache total = 0 max_retries = 5 cache_key = (ContentCache.key_from_args(url, params, data, auth) if cache_valid else None) while total < max_retries: try: with ContentCache() as cc: if cache_key: result = cc.get(cache_key) if result is not None: return result result = _get_api_result(url, params, data) if cache_valid: with ContentCache() as cc: cc.set(cache_key, result, cache_valid) return result except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e: if not internet_on(): raise exception.InternetIsOffline() from platformio.maintenance import in_silence total += 1 if not in_silence(): click.secho( "[API] ConnectionError: {0} (incremented retry: max={1}, " "total={2})".format(e, max_retries, total), fg="yellow") sleep(2 * total) raise exception.APIRequestError( "Could not connect to PlatformIO API Service. " "Please try later.")
def internet_on(raise_exception=False): result = _internet_on() if raise_exception and not result: raise exception.InternetIsOffline() return result