29 lines
1.0 KiB
Python
29 lines
1.0 KiB
Python
import os
|
|
import requests
|
|
from config import DIGIKEY_API_KEY
|
|
|
|
# Very light wrapper (adjust base/headers to match your DK plan)
|
|
BASE = "https://api.digikey.com/services/partsearch/v2"
|
|
|
|
def suggest_category_from_digikey(mp_or_dkpn: str) -> str | None:
|
|
key = (DIGIKEY_API_KEY or "").strip()
|
|
if not key:
|
|
return None # no key → skip DK lookup
|
|
try:
|
|
hdr = {"X-API-Key": key}
|
|
# Try by part number; if this endpoint differs in your plan, swap as needed:
|
|
r = requests.get(f"{BASE}/parts/{mp_or_dkpn}", headers=hdr, timeout=12)
|
|
if r.status_code != 200:
|
|
return None
|
|
js = r.json() or {}
|
|
# Adjust paths to whatever your DK response returns:
|
|
# Common fields are like "Category", "Family", etc.
|
|
cat = js.get("Category") or js.get("CategoryName") or js.get("Class")
|
|
if isinstance(cat, dict):
|
|
return cat.get("Name") or cat.get("name")
|
|
if isinstance(cat, str) and cat.strip():
|
|
return cat.strip()
|
|
except Exception:
|
|
pass
|
|
return None
|