diff --git a/add_links/wikidata_oauth.py b/add_links/wikidata_oauth.py index d91b66e..9b4de72 100644 --- a/add_links/wikidata_oauth.py +++ b/add_links/wikidata_oauth.py @@ -1,7 +1,10 @@ +"""Wikipedia OAuth.""" + import typing import urllib from typing import cast +import requests from flask import current_app, session from requests_oauthlib import OAuth1Session @@ -9,6 +12,12 @@ wiki_hostname = "en.wikipedia.org" api_url = f"https://{wiki_hostname}/w/api.php" +class LoginNeeded(Exception): + """Not logged in.""" + + pass + + def get_edit_proxy() -> dict[str, str]: """Retrieve proxy information from config.""" edit_proxy = current_app.config.get("EDIT_PROXY") @@ -18,7 +27,7 @@ def get_edit_proxy() -> dict[str, str]: return {} -def api_post_request(params: dict[str, str | int]): +def api_post_request(params: dict[str, str | int]) -> requests.Response: """HTTP Post using Oauth.""" app = current_app # url = "https://www.wikidata.org/w/api.php" @@ -34,12 +43,14 @@ def api_post_request(params: dict[str, str | int]): return oauth.post(api_url, data=params, timeout=4, proxies=proxies) -def raw_request(params: typing.Mapping[str, str | int]): +def raw_request(params: typing.Mapping[str, str | int]) -> requests.Response: """Low-level API request.""" app = current_app # url = "https://www.wikidata.org/w/api.php?" + urlencode(params) client_key = app.config["CLIENT_KEY"] client_secret = app.config["CLIENT_SECRET"] + if "owner_key" not in session or "owner_secret" not in session: + raise LoginNeeded oauth = OAuth1Session( client_key, client_secret=client_secret,