Log Wikimedia API request metrics
This commit is contained in:
parent
eecbd3cfb8
commit
9486d9cb8a
5 changed files with 268 additions and 16 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
import typing
|
||||
|
|
@ -8,6 +10,7 @@ from simplejson.scanner import JSONDecodeError
|
|||
|
||||
from .language import get_current_language
|
||||
from .util import is_disambig
|
||||
from .wikimedia_api_logging import WikimediaApiLogConfig, logged_request
|
||||
|
||||
StrDict = dict[str, typing.Any]
|
||||
|
||||
|
|
@ -15,6 +18,15 @@ ua = (
|
|||
"find-link/2.2 "
|
||||
+ "(https://github.com/EdwardBetts/find_link; contact: edward@4angle.com)"
|
||||
)
|
||||
wikimedia_log_config = WikimediaApiLogConfig(
|
||||
tool="missinglink",
|
||||
log_path=Path(
|
||||
os.environ.get(
|
||||
"MISSINGLINK_WIKIMEDIA_API_LOG", "/var/log/missinglink/wikimedia-api.jsonl"
|
||||
)
|
||||
),
|
||||
user_agent=ua,
|
||||
)
|
||||
re_disambig = re.compile(r"^(.*) \((.*)\)$")
|
||||
|
||||
|
||||
|
|
@ -73,23 +85,22 @@ webpage_error = (
|
|||
)
|
||||
|
||||
|
||||
def _get_active_session() -> requests.sessions.Session:
|
||||
"""Return OAuth session if one is available in Flask context, else plain session."""
|
||||
def _get_active_session() -> tuple[requests.sessions.Session, str]:
|
||||
"""Return active session and auth mode."""
|
||||
try:
|
||||
from flask import g
|
||||
if hasattr(g, "oauth_session") and g.oauth_session is not None:
|
||||
return g.oauth_session # type: ignore[return-value]
|
||||
return g.oauth_session, "oauth" # type: ignore[return-value]
|
||||
except RuntimeError:
|
||||
pass
|
||||
print("WARNING: using unauthenticated session", file=sys.stderr)
|
||||
return get_session()
|
||||
return get_session(), "anon"
|
||||
|
||||
|
||||
def api_get(params: StrDict) -> StrDict:
|
||||
"""Make call to Wikipedia API."""
|
||||
s = _get_active_session()
|
||||
|
||||
r = s.get(get_query_url(), params=params)
|
||||
s, _auth = _get_active_session()
|
||||
r = logged_request(s, wikimedia_log_config, "GET", get_query_url(), params=params)
|
||||
try:
|
||||
ret: StrDict = r.json()
|
||||
except JSONDecodeError:
|
||||
|
|
@ -287,8 +298,8 @@ def call_get_diff(title: str, section_num: int, section_text: str) -> str:
|
|||
"rvdifftotext": section_text.strip(),
|
||||
}
|
||||
|
||||
s = _get_active_session()
|
||||
r = s.post(get_query_url(), data=data)
|
||||
s, _auth = _get_active_session()
|
||||
r = logged_request(s, wikimedia_log_config, "POST", get_query_url(), data=data)
|
||||
try:
|
||||
ret = r.json()
|
||||
except JSONDecodeError:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue