perf: reuse connections and cache Anna's Archive download counts (#1546)

Two small changes that stop Shelfmark from opening a new HTTPS
connection for requests that go to the same host over and over.

Anna's Archive download counts. Every search result gets its count from
`/dyn/md5/inline_info/<md5>`, and each of those was a bare
`requests.get`, so a search with 50 results opened 50 TCP and TLS
connections to the mirror. `search_books` also runs once per title
variant, and the variants of one book mostly find the same files, so the
same md5 was looked up again for every variant that found it, and again
when the release list was reopened. The lookups now share one
`requests.Session`, and a successful count is kept for five minutes in a
bounded `CacheService` (the same class the metadata cache uses, as its
own instance, so it does not depend on the metadata cache toggle). A
lookup that fails or comes back without a count is not cached, so it is
retried on the next search just like today. The URL, headers and 5
second timeout are unchanged.

Debrid clients. Real-Debrid, AllDebrid, Debrid-Link and TorBox also used
the bare `requests` helpers. The download handler polls `get_status`
every two seconds for each active job, so every poll was a fresh TLS
handshake with the debrid API. Each of those modules now keeps one
`requests.Session` and sends its API calls through it, the way the
qBittorrent, Deluge, Prowlarr and Newznab clients already do. Headers,
timeouts, `verify` and error handling are still passed per call exactly
as before. The existing tests that patched
`<module>.requests.get/post/request` now patch
`<module>._session.<verb>` instead; nothing else in them changed.

How I checked it: each change has a test that fails on main and passes
here. They count the requests that reach `requests.Session.request` and
the sessions built while doing it, with no network. On main, three polls
of each debrid client build three sessions, and two overlapping batches
of AA results look `bbb` up twice. There are also tests for the count
cache expiring (with a faked clock) and for a failed lookup not being
cached. Full suite: 4432 passed, 48 skipped. ruff and basedpyright are
clean on the changed files.

I also looked at caching `/api/releases` on the server and left it out.
The release modal already caches each book's results per source and
content type in the browser for five minutes (longer for IRC and slskd
through `cache_ttl_seconds`), and that cache survives closing and
reopening the modal, so reopening it does not run a new search today. A
server cache would only help after a page reload, and it would quietly
undo the modal's "apply filters" and manual search refetches, which drop
the browser cache on purpose, unless the frontend also learned to send a
bypass. That did not seem worth it for this PR.
This commit is contained in:
splitsec2 authored and GitHub committed 2026-10-22 20:24:40 -04:00
1 parent 08fdb7ea87
commit 2668705bfa
10 files changed
+223 -44

No files matched your search

