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

@ -4,8 +4,14 @@ import typing
from pprint import pprint
from typing import Any, cast
import requests
from . import mediawiki_oauth
class APIError(Exception):
"""Unexpected response from the MediaWiki API."""
wiki_hostname = "en.wikipedia.org"
wiki_api_php = f"https://{wiki_hostname}/w/api.php"
user_agent = "add-links/0.1"
@ -27,10 +33,13 @@ def parse_page(enwiki: str) -> dict[str, Any]:
return parse
def call(params: dict[str, str | int]) -> dict[str, typing.Any]:
def call(params: dict[str, str | int], timeout: int = 4) -> dict[str, typing.Any]:
"""Make GET request to mediawiki API."""
data = mediawiki_oauth.api_post_request(params)
return cast(dict[str, Any], data.json())
r = mediawiki_oauth.api_post_request(params, timeout=timeout)
try:
return cast(dict[str, Any], r.json())
except requests.exceptions.JSONDecodeError:
raise APIError(f"HTTP {r.status_code}: {r.text[:200]!r}")
def article_exists(title: str) -> bool:
@ -92,7 +101,7 @@ def edit_page(
"summary": summary,
"section": section,
}
ret = call(params)
ret = call(params, timeout=30)
if "edit" not in ret:
print("params")
pprint(params)