This commit is contained in:
2026-01-11 14:46:41 -08:00
parent 1f3de7db1c
commit 275f18cb31
19 changed files with 2741 additions and 394 deletions

View File

@@ -15,31 +15,18 @@ from __future__ import annotations
import json
from typing import Any, Dict, Optional
from API.HTTP import HTTPClient
from .base import API, ApiError
class LOCError(Exception):
class LOCError(ApiError):
pass
class LOCClient:
class LOCClient(API):
"""Minimal client for the public LoC JSON API."""
BASE_URL = "https://www.loc.gov"
def __init__(self, *, timeout: float = 20.0):
self.timeout = float(timeout)
def _get_json(self, path: str, params: Dict[str, Any]) -> Dict[str, Any]:
url = self.BASE_URL.rstrip("/") + "/" + str(path or "").lstrip("/")
try:
with HTTPClient(timeout=self.timeout) as client:
resp = client.get(url, params=params)
resp.raise_for_status()
# httpx.Response.json() exists but keep decoding consistent
return json.loads(resp.content.decode("utf-8"))
except Exception as exc:
raise LOCError(str(exc)) from exc
def __init__(self, *, base_url: str = "https://www.loc.gov", timeout: float = 20.0):
super().__init__(base_url=base_url, timeout=timeout)
def search_chronicling_america(
self,