From ef8bb61fc3e0d78df2168ad8e5fa8e2e810c4617 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 19 Jan 2020 11:33:27 +1000 Subject: [PATCH] AnkiRequestsClient -> HttpClient --- pylib/anki/httpclient.py | 4 ++-- pylib/anki/sync.py | 7 +++++-- qt/aqt/addons.py | 30 +++++++++++++----------------- qt/aqt/editor.py | 4 ++-- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/pylib/anki/httpclient.py b/pylib/anki/httpclient.py index 84bcd04c0..e0c054626 100644 --- a/pylib/anki/httpclient.py +++ b/pylib/anki/httpclient.py @@ -17,7 +17,7 @@ HTTP_BUF_SIZE = 64 * 1024 ProgressCallback = Callable[[int, int], None] -class AnkiRequestsClient: +class HttpClient: verify = True timeout = 60 @@ -68,7 +68,7 @@ class AnkiRequestsClient: # allow user to accept invalid certs in work/school settings if os.environ.get("ANKI_NOVERIFYSSL"): - AnkiRequestsClient.verify = False + HttpClient.verify = False import warnings diff --git a/pylib/anki/sync.py b/pylib/anki/sync.py index d138c9965..c962b5e42 100644 --- a/pylib/anki/sync.py +++ b/pylib/anki/sync.py @@ -17,9 +17,12 @@ from anki.db import DB, DBError from anki.utils import checksum, devMode, ids2str, intTime, platDesc, versionWithBuild from . import hooks -from .httpclient import AnkiRequestsClient +from .httpclient import HttpClient from .lang import ngettext +# add-on compat +AnkiRequestsClient = HttpClient + class UnexpectedSchemaChange(Exception): pass @@ -467,7 +470,7 @@ class HttpSyncer: def __init__(self, hkey=None, client=None, hostNum=None) -> None: self.hkey = hkey self.skey = checksum(str(random.random()))[:8] - self.client = client or AnkiRequestsClient() + self.client = client or HttpClient() self.postVars: Dict[str, str] = {} self.hostNum = hostNum self.prefix = "sync/" diff --git a/qt/aqt/addons.py b/qt/aqt/addons.py index af5174851..6c4466843 100644 --- a/qt/aqt/addons.py +++ b/qt/aqt/addons.py @@ -22,7 +22,7 @@ from send2trash import send2trash import aqt import aqt.forms -from anki.httpclient import AnkiRequestsClient +from anki.httpclient import HttpClient from anki.lang import _, ngettext from anki.utils import intTime from aqt.qt import * @@ -734,9 +734,7 @@ class GetAddons(QDialog): ###################################################################### -def download_addon( - client: AnkiRequestsClient, id: int -) -> Union[DownloadOk, DownloadError]: +def download_addon(client: HttpClient, id: int) -> Union[DownloadOk, DownloadError]: "Fetch a single add-on from AnkiWeb." try: resp = client.get(aqt.appShared + f"download/{id}?v=2.1") @@ -789,7 +787,7 @@ def download_encountered_problem(log: List[DownloadLogEntry]) -> bool: def download_and_install_addon( - mgr: AddonManager, client: AnkiRequestsClient, id: int + mgr: AddonManager, client: HttpClient, id: int ) -> DownloadLogEntry: "Download and install a single add-on." result = download_addon(client, id) @@ -810,9 +808,7 @@ def download_and_install_addon( class DownloaderInstaller(QObject): progressSignal = pyqtSignal(int, int) - def __init__( - self, parent: QWidget, mgr: AddonManager, client: AnkiRequestsClient - ) -> None: + def __init__(self, parent: QWidget, mgr: AddonManager, client: HttpClient) -> None: QObject.__init__(self, parent) self.mgr = mgr self.client = client @@ -875,10 +871,10 @@ def download_addons( mgr: AddonManager, ids: List[int], on_done: Callable[[List[DownloadLogEntry]], None], - client: Optional[AnkiRequestsClient] = None, + client: Optional[HttpClient] = None, ) -> None: if client is None: - client = AnkiRequestsClient() + client = HttpClient() downloader = DownloaderInstaller(parent, mgr, client) downloader.download(ids, on_done=on_done) @@ -887,7 +883,7 @@ def download_addons( ###################################################################### -def fetch_update_info(client: AnkiRequestsClient, ids: List[int]) -> List[UpdateInfo]: +def fetch_update_info(client: HttpClient, ids: List[int]) -> List[UpdateInfo]: """Fetch update info from AnkiWeb in one or more batches.""" all_info: List[UpdateInfo] = [] @@ -903,7 +899,7 @@ def fetch_update_info(client: AnkiRequestsClient, ids: List[int]) -> List[Update def _fetch_update_info_batch( - client: AnkiRequestsClient, chunk: Iterable[str] + client: HttpClient, chunk: Iterable[str] ) -> Iterable[UpdateInfo]: """Get update info from AnkiWeb. @@ -931,16 +927,16 @@ def check_and_prompt_for_updates( mgr: AddonManager, on_done: Callable[[List[DownloadLogEntry]], None], ): - def on_updates_received(client: AnkiRequestsClient, items: List[UpdateInfo]): + def on_updates_received(client: HttpClient, items: List[UpdateInfo]): handle_update_info(parent, mgr, client, items, on_done) check_for_updates(mgr, on_updates_received) def check_for_updates( - mgr: AddonManager, on_done: Callable[[AnkiRequestsClient, List[UpdateInfo]], None] + mgr: AddonManager, on_done: Callable[[HttpClient, List[UpdateInfo]], None] ): - client = AnkiRequestsClient() + client = HttpClient() def check(): return fetch_update_info(client, mgr.enabled_addon_ids()) @@ -971,7 +967,7 @@ def check_for_updates( def handle_update_info( parent: QWidget, mgr: AddonManager, - client: AnkiRequestsClient, + client: HttpClient, items: List[UpdateInfo], on_done: Callable[[List[DownloadLogEntry]], None], ) -> None: @@ -991,7 +987,7 @@ def handle_update_info( def prompt_to_update( parent: QWidget, mgr: AddonManager, - client: AnkiRequestsClient, + client: HttpClient, ids: List[int], on_done: Callable[[List[DownloadLogEntry]], None], ) -> None: diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index b0ece2782..37fcd67bb 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -20,7 +20,7 @@ from bs4 import BeautifulSoup import aqt import aqt.sound from anki.hooks import runFilter -from anki.httpclient import AnkiRequestsClient +from anki.httpclient import HttpClient from anki.lang import _ from anki.notes import Note from anki.utils import checksum, isWin, namedtmp, stripHTMLMedia @@ -773,7 +773,7 @@ to a cloze type first, via Edit>Change Note Type.""" ) filecontents = urllib.request.urlopen(req).read() else: - reqs = AnkiRequestsClient() + reqs = HttpClient() reqs.timeout = 30 r = reqs.get(url) if r.status_code != 200: