fix(downloader): correct resume progress and avoid httpx private API

Two bugs found by code review:

1. Progress bar exceeds 100% on resumed downloads because
   Content-Length from a Range response covers only remaining
   bytes. Now adds resume_from for the true total.

2. session.transport reached into httpx.Client._transport, a
   private attribute that may break on httpx upgrades. Transport
   is now stored at construction time instead.
This commit is contained in:
AlexKucera committed 2026-06-06 09:28:33 +02:00
1 parent 2466ef5fa8
commit 8acea390e6
4 files changed
+21 -5

No files matched your search

+7
View File
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Fixed
- **downloader:** Fix progress bar exceeding 100% on resumed downloads.
`Content-Length` from a Range response covers only remaining bytes;
now adds `resume_from` for the true total.
- **session:** Stop reaching into `httpx.Client._transport` (private API).
Transport reference is now stored at construction time.
### Added
+4 -1
View File
@@ -109,7 +109,10 @@ def _stream_to_file(
if content_length is None:
cl_header = resp.headers.get("content-length")
if cl_header:
content_length = int(cl_header)
# Content-Length from a Range response
# covers only remaining bytes; add
# resume_from for true total.
content_length = int(cl_header) + resume_from
progress(downloaded, total=content_length)
else:
progress(downloaded)
+8 -3
View File
@@ -47,6 +47,7 @@ class LibroFmSession:
self._timeout = timeout
self._access_token: str | None = None
self._api_semaphore = threading.Semaphore(API_SEMAPHORE_CAPACITY)
self._transport = transport
self._client = httpx.Client(
base_url=self._base_url,
@@ -56,9 +57,13 @@ class LibroFmSession:
)
@property
def transport(self) -> httpx.BaseTransport:
"""Return the underlying httpx transport for streaming downloads."""
return self._client._transport
def transport(self) -> httpx.BaseTransport | None:
"""Return the httpx transport for streaming downloads.
Stored at construction time to avoid reaching into
``httpx.Client._transport``, which is private API.
"""
return self._transport
def authenticate(self) -> str:
"""OAuth2 password grant → returns access_token.
+2 -1
View File
@@ -712,7 +712,8 @@ class TestTransportProperty:
password="p",
)
assert session.transport is session._client._transport
# Default transport is None when not injected
assert session.transport is None
def test_returns_injected_mock_transport(self):
"""When a MockTransport is injected, session.transport returns it."""