f
This commit is contained in:
23
API/loc.py
23
API/loc.py
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user