Skip to content

Commit

Permalink
Display rate limit from requests.
Browse files Browse the repository at this point in the history
Limits are shown in Warnings summary whether or not the test succeeds.
Limits are also shown in a log message if the test fails.
  • Loading branch information
mrclary committed May 23, 2024
1 parent f33cf21 commit 100ae99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions spyder/plugins/updatemanager/tests/test_update_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def worker():
# ---- Test WorkerUpdate

@pytest.mark.parametrize("version", ["1.0.0", "1000.0.0"])
def test_updates_appenv(qtbot, mocker, version):
def test_updates_appenv(qtbot, mocker, version, caplog):
"""
Test whether or not we offer updates for our installers according to the
current Spyder version.
Expand All @@ -51,9 +51,10 @@ def test_updates_appenv(qtbot, mocker, version):
return_value=("conda-forge", "https://conda.anaconda.org/conda-forge")
)

um = UpdateManagerWidget(None)
um.start_check_update()
qtbot.waitUntil(um.update_thread.isFinished)
with caplog.at_level(logging.DEBUG, logger='spyder.plugins.updatemanager'):
um = UpdateManagerWidget(None)
um.start_check_update()
qtbot.waitUntil(um.update_thread.isFinished)

update_available = um.update_worker.update_available
if version.split('.')[0] == '1':
Expand Down
17 changes: 17 additions & 0 deletions spyder/plugins/updatemanager/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
# (see spyder/__init__.py for details)

# Standard library imports
from datetime import datetime as dt
import logging
import os
import os.path as osp
import shutil
from time import sleep
import traceback
import warnings

# Third party imports
from packaging.version import parse
Expand Down Expand Up @@ -153,6 +155,7 @@ def start(self):
logger.info(f"Checking for updates from {url}")
try:
page = requests.get(url)
rate_limits(page)
page.raise_for_status()
data = page.json()

Expand Down Expand Up @@ -321,3 +324,17 @@ def start(self):
self.sig_ready.emit()
except RuntimeError:
pass


def rate_limits(page):
xrlr = dt.utcfromtimestamp(int(page.headers['X-RateLimit-Reset']))
msg_items = [
"Rate Limits:",
f"Resource: {page.headers['X-RateLimit-Resource']}",
f"Reset: {xrlr}",
f"Limit: {page.headers['X-RateLimit-Limit']:>5s}",
f"Used: {page.headers['X-RateLimit-Used']:>5s}",
f"Remaining: {page.headers['X-RateLimit-Remaining']:>5s}",
]
logger.debug("\n\t".join(msg_items))
warnings.warn(DeprecationWarning("\n\t".join(msg_items)))

0 comments on commit 100ae99

Please sign in to comment.