api_key.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_manage_maintenance: bool = (
  12. True # Log/reset maintenance items, edit intervals, manage type catalog (#1832 follow-up)
  13. )
  14. can_access_cloud: bool = False # Read /cloud/* on the creator's behalf — default off (#1182)
  15. can_update_energy_cost: bool = False # POST /settings/electricity-price only (#1356)
  16. printer_ids: list[int] | None = None # null = all printers
  17. expires_at: datetime | None = None
  18. class APIKeyUpdate(BaseModel):
  19. """Schema for updating an API key."""
  20. name: str | None = None
  21. can_queue: bool | None = None
  22. can_control_printer: bool | None = None
  23. can_read_status: bool | None = None
  24. can_manage_library: bool | None = None
  25. can_manage_inventory: bool | None = None
  26. can_manage_maintenance: bool | None = None
  27. can_access_cloud: bool | None = None
  28. can_update_energy_cost: bool | None = None
  29. printer_ids: list[int] | None = None
  30. enabled: bool | None = None
  31. expires_at: datetime | None = None
  32. class APIKeyResponse(BaseModel):
  33. """Schema for API key response (without full key)."""
  34. id: int
  35. name: str
  36. key_prefix: str # First 8 chars for identification
  37. user_id: int | None # Owner — NULL on legacy keys created before per-user ownership (#1182)
  38. can_queue: bool
  39. can_control_printer: bool
  40. can_read_status: bool
  41. can_manage_library: bool
  42. can_manage_inventory: bool
  43. can_manage_maintenance: bool
  44. can_access_cloud: bool
  45. can_update_energy_cost: bool
  46. printer_ids: list[int] | None
  47. enabled: bool
  48. last_used: datetime | None
  49. created_at: datetime
  50. expires_at: datetime | None
  51. class Config:
  52. from_attributes = True
  53. class APIKeyCreateResponse(APIKeyResponse):
  54. """Response when creating a key - includes full key (shown only once)."""
  55. key: str # Full API key, only shown on creation