api_key.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from datetime import datetime
  2. from pydantic import BaseModel
  3. class APIKeyCreate(BaseModel):
  4. """Schema for creating a new API key."""
  5. name: str
  6. can_queue: bool = True
  7. can_control_printer: bool = False
  8. can_read_status: bool = True
  9. can_manage_library: bool = True # Upload / rename / delete own library files + MakerWorld import
  10. can_manage_inventory: bool = True # Inventory writes — SpoolBuddy NFC/scale/system, manual stock edits via API
  11. can_access_cloud: bool = False # Read /cloud/* on the creator's behalf — default off (#1182)
  12. can_update_energy_cost: bool = False # POST /settings/electricity-price only (#1356)
  13. printer_ids: list[int] | None = None # null = all printers
  14. expires_at: datetime | None = None
  15. class APIKeyUpdate(BaseModel):
  16. """Schema for updating an API key."""
  17. name: str | None = None
  18. can_queue: bool | None = None
  19. can_control_printer: bool | None = None
  20. can_read_status: bool | None = None
  21. can_manage_library: bool | None = None
  22. can_manage_inventory: bool | None = None
  23. can_access_cloud: bool | None = None
  24. can_update_energy_cost: bool | None = None
  25. printer_ids: list[int] | None = None
  26. enabled: bool | None = None
  27. expires_at: datetime | None = None
  28. class APIKeyResponse(BaseModel):
  29. """Schema for API key response (without full key)."""
  30. id: int
  31. name: str
  32. key_prefix: str # First 8 chars for identification
  33. user_id: int | None # Owner — NULL on legacy keys created before per-user ownership (#1182)
  34. can_queue: bool
  35. can_control_printer: bool
  36. can_read_status: bool
  37. can_manage_library: bool
  38. can_manage_inventory: bool
  39. can_access_cloud: bool
  40. can_update_energy_cost: bool
  41. printer_ids: list[int] | None
  42. enabled: bool
  43. last_used: datetime | None
  44. created_at: datetime
  45. expires_at: datetime | None
  46. class Config:
  47. from_attributes = True
  48. class APIKeyCreateResponse(APIKeyResponse):
  49. """Response when creating a key - includes full key (shown only once)."""
  50. key: str # Full API key, only shown on creation