mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-22 01:07:03 +00:00
fix(googlebooks): report more pages when limit is above the 40 result maximum (#1434)
Kept small on purpose so it's easy to review. Same kind of small fix as #1429 to #1432, so see the question in #1429 about separate PRs versus combined. I'm sending them separately until I hear otherwise. `search_paginated` guesses `has_more` as `len(books) >= options.limit`. Google Books returns at most 40 per page and `search` already caps `maxResults` at 40, but `/api/metadata/search` accepts a limit up to 100. With a limit over 40 a full page of 40 never reaches `limit`, so `has_more` is always False. The UI sends 40, so this only affects direct API callers. The Google provider now overrides `search_paginated` to apply the same cap before the base heuristic runs. A full page of 40 with a limit of 100 reports more results and a short page doesn't. The first test fails on current `main`. Ruff, basedpyright and `tests/metadata` pass. The one red check is the existing clock-dependent test `test_a_queued_torrent_asks_for_a_grace_once`, which fails on any runner that booted recently. It is fixed in #1426 and is not caused by this change.
This commit is contained in:
1 parent
f0f0d46381
commit
61cf0792bd
2 files changed
+39
No files matched your search
@@ -7,6 +7,7 @@ API Documentation: https://developers.google.com/books/docs/v1/using
|
||||
"""
|
||||
|
||||
from contextlib import suppress
|
||||
from dataclasses import replace
|
||||
from http import HTTPStatus
|
||||
from typing import Any, ClassVar
|
||||
|
||||
@@ -32,6 +33,7 @@ from shelfmark.metadata_providers import (
|
||||
MetadataProvider,
|
||||
MetadataSearchOptions,
|
||||
SearchField,
|
||||
SearchResult,
|
||||
SearchType,
|
||||
SortOrder,
|
||||
TextSearchField,
|
||||
@@ -217,6 +219,10 @@ class GoogleBooksProvider(MetadataProvider):
|
||||
ttl_default=600,
|
||||
key_prefix="googlebooks:isbn",
|
||||
)
|
||||
def search_paginated(self, options: MetadataSearchOptions) -> SearchResult:
|
||||
"""Google returns at most 40 per page, so a full page is 40 whatever limit asked for."""
|
||||
return super().search_paginated(replace(options, limit=min(options.limit, 40)))
|
||||
|
||||
def search_by_isbn(self, isbn: str) -> BookMetadata | None:
|
||||
"""Search for a book by ISBN-10 or ISBN-13."""
|
||||
# Clean ISBN (remove hyphens and spaces)
|
||||
|
||||
@@ -143,3 +143,36 @@ class TestGoogleBooksParseVolume:
|
||||
)
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
class _FullPageSession:
|
||||
def __init__(self, count):
|
||||
self.count = count
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
items = [
|
||||
{"id": f"v{i}", "volumeInfo": {"title": f"Book {i}", "authors": ["Alice Author"]}}
|
||||
for i in range(self.count)
|
||||
]
|
||||
return _GoogleBooksResponse({"items": items})
|
||||
|
||||
|
||||
def test_googlebooks_reports_more_pages_when_limit_exceeds_api_maximum():
|
||||
get_metadata_cache().clear()
|
||||
provider = GoogleBooksProvider(api_key="test-key")
|
||||
provider.session = _FullPageSession(40)
|
||||
|
||||
result = provider.search_paginated(MetadataSearchOptions(query="Dune", limit=100))
|
||||
|
||||
assert len(result.books) == 40
|
||||
assert result.has_more is True
|
||||
|
||||
|
||||
def test_googlebooks_reports_no_more_pages_after_a_short_page():
|
||||
get_metadata_cache().clear()
|
||||
provider = GoogleBooksProvider(api_key="test-key")
|
||||
provider.session = _FullPageSession(12)
|
||||
|
||||
result = provider.search_paginated(MetadataSearchOptions(query="Dune", limit=100))
|
||||
|
||||
assert result.has_more is False
|
||||
Reference in new issue
Block a user