Initial move to app, scanning and reading basic part info works, updating info starting to work
This commit is contained in:
28
apis/digikey_api.py
Normal file
28
apis/digikey_api.py
Normal file
@@ -0,0 +1,28 @@
|
||||
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
|
||||
Reference in New Issue
Block a user