Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 1,180 Bytes
e7b1c7d 2e06b71 e7b1c7d 2e06b71 e7b1c7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | """Shared LegiScan API helper used by get_data.py and fix_pdf_bills.py."""
import os
import logging
import requests
from dotenv import load_dotenv
load_dotenv()
logger = logging.getLogger(__name__)
API_KEY = os.getenv("LEGISCAN_API_KEY")
RATE_LIMIT = 0.2 # seconds between requests
# API call counter — tracks total calls made in this process
_api_call_count = 0
def get_api_call_count():
"""Return the number of LegiScan API calls made so far."""
return _api_call_count
def legi_request(op, params):
"""Make a request to the LegiScan API.
Returns the parsed JSON response on success, or None on failure.
"""
base = "https://api.legiscan.com/"
global _api_call_count
_api_call_count += 1
params.update({"key": API_KEY, "op": op})
try:
resp = requests.get(base, params=params, timeout=10)
resp.raise_for_status()
data = resp.json()
if data.get("status") != "OK":
logger.error(f"API error {op}: {data.get('message', data)}")
return None
return data
except requests.RequestException as e:
logger.error(f"Request failed ({op}): {e}")
return None
|