Fix User-Agent header, timeouts, and JSON error handling

mediawiki_oauth: set User-Agent on all OAuth1Session instances so
Wikimedia doesn't reject token and API requests with 403; add timeout
parameter to api_post_request (default 4s).

mediawiki_api: add APIError exception; wrap .json() in call() to raise
APIError with status code and response body on decode failure; raise
timeout to 30s for edit POSTs.

api: wrap call_get_diff .json() with the same JSONDecodeError guard,
raising MediawikiError with HTTP status and body on failure.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2026-05-09 18:10:57 +01:00
parent da83f0791d
commit 95ca5f755d
3 changed files with 24 additions and 7 deletions

View file

@ -272,6 +272,10 @@ def call_get_diff(title: str, section_num: int, section_text: str) -> str:
}
s = get_session()
ret = s.post(get_query_url(), data=data).json()
r = s.post(get_query_url(), data=data)
try:
ret = r.json()
except JSONDecodeError:
raise MediawikiError(f"HTTP {r.status_code}: {r.text[:200]!r}")
check_for_error(ret)
return typing.cast(str, ret["query"]["pages"][0]["revisions"][0]["diff"]["body"])