debug_preset.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. """Debug script to investigate Bambu Cloud preset API responses."""
  3. import asyncio
  4. import json
  5. import sys
  6. from pathlib import Path
  7. # Add backend to path
  8. sys.path.insert(0, str(Path(__file__).parent.parent))
  9. import httpx
  10. from sqlalchemy import create_engine, text
  11. from backend.app.core.config import settings
  12. # Test preset IDs (from the warning logs)
  13. TEST_IDS = ["GFG02", "GFL05", "GFA00", "GFA02", "GFA06"]
  14. def get_token_from_db() -> str | None:
  15. """Get the stored token from the database."""
  16. db_path = settings.base_dir / "bambuddy.db"
  17. engine = create_engine(f"sqlite:///{db_path}")
  18. with engine.connect() as conn:
  19. result = conn.execute(text("SELECT value FROM settings WHERE key = 'bambu_cloud_token'"))
  20. row = result.fetchone()
  21. if row and row[0]:
  22. return row[0]
  23. return None
  24. async def test_preset(setting_id: str, token: str, base_url: str = "https://api.bambulab.com"):
  25. """Test fetching a single preset and show full response."""
  26. url = f"{base_url}/v1/iot-service/api/slicer/setting/{setting_id}"
  27. headers = {
  28. "Authorization": f"Bearer {token}",
  29. "Content-Type": "application/json",
  30. }
  31. print(f"\n{'=' * 60}")
  32. print(f"Testing preset: {setting_id}")
  33. print(f"URL: {url}")
  34. print(f"{'=' * 60}")
  35. async with httpx.AsyncClient() as client:
  36. response = await client.get(url, headers=headers)
  37. print(f"Status: {response.status_code}")
  38. print("\nResponse body:")
  39. try:
  40. data = response.json()
  41. print(json.dumps(data, indent=2))
  42. except Exception:
  43. print(response.text)
  44. return response.status_code
  45. async def main():
  46. # Get token from DB
  47. token = get_token_from_db()
  48. if not token:
  49. print("Could not find token in database.")
  50. print("Make sure you're logged into Bambu Cloud in Bambuddy.")
  51. sys.exit(1)
  52. print(f"Found token in database (length: {len(token)})")
  53. # Allow testing specific preset IDs from command line
  54. test_ids = sys.argv[1:] if len(sys.argv) > 1 else TEST_IDS
  55. # Test each preset
  56. for preset_id in test_ids:
  57. await test_preset(preset_id, token)
  58. if __name__ == "__main__":
  59. asyncio.run(main())