bambu_cloud.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. """
  2. Bambu Lab Cloud API Service
  3. Handles authentication and profile management with Bambu Lab's cloud services.
  4. """
  5. import logging
  6. from datetime import datetime, timedelta, timezone
  7. import httpx
  8. logger = logging.getLogger(__name__)
  9. BAMBU_API_BASE = "https://api.bambulab.com"
  10. BAMBU_API_BASE_CN = "https://api.bambulab.cn"
  11. # Client identity sent to Bambu Lab's cloud services. We identify honestly as
  12. # Bambuddy — the URL in parens makes the source unambiguous so Bambu can
  13. # distinguish our traffic from impersonators. This is the opposite of what the
  14. # OrcaSlicer fork was called out for in the May 2026 Bambu Lab blog post
  15. # ("Setting the record straight on cloud access and community"): we do not
  16. # introduce ourselves as official Bambu Studio.
  17. _USER_AGENT = "Bambuddy/1.0 (+https://github.com/maziggy/bambuddy)"
  18. # Cloudflare protection on Bambu Lab's edge intermittently returns interstitials /
  19. # challenges instead of the JSON the API normally produces (issue #1575). The
  20. # parse error that results is opaque — these helpers detect the CF markers so
  21. # we can surface an actionable message instead of "Invalid response from Bambu Cloud".
  22. _CF_INTERSTITIAL_USER_MESSAGE = (
  23. "Bambu Cloud is temporarily blocking automated requests from your network. "
  24. "This is a Cloudflare protection on Bambu Lab's side, not a Bambuddy issue. "
  25. "Please wait a few minutes and try again. If it persists, signing in to "
  26. "bambulab.com once from a browser on the same network usually clears the "
  27. "challenge."
  28. )
  29. def _detect_cloudflare_challenge(response) -> str | None:
  30. """Return a user-actionable message when the response is a Cloudflare
  31. challenge / mitigation page instead of the JSON the API normally returns.
  32. Triggers on any of:
  33. - body contains "Just a moment..." (CF interactive challenge title)
  34. - body contains "challenges.cloudflare.com" (CF turnstile widget src)
  35. - HTTP 403 with a "cf-mitigated" response header (CF blocked)
  36. - HTTP 503 with a "cf-ray" response header (CF Under Attack mode)
  37. Returns None when the response doesn't look like a CF challenge — callers
  38. fall through to their existing error path.
  39. """
  40. try:
  41. body = response.text or ""
  42. except Exception:
  43. body = ""
  44. if "Just a moment..." in body or "challenges.cloudflare.com" in body:
  45. return _CF_INTERSTITIAL_USER_MESSAGE
  46. try:
  47. status = int(getattr(response, "status_code", 0) or 0)
  48. except (TypeError, ValueError):
  49. status = 0
  50. headers = getattr(response, "headers", {}) or {}
  51. if status == 403 and "cf-mitigated" in headers:
  52. return _CF_INTERSTITIAL_USER_MESSAGE
  53. if status == 503 and "cf-ray" in headers:
  54. return _CF_INTERSTITIAL_USER_MESSAGE
  55. return None
  56. # The `/v1/iot-service/api/slicer/setting` endpoint requires a `version` query
  57. # parameter in the XX.YY.ZZ.WW format Bambu Studio releases use (without it the
  58. # API returns HTTP 400 "field 'version' is not set"; non-matching formats like
  59. # "bambuddy-1.0" return HTTP 422 "Invalid input parameters"). However, Bambu's
  60. # server accepts ANY value within that format — it doesn't validate against a
  61. # release manifest. We therefore use a neutral "1.0.0.0" placeholder that does
  62. # not impersonate any real Bambu Studio release. Our client identity is in the
  63. # User-Agent header.
  64. _SLICER_API_VERSION = "1.0.0.0"
  65. class BambuCloudError(Exception):
  66. """Base exception for Bambu Cloud errors."""
  67. pass
  68. class BambuCloudAuthError(BambuCloudError):
  69. """Authentication related errors."""
  70. pass
  71. _shared_http_client: httpx.AsyncClient | None = None
  72. def set_shared_http_client(client: httpx.AsyncClient | None) -> None:
  73. """Register an app-scoped ``httpx.AsyncClient`` so per-request
  74. ``BambuCloudService`` instances can reuse its connection pool.
  75. Pass ``None`` during shutdown to unregister. The service only holds a
  76. reference (never closes a client it does not own), so region + token
  77. state still stays per-request — this only shares the transport pool.
  78. """
  79. global _shared_http_client
  80. _shared_http_client = client
  81. class BambuCloudService:
  82. """Service for interacting with Bambu Lab Cloud API."""
  83. def __init__(self, region: str = "global", client: httpx.AsyncClient | None = None):
  84. self.base_url = BAMBU_API_BASE if region == "global" else BAMBU_API_BASE_CN
  85. self.access_token: str | None = None
  86. self.refresh_token: str | None = None
  87. self.token_expiry: datetime | None = None
  88. # Prefer an explicitly-injected client (tests), else fall back to the
  89. # app-scoped shared client (production), and finally create our own so
  90. # scripts / tests that skip the lifespan still get a working service.
  91. if client is not None:
  92. self._client = client
  93. self._owns_client = False
  94. elif _shared_http_client is not None:
  95. self._client = _shared_http_client
  96. self._owns_client = False
  97. else:
  98. self._client = httpx.AsyncClient(timeout=30.0)
  99. self._owns_client = True
  100. @property
  101. def is_authenticated(self) -> bool:
  102. """Check if we have a valid token."""
  103. if not self.access_token:
  104. return False
  105. return not (self.token_expiry and datetime.now(timezone.utc) > self.token_expiry)
  106. def _get_headers(self) -> dict:
  107. """Get headers for authenticated requests."""
  108. headers = {
  109. "Content-Type": "application/json",
  110. "User-Agent": _USER_AGENT,
  111. }
  112. if self.access_token:
  113. headers["Authorization"] = f"Bearer {self.access_token}"
  114. return headers
  115. async def login_request(self, email: str, password: str) -> dict:
  116. """
  117. Initiate login - this will trigger either email verification or TOTP prompt.
  118. Returns dict with login status, verification type, and tfaKey if needed.
  119. """
  120. try:
  121. response = await self._client.post(
  122. f"{self.base_url}/v1/user-service/user/login",
  123. headers={"Content-Type": "application/json"},
  124. json={
  125. "account": email,
  126. "password": password,
  127. },
  128. )
  129. try:
  130. data = response.json()
  131. except Exception as json_err:
  132. logger.error("Failed to parse login response: %s, body: %s", json_err, response.text[:500])
  133. cf_message = _detect_cloudflare_challenge(response)
  134. return {
  135. "success": False,
  136. "needs_verification": False,
  137. "message": cf_message or "Invalid response from Bambu Cloud",
  138. }
  139. logger.debug(
  140. f"Login response: status={response.status_code}, loginType={data.get('loginType')}, hasTfaKey={'tfaKey' in data}"
  141. )
  142. if response.status_code == 200:
  143. login_type = data.get("loginType")
  144. tfa_key = data.get("tfaKey")
  145. # TOTP authentication required
  146. if login_type == "tfa" or (tfa_key and login_type != "verifyCode"):
  147. return {
  148. "success": False,
  149. "needs_verification": True,
  150. "verification_type": "totp",
  151. "tfa_key": tfa_key,
  152. "message": "Enter the code from your authenticator app",
  153. }
  154. # Email verification required
  155. if login_type == "verifyCode":
  156. return {
  157. "success": False,
  158. "needs_verification": True,
  159. "verification_type": "email",
  160. "tfa_key": None,
  161. "message": "Verification code sent to email",
  162. }
  163. # Direct login success (rare, usually needs 2FA)
  164. if "accessToken" in data:
  165. self._set_tokens(data)
  166. return {"success": True, "needs_verification": False, "message": "Login successful"}
  167. # Handle specific error codes
  168. error_msg = data.get("message") or data.get("error") or "Login failed"
  169. return {"success": False, "needs_verification": False, "message": error_msg}
  170. except Exception as e:
  171. logger.error("Login request failed: %s", e)
  172. raise BambuCloudAuthError(f"Login request failed: {e}")
  173. async def verify_code(self, email: str, code: str) -> dict:
  174. """
  175. Complete login with email verification code.
  176. """
  177. try:
  178. response = await self._client.post(
  179. f"{self.base_url}/v1/user-service/user/login",
  180. headers={"Content-Type": "application/json"},
  181. json={
  182. "account": email,
  183. "code": code,
  184. },
  185. )
  186. try:
  187. data = response.json()
  188. except Exception as json_err:
  189. logger.error("Failed to parse email-verify response: %s, body: %s", json_err, response.text[:500])
  190. cf_message = _detect_cloudflare_challenge(response)
  191. return {"success": False, "message": cf_message or "Invalid response from Bambu Cloud"}
  192. logger.debug("Email verify response: status=%s, hasToken=%s", response.status_code, "accessToken" in data)
  193. if response.status_code == 200 and "accessToken" in data:
  194. self._set_tokens(data)
  195. return {"success": True, "message": "Login successful"}
  196. return {"success": False, "message": data.get("message", "Verification failed")}
  197. except Exception as e:
  198. logger.error("Email verification failed: %s", e)
  199. raise BambuCloudAuthError(f"Verification failed: {e}")
  200. async def verify_totp(self, tfa_key: str, code: str) -> dict:
  201. """
  202. Complete login with TOTP code from authenticator app.
  203. Args:
  204. tfa_key: The tfaKey returned from initial login request
  205. code: 6-digit TOTP code from authenticator app
  206. """
  207. try:
  208. # TFA endpoint is on bambulab.com, NOT api.bambulab.com.
  209. # We previously sent a Chrome User-Agent plus Origin/Referer headers
  210. # under the assumption Cloudflare would block bot-identified
  211. # requests. Verified 2026-05-12 via curl that the endpoint accepts
  212. # honest "Bambuddy/X.Y.Z" identification cleanly (HTTP 400 with the
  213. # expected application-level "Login failed" JSON, no Cloudflare
  214. # interstitial). Browser-impersonation removed to stay clearly on
  215. # the right side of Bambu Lab's "no falsified client identity" line.
  216. tfa_url = "https://bambulab.com/api/sign-in/tfa"
  217. if "bambulab.cn" in self.base_url:
  218. tfa_url = "https://bambulab.cn/api/sign-in/tfa"
  219. response = await self._client.post(
  220. tfa_url,
  221. headers={
  222. "Content-Type": "application/json",
  223. "User-Agent": _USER_AGENT,
  224. "Accept": "application/json",
  225. },
  226. json={
  227. "tfaKey": tfa_key,
  228. "tfaCode": code,
  229. },
  230. )
  231. logger.debug(
  232. f"TOTP verify response: status={response.status_code}, body={response.text[:200] if response.text else '(empty)'}"
  233. )
  234. # Handle empty response
  235. if not response.text or not response.text.strip():
  236. logger.warning("TOTP verification returned empty response (status %s)", response.status_code)
  237. return {"success": False, "message": "Bambu Cloud returned empty response. Please try again."}
  238. try:
  239. data = response.json()
  240. except Exception as json_err:
  241. logger.error("Failed to parse TOTP response: %s, body: %s", json_err, response.text[:500])
  242. cf_message = _detect_cloudflare_challenge(response)
  243. return {"success": False, "message": cf_message or "Invalid response from Bambu Cloud"}
  244. # Token might be in accessToken, token field, or cookies
  245. access_token = data.get("accessToken") or data.get("token")
  246. # Also check cookies for token
  247. if not access_token:
  248. for cookie in response.cookies:
  249. if "token" in cookie.lower():
  250. access_token = response.cookies.get(cookie)
  251. break
  252. if response.status_code == 200 and access_token:
  253. self.access_token = access_token
  254. self.refresh_token = data.get("refreshToken")
  255. from datetime import datetime, timedelta, timezone
  256. self.token_expiry = datetime.now(timezone.utc) + timedelta(days=30)
  257. return {"success": True, "message": "Login successful"}
  258. # Provide helpful error message
  259. error_msg = data.get("message", "")
  260. if "expired" in error_msg.lower():
  261. return {"success": False, "message": "TOTP session expired. Please try logging in again."}
  262. if not error_msg:
  263. error_msg = f"TOTP verification failed (status {response.status_code})"
  264. return {"success": False, "message": error_msg}
  265. except Exception as e:
  266. logger.error("TOTP verification failed: %s", e)
  267. # Return error instead of raising - don't trigger 401/500
  268. return {"success": False, "message": f"TOTP verification error: {e}"}
  269. def _set_tokens(self, data: dict):
  270. """Set tokens from login response."""
  271. self.access_token = data.get("accessToken")
  272. self.refresh_token = data.get("refreshToken")
  273. # Token typically valid for ~3 months, but we'll refresh more often
  274. self.token_expiry = datetime.now(timezone.utc) + timedelta(days=30)
  275. def set_token(self, access_token: str):
  276. """Set access token directly (for stored tokens)."""
  277. self.access_token = access_token
  278. self.token_expiry = datetime.now(timezone.utc) + timedelta(days=30)
  279. def logout(self):
  280. """Clear authentication state."""
  281. self.access_token = None
  282. self.refresh_token = None
  283. self.token_expiry = None
  284. async def get_user_profile(self) -> dict:
  285. """Get user profile information."""
  286. if not self.is_authenticated:
  287. raise BambuCloudAuthError("Not authenticated")
  288. try:
  289. response = await self._client.get(
  290. f"{self.base_url}/v1/design-user-service/my/preference", headers=self._get_headers()
  291. )
  292. if response.status_code == 200:
  293. return response.json()
  294. raise BambuCloudError(f"Failed to get profile: {response.status_code}")
  295. except httpx.RequestError as e:
  296. raise BambuCloudError(f"Request failed: {e}")
  297. async def get_slicer_settings(self, version: str = _SLICER_API_VERSION) -> dict:
  298. """
  299. Get all slicer settings (filament, printer, process presets).
  300. Args:
  301. version: Slicer version string. Bambu's API requires the XX.YY.ZZ.WW
  302. format but does not validate against a release manifest — we
  303. default to the neutral _SLICER_API_VERSION placeholder so we
  304. never claim to be a specific Bambu Studio build. Callers should
  305. normally use the default.
  306. """
  307. if not self.is_authenticated:
  308. raise BambuCloudAuthError("Not authenticated")
  309. try:
  310. response = await self._client.get(
  311. f"{self.base_url}/v1/iot-service/api/slicer/setting",
  312. headers=self._get_headers(),
  313. params={"version": version},
  314. )
  315. data = response.json()
  316. if response.status_code == 200:
  317. return data
  318. raise BambuCloudError(f"Failed to get settings: {response.status_code}")
  319. except httpx.RequestError as e:
  320. raise BambuCloudError(f"Request failed: {e}")
  321. async def get_setting_detail(self, setting_id: str) -> dict:
  322. """Get detailed information for a specific setting/preset."""
  323. if not self.is_authenticated:
  324. raise BambuCloudAuthError("Not authenticated")
  325. try:
  326. response = await self._client.get(
  327. f"{self.base_url}/v1/iot-service/api/slicer/setting/{setting_id}", headers=self._get_headers()
  328. )
  329. if response.status_code == 200:
  330. return response.json()
  331. raise BambuCloudError(f"Failed to get setting detail: {response.status_code}")
  332. except httpx.RequestError as e:
  333. raise BambuCloudError(f"Request failed: {e}")
  334. async def create_setting(
  335. self, preset_type: str, name: str, base_id: str, setting: dict, version: str = "2.0.0.0"
  336. ) -> dict:
  337. """
  338. Create a new slicer preset/setting.
  339. Args:
  340. preset_type: Type of preset - "filament", "print", or "printer"
  341. name: Display name for the preset
  342. base_id: Base preset ID to inherit from (e.g., "GFSA00")
  343. setting: Dict of setting key-value pairs (only modified values from base)
  344. version: Version string for the preset (default: "2.0.0.0")
  345. Returns:
  346. Created preset data including the new setting_id
  347. """
  348. if not self.is_authenticated:
  349. raise BambuCloudAuthError("Not authenticated")
  350. try:
  351. # Add timestamp if not present
  352. import time
  353. if "updated_time" not in setting:
  354. setting["updated_time"] = str(int(time.time()))
  355. payload = {
  356. "type": preset_type,
  357. "name": name,
  358. "version": version,
  359. "base_id": base_id,
  360. "setting": setting,
  361. }
  362. response = await self._client.post(
  363. f"{self.base_url}/v1/iot-service/api/slicer/setting", headers=self._get_headers(), json=payload
  364. )
  365. data = response.json()
  366. if response.status_code in (200, 201):
  367. return data
  368. error_msg = data.get("message") or data.get("error") or f"HTTP {response.status_code}"
  369. raise BambuCloudError(f"Failed to create setting: {error_msg}")
  370. except httpx.RequestError as e:
  371. raise BambuCloudError(f"Request failed: {e}")
  372. async def update_setting(self, setting_id: str, name: str | None = None, setting: dict | None = None) -> dict:
  373. """
  374. Update an existing slicer preset/setting.
  375. Note: Bambu Cloud API doesn't support true updates. Instead, we:
  376. 1. Fetch the current setting metadata (type, base_id, version)
  377. 2. Use the provided settings as the new complete settings (NOT merged)
  378. 3. Delete the old setting first (to avoid name conflicts)
  379. 4. Create a new setting via POST
  380. Args:
  381. setting_id: ID of the preset to update
  382. name: New display name (optional)
  383. setting: Dict of setting key-value pairs - this REPLACES the old settings entirely
  384. Returns:
  385. Updated preset data with new setting_id
  386. """
  387. if not self.is_authenticated:
  388. raise BambuCloudAuthError("Not authenticated")
  389. try:
  390. # Fetch current setting to get metadata (type, base_id, version)
  391. current = await self.get_setting_detail(setting_id)
  392. preset_type = current.get("type", "filament")
  393. # Use provided settings directly (complete replacement, not merge)
  394. # This allows the frontend to edit the full settings JSON
  395. if setting is not None:
  396. updated_setting = setting.copy()
  397. else:
  398. updated_setting = current.get("setting", {}).copy()
  399. # Extract name from settings_id field in the JSON, or use provided name, or fall back to current
  400. # The settings_id field contains the name in quotes, e.g., '"My Preset Name"'
  401. settings_id_key = {
  402. "filament": "filament_settings_id",
  403. "print": "print_settings_id",
  404. "printer": "printer_settings_id",
  405. }.get(preset_type, "filament_settings_id")
  406. settings_id_value = updated_setting.get(settings_id_key, "")
  407. if settings_id_value:
  408. # Remove surrounding quotes if present (e.g., '"foo"' -> 'foo')
  409. updated_name = settings_id_value.strip('"')
  410. elif name is not None:
  411. updated_name = name
  412. else:
  413. updated_name = current.get("name", "Untitled")
  414. # Update the timestamp
  415. import time
  416. updated_setting["updated_time"] = str(int(time.time()))
  417. # Ensure settings_id field matches the name
  418. updated_setting[settings_id_key] = f'"{updated_name}"'
  419. # Delete the old setting FIRST to avoid name conflicts
  420. await self.delete_setting(setting_id)
  421. # Create new setting via POST
  422. payload = {
  423. "type": preset_type,
  424. "name": updated_name,
  425. "version": current.get("version", "2.0.0.0"),
  426. "base_id": current.get("base_id", ""),
  427. "setting": updated_setting,
  428. }
  429. response = await self._client.post(
  430. f"{self.base_url}/v1/iot-service/api/slicer/setting", headers=self._get_headers(), json=payload
  431. )
  432. data = response.json()
  433. if response.status_code == 200:
  434. return data
  435. error_msg = data.get("message") or data.get("error") or f"HTTP {response.status_code}"
  436. raise BambuCloudError(f"Failed to update setting: {error_msg}")
  437. except httpx.RequestError as e:
  438. raise BambuCloudError(f"Request failed: {e}")
  439. async def delete_setting(self, setting_id: str) -> dict:
  440. """
  441. Delete a slicer preset/setting.
  442. Args:
  443. setting_id: ID of the preset to delete
  444. Returns:
  445. Deletion confirmation
  446. """
  447. if not self.is_authenticated:
  448. raise BambuCloudAuthError("Not authenticated")
  449. try:
  450. response = await self._client.delete(
  451. f"{self.base_url}/v1/iot-service/api/slicer/setting/{setting_id}", headers=self._get_headers()
  452. )
  453. if response.status_code in (200, 204):
  454. return {"success": True, "message": "Setting deleted"}
  455. data = response.json() if response.content else {}
  456. error_msg = data.get("message") or data.get("error") or f"HTTP {response.status_code}"
  457. raise BambuCloudError(f"Failed to delete setting: {error_msg}")
  458. except httpx.RequestError as e:
  459. raise BambuCloudError(f"Request failed: {e}")
  460. async def get_devices(self) -> dict:
  461. """Get list of bound devices."""
  462. if not self.is_authenticated:
  463. raise BambuCloudAuthError("Not authenticated")
  464. try:
  465. response = await self._client.get(
  466. f"{self.base_url}/v1/iot-service/api/user/bind", headers=self._get_headers()
  467. )
  468. if response.status_code == 200:
  469. return response.json()
  470. raise BambuCloudError(f"Failed to get devices: {response.status_code}")
  471. except httpx.RequestError as e:
  472. raise BambuCloudError(f"Request failed: {e}")
  473. async def get_firmware_version(self, device_id: str) -> dict:
  474. """
  475. Get firmware version info for a device.
  476. Returns dict with:
  477. - current_version: Installed firmware version
  478. - latest_version: Latest available firmware version
  479. - update_available: Boolean indicating if update is available
  480. - release_notes: Release notes for latest version
  481. """
  482. if not self.is_authenticated:
  483. raise BambuCloudAuthError("Not authenticated")
  484. try:
  485. response = await self._client.get(
  486. f"{self.base_url}/v1/iot-service/api/user/device/version",
  487. headers=self._get_headers(),
  488. params={"device_id": device_id},
  489. )
  490. if response.status_code == 200:
  491. data = response.json()
  492. # API wraps response in 'data' field
  493. return data.get("data", data)
  494. raise BambuCloudError(f"Failed to get firmware version: {response.status_code}")
  495. except httpx.RequestError as e:
  496. raise BambuCloudError(f"Request failed: {e}")
  497. async def close(self):
  498. """Close the HTTP client we own. No-op when sharing an app-scoped client."""
  499. if self._owns_client:
  500. await self._client.aclose()
  501. # Previously this module exposed a process-wide ``_cloud_service`` singleton
  502. # via ``get_cloud_service()`` / ``reset_cloud_service()``. That pattern leaked
  503. # region and token state across users (a China-region login would pin the
  504. # singleton to api.bambulab.cn until the next explicit reset), so the singleton
  505. # has been removed. Callers should construct a per-request
  506. # ``BambuCloudService(region=...)`` from the stored region and ``await
  507. # cloud.close()`` it when done. See ``routes.cloud.build_authenticated_cloud``
  508. # for the standard pattern.