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]
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

View File

@ -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/"

View File

@ -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:

View File

@ -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: