forked from platformio/platformio-core
Check cached API result before Internet
This commit is contained in:
@ -621,7 +621,6 @@ def _get_api_result(
|
|||||||
|
|
||||||
|
|
||||||
def get_api_result(url, params=None, data=None, auth=None, cache_valid=None):
|
def get_api_result(url, params=None, data=None, auth=None, cache_valid=None):
|
||||||
internet_on(raise_exception=True)
|
|
||||||
from platformio.app import ContentCache
|
from platformio.app import ContentCache
|
||||||
total = 0
|
total = 0
|
||||||
max_retries = 5
|
max_retries = 5
|
||||||
@ -634,6 +633,10 @@ def get_api_result(url, params=None, data=None, auth=None, cache_valid=None):
|
|||||||
result = cc.get(cache_key)
|
result = cc.get(cache_key)
|
||||||
if result is not None:
|
if result is not None:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
# check internet before and resolve issue with 60 seconds timeout
|
||||||
|
internet_on(raise_exception=True)
|
||||||
|
|
||||||
result = _get_api_result(url, params, data)
|
result = _get_api_result(url, params, data)
|
||||||
if cache_valid:
|
if cache_valid:
|
||||||
with ContentCache() as cc:
|
with ContentCache() as cc:
|
||||||
|
@ -12,11 +12,25 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import pytest
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from platformio import util
|
from platformio import exception, util
|
||||||
|
|
||||||
|
|
||||||
def test_ping_internet_ips():
|
def test_ping_internet_ips():
|
||||||
for ip in util.PING_INTERNET_IPS:
|
for ip in util.PING_INTERNET_IPS:
|
||||||
requests.get("http://%s" % ip, allow_redirects=False, timeout=2)
|
requests.get("http://%s" % ip, allow_redirects=False, timeout=2)
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_internet_offline(monkeypatch, isolated_pio_home):
|
||||||
|
monkeypatch.setattr(util, "_internet_on", lambda: False)
|
||||||
|
with pytest.raises(exception.InternetIsOffline):
|
||||||
|
util.get_api_result("/stats")
|
||||||
|
|
||||||
|
def test_api_cache(monkeypatch, isolated_pio_home):
|
||||||
|
api_kwargs = {"url": "/stats", "cache_valid": "1m"}
|
||||||
|
result = util.get_api_result(**api_kwargs)
|
||||||
|
assert result and "boards" in result
|
||||||
|
monkeypatch.setattr(util, '_internet_on', lambda: False)
|
||||||
|
assert util.get_api_result(**api_kwargs) == result
|
||||||
|
Reference in New Issue
Block a user