Raise LoginNeeded if not logged in

This commit is contained in:
Edward Betts 2023-12-06 09:53:06 +00:00
parent 32a7e939a5
commit 39f9ba31ed

View file

@ -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,