gutendex: send a User-Agent (Cloudflare 403s the default urllib agent)

Gutendex.com sits behind Cloudflare, which rejects urllib's default
`Python-urllib/<ver>` User-Agent with HTTP 403. Both skills therefore failed
on their first real call ("could not reach the Gutendex catalog ... HTTP Error
403"); the hermetic loopback tests cannot catch this by design.

Send an identifying `USER_AGENT` on every request (the /books catalog lookup
and the plain-text download) and assert it in each skill's test so the
regression is caught locally.

Verified with a real run: find_books returned 41 live results for
"jane austen"; read_book downloaded and saved Pride and Prejudice (1342).
This commit is contained in:
gabby committed 2026-10-17 08:18:13 +00:00
1 parent b775069613
commit 52346baf52
4 files changed
+67 -5

No files matched your search

+10 -1
View File
@@ -6,6 +6,9 @@ request's search terms are taken from the user's query and the catalog is
searched by author/title (`search`) or by genre/subject (`topic`) — which one
is a SemIf decision over the real query. A real HTTP failure is reported
honestly; book data is never invented.
Gutendex sits behind Cloudflare, which rejects the default `Python-urllib`
User-Agent with HTTP 403, so every request identifies itself with `USER_AGENT`.
"""
from __future__ import annotations
@@ -29,6 +32,10 @@ CONTRACT = {
"gutendex_base_url": "Base URL of the Gutendex Project Gutenberg API, e.g. https://gutendex.com (no trailing slash needed).",
}
# Gutendex's Cloudflare front blocks the default `Python-urllib/<ver>` agent
# with HTTP 403; a plain, identifying agent is accepted.
USER_AGENT = "semif-agent/1.0"
TIMEOUT_SECONDS = 20
MAX_RESULTS = 5
RESULT_CHARS = 300
@@ -88,7 +95,9 @@ def _get_json(base_url, path, params=None):
url = base_url + path
if params:
url += "?" + urllib.parse.urlencode(params)
http_request = urllib.request.Request(url, headers={"Accept": "application/json"})
http_request = urllib.request.Request(
url, headers={"Accept": "application/json", "User-Agent": USER_AGENT}
)
with urllib.request.urlopen(http_request, timeout=TIMEOUT_SECONDS) as response:
return json.loads(response.read().decode("utf-8"))