build: replace httpx with httpx2 in website scripts

httpx has had no release since December 2024.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Vinta Chen 2026-10-31 22:21:52 +08:00
parent eb98d2087d
commit 038f184473
6 changed files with 46 additions and 36 deletions

View file

@ -15,7 +15,7 @@ from datetime import UTC, datetime, timedelta
from itertools import batched
from pathlib import Path
import httpx
import httpx2
from build import extract_github_repo, load_stars
@ -84,7 +84,7 @@ def parse_graphql_response(
return result
def fetch_batch(repos: Sequence[str], client: httpx.Client) -> dict[str, dict]:
def fetch_batch(repos: Sequence[str], client: httpx2.Client) -> dict[str, dict]:
"""Fetch star data for a batch of repos via GitHub GraphQL API."""
query = build_graphql_query(repos)
if not query:
@ -142,9 +142,9 @@ def main() -> None:
now_iso = now.isoformat()
total_batches = (len(to_fetch) + BATCH_SIZE - 1) // BATCH_SIZE
with httpx.Client(
with httpx2.Client(
headers={"Authorization": f"bearer {token}", "Content-Type": "application/json"},
transport=httpx.HTTPTransport(retries=2, proxy=os.environ.get("HTTPS_PROXY") or None),
transport=httpx2.HTTPTransport(retries=2, proxy=os.environ.get("HTTPS_PROXY") or None),
timeout=30,
) as client:
for batch_num, batch in enumerate(batched(to_fetch, BATCH_SIZE), 1):
@ -152,7 +152,7 @@ def main() -> None:
try:
results = fetch_batch(batch, client)
except httpx.HTTPStatusError as e:
except httpx2.HTTPStatusError as e:
print(f"HTTP error {e.response.status_code}", file=sys.stderr)
if e.response.status_code == 401:
print("Error: Invalid GITHUB_TOKEN.", file=sys.stderr)

View file

@ -36,7 +36,7 @@ import re
from datetime import UTC, datetime
from pathlib import Path
import httpx
import httpx2
from readme_parser import parse_readme
@ -86,7 +86,7 @@ def collect_names(readme_text: str) -> list[str]:
def fetch_clickpy(names: list[str]) -> dict[str, int]:
in_list = ", ".join(f"'{name}'" for name in names)
query = f"SELECT project, sum(count) AS downloads FROM pypi.pypi_downloads_per_day WHERE project IN ({in_list}) AND date >= today() - 30 GROUP BY project FORMAT JSON"
resp = httpx.post(CLICKPY_URL, content=query, timeout=60)
resp = httpx2.post(CLICKPY_URL, content=query, timeout=60)
resp.raise_for_status()
return {row["project"]: int(row["downloads"]) for row in resp.json()["data"]}

View file

@ -22,7 +22,7 @@ import sys
import time
from pathlib import Path
import httpx
import httpx2
from fetch_pypi_downloads_via_clickpy import resolve
@ -60,7 +60,7 @@ def main() -> None:
print(f"{arg}: not pip-installable per pypi_name_overrides.json, skipping", file=sys.stderr)
else:
names.append(pkg)
with httpx.Client(headers={"X-API-Key": load_api_key()}, timeout=30) as client:
with httpx2.Client(headers={"X-API-Key": load_api_key()}, timeout=30) as client:
for i, name in enumerate(names):
if i:
time.sleep(SECONDS_BETWEEN_REQUESTS)

View file

@ -254,7 +254,7 @@ class TestMainSkipsFreshCache:
mock_client.__enter__ = MagicMock(return_value=mock_client)
mock_client.__exit__ = MagicMock(return_value=False)
mock_client.post.return_value = mock_response
monkeypatch.setattr("fetch_github_stars.httpx.Client", lambda **kwargs: mock_client)
monkeypatch.setattr("fetch_github_stars.httpx2.Client", lambda **kwargs: mock_client)
main()