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

@ -8,6 +8,8 @@ import requests
from flask import current_app, session
from requests_oauthlib import OAuth1Session
from .api import ua
wiki_hostname = "en.wikipedia.org"
api_url = f"https://{wiki_hostname}/w/api.php"
@ -27,7 +29,7 @@ def get_edit_proxy() -> dict[str, str]:
return {}
def api_post_request(params: dict[str, str | int]) -> requests.Response:
def api_post_request(params: dict[str, str | int], timeout: int = 4) -> requests.Response:
"""HTTP Post using Oauth."""
app = current_app
# url = "https://www.wikidata.org/w/api.php"
@ -39,8 +41,9 @@ def api_post_request(params: dict[str, str | int]) -> requests.Response:
resource_owner_key=session["owner_key"],
resource_owner_secret=session["owner_secret"],
)
oauth.headers.update({"User-Agent": ua})
proxies = get_edit_proxy()
return oauth.post(api_url, data=params, timeout=4, proxies=proxies)
return oauth.post(api_url, data=params, timeout=timeout, proxies=proxies)
def raw_request(params: typing.Mapping[str, str | int]) -> requests.Response:
@ -57,6 +60,7 @@ def raw_request(params: typing.Mapping[str, str | int]) -> requests.Response:
resource_owner_key=session["owner_key"],
resource_owner_secret=session["owner_secret"],
)
oauth.headers.update({"User-Agent": ua})
proxies = get_edit_proxy()
return oauth.get(
api_url + "?" + urllib.parse.urlencode(params), timeout=4, proxies=proxies