+14 -9
View File
@@ -37,6 +37,11 @@ from shelfmark.download.network import get_ssl_verify
logger = setup_logger(__name__)
_API_BASE = "https://api.alldebrid.com/v4"
# One session for every client instance, so the status poll every couple of seconds
# reuses a keep-alive connection instead of opening a new TLS connection each time.
_session = requests.Session()
_AGENT = "shelfmark"
_ALLDEBRID_CLIENT_ERRORS = (
@@ -177,7 +182,7 @@ class AllDebridClient(DownloadClient):
return False, "AllDebrid API Key is required"
try:
url = f"{_API_BASE}/user"
resp = requests.get(
resp = _session.get(
url,
headers=self._auth_headers(),
timeout=_STATUS_TIMEOUT,
@@ -261,7 +266,7 @@ class AllDebridClient(DownloadClient):
if isinstance(upload, DebridMagnet):
api_url = f"{_API_BASE}/magnet/upload"
entries_key = "magnets"
resp = requests.post(
resp = _session.post(
api_url,
headers=self._auth_headers(),
data={"magnets[]": upload.magnet_url},
@@ -271,7 +276,7 @@ class AllDebridClient(DownloadClient):
else:
api_url = f"{_API_BASE}/magnet/upload/file"
entries_key = "files"
resp = requests.post(
resp = _session.post(
api_url,
headers=self._auth_headers(),
files={
@@ -335,7 +340,7 @@ class AllDebridClient(DownloadClient):
# Ask AllDebrid for the current magnet status.
try:
status_url = f"{_API_BASE.replace('/v4', '/v4.1')}/magnet/status"
resp = requests.post(
resp = _session.post(
status_url,
headers=self._auth_headers(),
data={"id": download_id},
@@ -369,7 +374,7 @@ class AllDebridClient(DownloadClient):
"""Delete the magnet from AllDebrid and clean up local files."""
try:
url = f"{_API_BASE}/magnet/delete"
requests.post(
_session.post(
url,
headers=self._auth_headers(),
data={"id": download_id},
@@ -516,7 +521,7 @@ class AllDebridClient(DownloadClient):
# 2. POST unlock (primary method).
try:
resp = requests.post(
resp = _session.post(
unlock_url,
headers=headers,
data={"link": link},
@@ -545,7 +550,7 @@ class AllDebridClient(DownloadClient):
get_url = (
f"{_API_BASE}/link/unlock?agent={_AGENT}&apikey={self._api_key}&link={encoded}"
)
resp = requests.get(
resp = _session.get(
get_url,
headers=headers,
timeout=_API_TIMEOUT,
@@ -601,7 +606,7 @@ class AllDebridClient(DownloadClient):
for _ in range(_DELAYED_POLL_MAX_ATTEMPTS):
time.sleep(_DELAYED_POLL_INTERVAL)
try:
resp = requests.post(
resp = _session.post(
delayed_url,
headers=headers,
data={"id": delayed_id},
@@ -693,7 +698,7 @@ class AllDebridClient(DownloadClient):
) -> list[dict[str, Any]]:
"""Retrieve and flatten the file tree for a magnet."""
url = f"{_API_BASE}/magnet/files"
resp = requests.post(
resp = _session.post(
url,
headers=self._auth_headers(),
data={"id[]": magnet_id},
+5 -1
View File
@@ -49,6 +49,10 @@ logger = setup_logger(__name__)
_API_BASE = "https://debrid-link.com/api/v2"
# One session for every client instance, so the status poll every couple of seconds
# reuses a keep-alive connection instead of opening a new TLS connection each time.
_session = requests.Session()
_DEBRIDLINK_CLIENT_ERRORS = (
AttributeError,
OSError,
@@ -428,7 +432,7 @@ class DebridLinkClient(DownloadClient):
"""
url = f"{_API_BASE}{path}"
try:
resp = requests.request(
resp = _session.request(
method,
url,
headers=self._auth_headers(),
+11 -7
View File
@@ -36,6 +36,10 @@ logger = setup_logger(__name__)
_API_BASE = "https://api.real-debrid.com/rest/1.0"
# One session for every client instance, so the status poll every couple of seconds
# reuses a keep-alive connection instead of opening a new TLS connection each time.
_session = requests.Session()
_REALDEBRID_CLIENT_ERRORS = (
AttributeError,
OSError,
@@ -151,7 +155,7 @@ class RealDebridClient(DownloadClient):
return False, "Real-Debrid API Key is required"
try:
url = f"{_API_BASE}/user"
resp = requests.get(
resp = _session.get(
url,
headers=self._auth_headers(),
timeout=_STATUS_TIMEOUT,
@@ -201,7 +205,7 @@ class RealDebridClient(DownloadClient):
# Select all files so Real-Debrid starts downloading the torrent
select_url = f"{_API_BASE}/torrents/selectFiles/{torrent_id}"
sel_resp = requests.post(
sel_resp = _session.post(
select_url,
headers=self._auth_headers(),
data={"files": "all"},
@@ -239,7 +243,7 @@ class RealDebridClient(DownloadClient):
"""Hand the torrent to Real-Debrid, as a magnet or as a file upload."""
if isinstance(upload, DebridMagnet):
add_url = f"{_API_BASE}/torrents/addMagnet"
resp = requests.post(
resp = _session.post(
add_url,
headers=self._auth_headers(),
data={"magnet": upload.magnet_url},
@@ -250,7 +254,7 @@ class RealDebridClient(DownloadClient):
# addTorrent is a PUT that takes the raw file as the request body,
# not a form field: https://api.real-debrid.com/
add_url = f"{_API_BASE}/torrents/addTorrent"
resp = requests.put(
resp = _session.put(
add_url,
headers={
**self._auth_headers(),
@@ -294,7 +298,7 @@ class RealDebridClient(DownloadClient):
# Query Real-Debrid for torrent info.
try:
info_url = f"{_API_BASE}/torrents/info/{download_id}"
resp = requests.get(
resp = _session.get(
info_url,
headers=self._auth_headers(),
timeout=_STATUS_TIMEOUT,
@@ -320,7 +324,7 @@ class RealDebridClient(DownloadClient):
"""Delete the torrent from Real-Debrid and clean up local files."""
try:
url = f"{_API_BASE}/torrents/delete/{download_id}"
requests.delete(
_session.delete(
url,
headers=self._auth_headers(),
timeout=_STATUS_TIMEOUT,
@@ -477,7 +481,7 @@ class RealDebridClient(DownloadClient):
# Unrestrict the Real-Debrid link to get direct CDN download URL
unrestrict_url = f"{_API_BASE}/unrestrict/link"
unl_resp = requests.post(
unl_resp = _session.post(
unrestrict_url,
headers=self._auth_headers(),
data={"link": link},
+6 -1
View File
@@ -37,6 +37,11 @@ if TYPE_CHECKING:
logger = setup_logger(__name__)
_API_BASE = "https://api.torbox.app/v1/api"
# One session for every client instance, so the status poll every couple of seconds
# reuses a keep-alive connection instead of opening a new TLS connection each time.
_session = requests.Session()
_API_TIMEOUT = 30
_STATUS_TIMEOUT = 15
_WORKER_JOIN_TIMEOUT = 5.0
@@ -320,7 +325,7 @@ class TorBoxClient(DownloadClient):
"verify": get_ssl_verify(url),
**kwargs,
}
request: Any = requests.get if method == "GET" else requests.post
request: Any = _session.get if method == "GET" else _session.post
try:
response = request(url, **request_kwargs)
except requests.exceptions.RequestException as e:
@@ -21,6 +21,7 @@ from bs4.element import NavigableString
from shelfmark.bypass.challenge import MAX_CHALLENGE_HTML_CHARS, challenge_marker
from shelfmark.config.env import DEBUG_SKIP_SOURCES
from shelfmark.core import search_deadline
from shelfmark.core.cache import CacheService
from shelfmark.core.config import config
from shelfmark.core.logger import setup_logger
from shelfmark.core.models import SearchFilters
@@ -822,15 +823,29 @@ def search_books(query: str, filters: SearchFilters) -> tuple[list[BrowseRecord]
return (books, total_count)
# One search asks for the same md5 once per title variant that finds it, and reopening
# the release list asks again. A count that is a few minutes old is fine on a list.
_DOWNLOAD_COUNT_TTL = 300
_DOWNLOAD_COUNT_WORKERS = 5
_download_count_cache = CacheService(max_size=2000)
# Keeps connections to the mirror open between lookups. requests' default pool of 10
# per host already covers _DOWNLOAD_COUNT_WORKERS.
_inline_info_session = requests.Session()
def _fetch_download_count_inline(book_id: str) -> int | None:
"""Fetch the download count for a single book from Anna's Archive inline_info API."""
cached = _download_count_cache.get(book_id)
if isinstance(cached, int):
return cached
try:
url = f"{network.get_aa_base_url()}/dyn/md5/inline_info/{book_id}"
resp = requests.get(url, timeout=5, headers={"Accept": "application/json"})
resp = _inline_info_session.get(url, timeout=5, headers={"Accept": "application/json"})
if resp.status_code == 200:
data = resp.json()
count = data.get("downloads_total")
if count is not None:
_download_count_cache.set(book_id, count, _DOWNLOAD_COUNT_TTL)
return count
except Exception:
logger.debug("Failed to fetch download count for %s", book_id, exc_info=True)
@@ -848,7 +863,7 @@ def _enrich_search_results_with_downloads(books: list[BrowseRecord]) -> None:
# Fetch counts in parallel using the inline_info API (cheaper than summary)
counts: dict[str, int] = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
with concurrent.futures.ThreadPoolExecutor(max_workers=_DOWNLOAD_COUNT_WORKERS) as executor:
futures = {executor.submit(_fetch_download_count_inline, bid): bid for bid in book_ids}
for future in concurrent.futures.as_completed(futures):
bid = futures[future]
@@ -0,0 +1,89 @@
"""Download counts on AA search results are fetched once per md5, over kept-alive connections.
`search_books` runs once per title variant, and the variants of one book mostly find the
same files, so each md5 used to be looked up again for every variant (and again whenever
the release list was reopened), each lookup on a brand new connection.
"""
from unittest.mock import MagicMock
import pytest
import requests
from shelfmark.release_sources import BrowseRecord
from shelfmark.release_sources.direct_download import annas_archive as aa
@pytest.fixture(autouse=True)
def _fresh_cache(monkeypatch):
aa._download_count_cache.clear()
monkeypatch.setattr(aa.network, "get_aa_base_url", lambda: "https://annas-archive.gl")
yield
aa._download_count_cache.clear()
def _records(*ids: str) -> list[BrowseRecord]:
return [
BrowseRecord(id=record_id, title=record_id, source="direct_download") for record_id in ids
]
def _count_lookups(monkeypatch, *, fail: bool = False) -> tuple[list[str], list[object]]:
looked_up: list[str] = []
sessions_built: list[object] = []
def fake_request(_self, method, url, *_args, **_kwargs):
looked_up.append(url.rsplit("/", 1)[-1])
if fail:
raise requests.exceptions.ConnectionError("offline")
return MagicMock(status_code=200, json=MagicMock(return_value={"downloads_total": 7}))
real_init = requests.Session.__init__
def counting_init(self, *args, **kwargs):
sessions_built.append(self)
real_init(self, *args, **kwargs)
monkeypatch.setattr(requests.Session, "request", fake_request)
monkeypatch.setattr(requests.Session, "__init__", counting_init)
return looked_up, sessions_built
def test_overlapping_title_variants_look_each_md5_up_once(monkeypatch):
looked_up, sessions_built = _count_lookups(monkeypatch)
first = _records("aaa", "bbb")
second = _records("bbb", "ccc")
aa._enrich_search_results_with_downloads(first)
aa._enrich_search_results_with_downloads(second)
assert sorted(looked_up) == ["aaa", "bbb", "ccc"]
assert sessions_built == [], "lookups should share one session instead of one each"
for record in first + second:
assert record.info == {"Downloads": ["7"]}
def test_cached_count_expires(monkeypatch):
looked_up, _ = _count_lookups(monkeypatch)
now = [1_000.0]
monkeypatch.setattr("shelfmark.core.cache.time.time", lambda: now[0])
aa._enrich_search_results_with_downloads(_records("aaa"))
now[0] += aa._DOWNLOAD_COUNT_TTL - 1
aa._enrich_search_results_with_downloads(_records("aaa"))
assert looked_up == ["aaa"]
now[0] += 2
aa._enrich_search_results_with_downloads(_records("aaa"))
assert looked_up == ["aaa", "aaa"]
def test_failed_lookup_is_not_cached(monkeypatch):
looked_up, _ = _count_lookups(monkeypatch, fail=True)
records = _records("aaa")
aa._enrich_search_results_with_downloads(records)
aa._enrich_search_results_with_downloads(_records("aaa"))
assert looked_up == ["aaa", "aaa"]
assert records[0].info is None or "Downloads" not in records[0].info
+3 -3
View File
@@ -151,8 +151,8 @@ class TestRealDebridAdd:
ok.json = MagicMock(return_value={"id": "RD1"})
post = MagicMock(return_value=ok)
put = MagicMock(return_value=ok)
monkeypatch.setattr("shelfmark.download.clients.realdebrid.requests.post", post)
monkeypatch.setattr("shelfmark.download.clients.realdebrid.requests.put", put)
monkeypatch.setattr("shelfmark.download.clients.realdebrid._session.post", post)
monkeypatch.setattr("shelfmark.download.clients.realdebrid._session.put", put)
return post, put
def test_proxy_url_is_uploaded_as_a_torrent_file_not_posted_as_a_magnet(self, monkeypatch):
@@ -258,7 +258,7 @@ class TestAllDebridAdd:
return_value={"status": "success", "data": {entries_key: [{"id": 4242}]}}
)
post = MagicMock(return_value=ok)
monkeypatch.setattr("shelfmark.download.clients.alldebrid.requests.post", post)
monkeypatch.setattr("shelfmark.download.clients.alldebrid._session.post", post)
return post
def test_proxy_url_is_uploaded_to_the_file_endpoint(self, monkeypatch):
@@ -0,0 +1,57 @@
"""Debrid status polls reuse one HTTP session instead of opening a connection each time.
The download handler polls `get_status` every couple of seconds for each active job.
A bare `requests.get` builds a throwaway Session per call, so every poll paid for a
fresh TCP and TLS handshake with the debrid API.
"""
import pytest
import requests
from shelfmark.download.clients.alldebrid import AllDebridClient
from shelfmark.download.clients.debridlink import DebridLinkClient
from shelfmark.download.clients.realdebrid import RealDebridClient
from shelfmark.download.clients.torbox import TorBoxClient
_CLIENTS = [
pytest.param(RealDebridClient, "realdebrid", "REALDEBRID_API_KEY", "ABC", id="realdebrid"),
pytest.param(AllDebridClient, "alldebrid", "ALLDEBRID_API_KEY", "123", id="alldebrid"),
pytest.param(DebridLinkClient, "debridlink", "DEBRIDLINK_API_KEY", "abc", id="debridlink"),
pytest.param(TorBoxClient, "torbox", "TORBOX_API_KEY", "42", id="torbox"),
]
@pytest.mark.parametrize(("client_cls", "module", "key_name", "download_id"), _CLIENTS)
def test_status_polls_share_one_session(
monkeypatch, tmp_path, client_cls, module, key_name, download_id
):
monkeypatch.setattr(
f"shelfmark.download.clients.{module}.config.get",
lambda key, default="": {key_name: "api-key"}.get(key, default),
)
monkeypatch.setattr(f"shelfmark.download.clients.{module}.TMP_DIR", tmp_path)
monkeypatch.setattr(client_cls, "_downloads", {})
sent: list[str] = []
def fake_request(_self, method, url, *_args, **_kwargs):
sent.append(url)
raise requests.exceptions.ConnectionError("offline")
sessions_built: list[object] = []
real_init = requests.Session.__init__
def counting_init(self, *args, **kwargs):
sessions_built.append(self)
real_init(self, *args, **kwargs)
client = client_cls()
monkeypatch.setattr(requests.Session, "request", fake_request)
monkeypatch.setattr(requests.Session, "__init__", counting_init)
for _ in range(3):
status = client.get_status(download_id)
assert status.state is not None
assert len(sent) == 3, "each poll should still ask the API once"
assert sessions_built == [], "polls should reuse a session built up front"
+4 -4
View File
@@ -54,12 +54,12 @@ def _mock_fetch(monkeypatch, *, content=b"", status_code=200, error=None):
def _mock_request(monkeypatch, payload, *, status_code=200):
"""Patch requests.request with a single canned Debrid-Link envelope."""
"""Patch the API session with a single canned Debrid-Link envelope."""
response = MagicMock(status_code=status_code)
response.raise_for_status = MagicMock()
response.json = MagicMock(return_value=payload)
request = MagicMock(return_value=response)
monkeypatch.setattr("shelfmark.download.clients.debridlink.requests.request", request)
monkeypatch.setattr("shelfmark.download.clients.debridlink._session.request", request)
return request
@@ -114,7 +114,7 @@ class TestEnvelope:
response = MagicMock(status_code=503)
response.json = MagicMock(side_effect=ValueError("no json"))
monkeypatch.setattr(
"shelfmark.download.clients.debridlink.requests.request",
"shelfmark.download.clients.debridlink._session.request",
MagicMock(return_value=response),
)
client = _client(monkeypatch)
@@ -140,7 +140,7 @@ class TestEnvelope:
def test_a_network_failure_does_not_leak_the_url(self, monkeypatch):
monkeypatch.setattr(
"shelfmark.download.clients.debridlink.requests.request",
"shelfmark.download.clients.debridlink._session.request",
MagicMock(side_effect=requests.ConnectionError("https://debrid-link.com/secret")),
)
client = _client(monkeypatch)
+17 -17
View File
@@ -54,7 +54,7 @@ class TestTorBoxConfiguration:
def test_connection_reports_valid_free_plan(self, monkeypatch):
client = _client(monkeypatch)
get = MagicMock(return_value=_response({"email": "reader@example.com", "plan": 0}))
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.get", get)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.get", get)
assert client.test_connection() == (
True,
@@ -73,7 +73,7 @@ class TestTorBoxConfiguration:
detail="Your token is invalid or has expired.",
)
)
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.get", get)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.get", get)
success, message = client.test_connection()
@@ -87,7 +87,7 @@ class TestTorBoxCreation:
client = _client(monkeypatch)
monkeypatch.setattr("shelfmark.download.clients.torbox.TMP_DIR", tmp_path)
post = MagicMock(return_value=_response({"torrent_id": 42}))
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.post", post)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.post", post)
assert client.add_download(MAGNET, "Dune") == "42"
@@ -104,7 +104,7 @@ class TestTorBoxCreation:
lambda *_args, **_kwargs: DebridTorrentFile(torrent_data=b"torrent"),
)
post = MagicMock(return_value=_response({"torrent_id": 42}))
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.post", post)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.post", post)
client.add_download("https://prowlarr.example/download", "Dune")
@@ -123,7 +123,7 @@ class TestTorBoxCreation:
detail="You have reached your active download limit.",
)
)
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.post", post)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.post", post)
with pytest.raises(RuntimeError, match=r"ACTIVE_LIMIT.*active download limit"):
client.add_download(MAGNET, "Dune")
@@ -132,7 +132,7 @@ class TestTorBoxCreation:
client = _client(monkeypatch)
monkeypatch.setattr("shelfmark.download.clients.torbox.TMP_DIR", tmp_path)
post = MagicMock(return_value=_response({"torrent_id": "x/../../escape"}))
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.post", post)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.post", post)
with pytest.raises(RuntimeError, match="invalid torrent ID"):
client.add_download(MAGNET, "Dune")
@@ -252,7 +252,7 @@ class TestTorBoxStatus:
get = MagicMock(
return_value=_response([{"id": 42, "download_state": "downloading", "progress": 20}])
)
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.get", get)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.get", get)
status = client.get_status("42")
@@ -265,7 +265,7 @@ class TestTorBoxStatus:
get = MagicMock(
return_value=_response([{"id": 42, "download_state": "downloading", "progress": 20}])
)
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.get", get)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.get", get)
try:
client.get_status("0042")
@@ -279,7 +279,7 @@ class TestTorBoxStatus:
client = _client(monkeypatch)
monkeypatch.setattr("shelfmark.download.clients.torbox.TMP_DIR", tmp_path)
get = MagicMock()
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.get", get)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.get", get)
with pytest.raises(RuntimeError, match="invalid torrent ID"):
client.get_status("x/../../escape")
@@ -343,7 +343,7 @@ class TestTorBoxFileRetrieval:
monkeypatch.setattr("shelfmark.download.clients.torbox.download_url", wait_for_cancellation)
monkeypatch.setattr(
"shelfmark.download.clients.torbox.requests.post",
"shelfmark.download.clients.torbox._session.post",
MagicMock(return_value=_response(None)),
)
TorBoxClient._downloads["42"] = state
@@ -369,7 +369,7 @@ class TestTorBoxFileRetrieval:
detail="Token rejected",
)
)
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.get", get)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.get", get)
with pytest.raises(RuntimeError) as excinfo:
client._request_download_link("42", 7)
@@ -381,7 +381,7 @@ class TestTorBoxFileRetrieval:
client = _client(monkeypatch)
signed_url = "HtTpS://cdn.example/Dune?token=signed"
monkeypatch.setattr(
"shelfmark.download.clients.torbox.requests.get",
"shelfmark.download.clients.torbox._session.get",
MagicMock(return_value=_response(signed_url)),
)
@@ -390,7 +390,7 @@ class TestTorBoxFileRetrieval:
@staticmethod
def _link_request(monkeypatch, data: object) -> MagicMock:
get = MagicMock(return_value=_response(data))
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.get", get)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.get", get)
return get
def test_file_link_rejects_http_urls(self, monkeypatch):
@@ -421,7 +421,7 @@ class TestTorBoxFileRetrieval:
logger = MagicMock()
monkeypatch.setattr("shelfmark.download.clients.torbox.logger", logger)
monkeypatch.setattr(
"shelfmark.download.clients.torbox.requests.get",
"shelfmark.download.clients.torbox._session.get",
MagicMock(side_effect=requests.exceptions.ConnectionError(f"token={API_KEY}")),
)
state = _state(tmp_path)
@@ -440,7 +440,7 @@ class TestTorBoxCleanup:
state = _DownloadState(torrent_id="42", name="Dune", target_dir=target_dir)
monkeypatch.setattr("shelfmark.download.clients.torbox.TMP_DIR", tmp_path)
monkeypatch.setattr(
"shelfmark.download.clients.torbox.requests.post",
"shelfmark.download.clients.torbox._session.post",
MagicMock(
return_value=_response(
None, success=False, error="NOT_OWNER", detail="You are not the owner."
@@ -462,7 +462,7 @@ class TestTorBoxCleanup:
escaped_dir = tmp_path.parent / "escape"
escaped_dir.mkdir()
post = MagicMock()
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.post", post)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.post", post)
with pytest.raises(RuntimeError, match="invalid torrent ID"):
client.remove("x/../../escape")
@@ -499,7 +499,7 @@ class TestTorBoxRemoveWorkerTimeout:
thread.is_alive.return_value = True
state.download_thread = thread
post = MagicMock(return_value=_response(None))
monkeypatch.setattr("shelfmark.download.clients.torbox.requests.post", post)
monkeypatch.setattr("shelfmark.download.clients.torbox._session.post", post)
monkeypatch.setattr("shelfmark.download.clients.torbox._WORKER_JOIN_TIMEOUT", 0.01)
TorBoxClient._downloads["42"] = state