AnkiRequestsClient -> HttpClient

This commit is contained in:
Damien Elmes 2020-01-19 11:33:27 +10:00
parent 09e47fbc36
commit ef8bb61fc3
4 changed files with 22 additions and 23 deletions

View file

@ -17,7 +17,7 @@ HTTP_BUF_SIZE = 64 * 1024
ProgressCallback = Callable[[int, int], None] ProgressCallback = Callable[[int, int], None]
class AnkiRequestsClient: class HttpClient:
verify = True verify = True
timeout = 60 timeout = 60
@ -68,7 +68,7 @@ class AnkiRequestsClient:
# allow user to accept invalid certs in work/school settings # allow user to accept invalid certs in work/school settings
if os.environ.get("ANKI_NOVERIFYSSL"): if os.environ.get("ANKI_NOVERIFYSSL"):
AnkiRequestsClient.verify = False HttpClient.verify = False
import warnings import warnings

View file

@ -17,9 +17,12 @@ from anki.db import DB, DBError
from anki.utils import checksum, devMode, ids2str, intTime, platDesc, versionWithBuild from anki.utils import checksum, devMode, ids2str, intTime, platDesc, versionWithBuild
from . import hooks from . import hooks
from .httpclient import AnkiRequestsClient from .httpclient import HttpClient
from .lang import ngettext from .lang import ngettext
# add-on compat
AnkiRequestsClient = HttpClient
class UnexpectedSchemaChange(Exception): class UnexpectedSchemaChange(Exception):
pass pass
@ -467,7 +470,7 @@ class HttpSyncer:
def __init__(self, hkey=None, client=None, hostNum=None) -> None: def __init__(self, hkey=None, client=None, hostNum=None) -> None:
self.hkey = hkey self.hkey = hkey
self.skey = checksum(str(random.random()))[:8] self.skey = checksum(str(random.random()))[:8]
self.client = client or AnkiRequestsClient() self.client = client or HttpClient()
self.postVars: Dict[str, str] = {} self.postVars: Dict[str, str] = {}
self.hostNum = hostNum self.hostNum = hostNum
self.prefix = "sync/" self.prefix = "sync/"

View file

@ -22,7 +22,7 @@ from send2trash import send2trash
import aqt import aqt
import aqt.forms import aqt.forms
from anki.httpclient import AnkiRequestsClient from anki.httpclient import HttpClient
from anki.lang import _, ngettext from anki.lang import _, ngettext
from anki.utils import intTime from anki.utils import intTime
from aqt.qt import * from aqt.qt import *
@ -734,9 +734,7 @@ class GetAddons(QDialog):
###################################################################### ######################################################################
def download_addon( def download_addon(client: HttpClient, id: int) -> Union[DownloadOk, DownloadError]:
client: AnkiRequestsClient, id: int
) -> Union[DownloadOk, DownloadError]:
"Fetch a single add-on from AnkiWeb." "Fetch a single add-on from AnkiWeb."
try: try:
resp = client.get(aqt.appShared + f"download/{id}?v=2.1") 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( def download_and_install_addon(
mgr: AddonManager, client: AnkiRequestsClient, id: int mgr: AddonManager, client: HttpClient, id: int
) -> DownloadLogEntry: ) -> DownloadLogEntry:
"Download and install a single add-on." "Download and install a single add-on."
result = download_addon(client, id) result = download_addon(client, id)
@ -810,9 +808,7 @@ def download_and_install_addon(
class DownloaderInstaller(QObject): class DownloaderInstaller(QObject):
progressSignal = pyqtSignal(int, int) progressSignal = pyqtSignal(int, int)
def __init__( def __init__(self, parent: QWidget, mgr: AddonManager, client: HttpClient) -> None:
self, parent: QWidget, mgr: AddonManager, client: AnkiRequestsClient
) -> None:
QObject.__init__(self, parent) QObject.__init__(self, parent)
self.mgr = mgr self.mgr = mgr
self.client = client self.client = client
@ -875,10 +871,10 @@ def download_addons(
mgr: AddonManager, mgr: AddonManager,
ids: List[int], ids: List[int],
on_done: Callable[[List[DownloadLogEntry]], None], on_done: Callable[[List[DownloadLogEntry]], None],
client: Optional[AnkiRequestsClient] = None, client: Optional[HttpClient] = None,
) -> None: ) -> None:
if client is None: if client is None:
client = AnkiRequestsClient() client = HttpClient()
downloader = DownloaderInstaller(parent, mgr, client) downloader = DownloaderInstaller(parent, mgr, client)
downloader.download(ids, on_done=on_done) 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.""" """Fetch update info from AnkiWeb in one or more batches."""
all_info: List[UpdateInfo] = [] all_info: List[UpdateInfo] = []
@ -903,7 +899,7 @@ def fetch_update_info(client: AnkiRequestsClient, ids: List[int]) -> List[Update
def _fetch_update_info_batch( def _fetch_update_info_batch(
client: AnkiRequestsClient, chunk: Iterable[str] client: HttpClient, chunk: Iterable[str]
) -> Iterable[UpdateInfo]: ) -> Iterable[UpdateInfo]:
"""Get update info from AnkiWeb. """Get update info from AnkiWeb.
@ -931,16 +927,16 @@ def check_and_prompt_for_updates(
mgr: AddonManager, mgr: AddonManager,
on_done: Callable[[List[DownloadLogEntry]], None], 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) handle_update_info(parent, mgr, client, items, on_done)
check_for_updates(mgr, on_updates_received) check_for_updates(mgr, on_updates_received)
def check_for_updates( 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(): def check():
return fetch_update_info(client, mgr.enabled_addon_ids()) return fetch_update_info(client, mgr.enabled_addon_ids())
@ -971,7 +967,7 @@ def check_for_updates(
def handle_update_info( def handle_update_info(
parent: QWidget, parent: QWidget,
mgr: AddonManager, mgr: AddonManager,
client: AnkiRequestsClient, client: HttpClient,
items: List[UpdateInfo], items: List[UpdateInfo],
on_done: Callable[[List[DownloadLogEntry]], None], on_done: Callable[[List[DownloadLogEntry]], None],
) -> None: ) -> None:
@ -991,7 +987,7 @@ def handle_update_info(
def prompt_to_update( def prompt_to_update(
parent: QWidget, parent: QWidget,
mgr: AddonManager, mgr: AddonManager,
client: AnkiRequestsClient, client: HttpClient,
ids: List[int], ids: List[int],
on_done: Callable[[List[DownloadLogEntry]], None], on_done: Callable[[List[DownloadLogEntry]], None],
) -> None: ) -> None:

View file

@ -20,7 +20,7 @@ from bs4 import BeautifulSoup
import aqt import aqt
import aqt.sound import aqt.sound
from anki.hooks import runFilter from anki.hooks import runFilter
from anki.httpclient import AnkiRequestsClient from anki.httpclient import HttpClient
from anki.lang import _ from anki.lang import _
from anki.notes import Note from anki.notes import Note
from anki.utils import checksum, isWin, namedtmp, stripHTMLMedia 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() filecontents = urllib.request.urlopen(req).read()
else: else:
reqs = AnkiRequestsClient() reqs = HttpClient()
reqs.timeout = 30 reqs.timeout = 30
r = reqs.get(url) r = reqs.get(url)
if r.status_code != 200: if r.status_code != 200: