"""Client for the Wikipedia REST API, version 1."""
from dataclasses import dataclass
import click
import desert
import marshmallow
import requests
API_URL: str = "https://{language}.wikipedia.org/api/rest_v1/page/random/summary"
[docs]@dataclass
class Page:
"""Page resource.
Attributes:
title: The title of the Wikipedia page.
extract: A plain text summary.
"""
title: str
extract: str
schema = desert.schema(Page, meta={"unknown": marshmallow.EXCLUDE})
[docs]def random_page(language: str = "en") -> Page:
"""Return a random page.
Performs a GET request to the /page/random/summary endpoint.
Args:
language: The Wikipedia langugage edition. By default,
the English Wikipedia is used ("en").
Returns:
A page resource.
Raises:
ClickException: The HTTP request failed or the HTTP
reponse contained an invalid body.
Example:
>>> from hypermodern_python import wikipedia
>>> page = wikipedia.random_page(language="en")
>>> bool(page.title)
True
"""
url = API_URL.format(language=language)
try:
with requests.get(url) as response:
response.raise_for_status()
data = response.json()
return schema.load(data)
except (requests.RequestException, marshmallow.ValidationError) as error:
err_message = str(error)
raise click.ClickException(err_message)