feat: add on-demand device screenshots
Tests / Test apps.device-host-agent.tests.test_mcp_token.test_load_or_create_concurrent_calls_do_not_corrupt failed
Tests / Test apps.device-host-agent.tests.test_mcp_token.test_load_or_create_concurrent_calls_do_not_corrupt failed
This commit is contained in:
@@ -11,6 +11,7 @@ import jinja2
|
||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
||||
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse, Response
|
||||
|
||||
from core.errors import DeviceNotFoundError, DeviceOfflineError, DeviceRuntimeError
|
||||
from device.manager import DeviceManager
|
||||
from host_agent.assignment import AssignmentExecutor
|
||||
from host_agent.client import (
|
||||
@@ -488,6 +489,43 @@ def create_console_app(
|
||||
error=None,
|
||||
)
|
||||
|
||||
@app.post("/api/devices/{device_id}/screenshot")
|
||||
async def api_device_screenshot(
|
||||
device_id: str,
|
||||
session: SessionState = Depends(require_csrf),
|
||||
) -> Response:
|
||||
"""Capture one on-demand screenshot for a connected local device."""
|
||||
try:
|
||||
screenshot = await asyncio.to_thread(
|
||||
lambda: manager.active_driver(device_id).screenshot()
|
||||
)
|
||||
except DeviceNotFoundError as exc:
|
||||
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
||||
except DeviceOfflineError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
||||
except DeviceRuntimeError as exc:
|
||||
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||
except Exception as exc:
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=str(exc) or "failed to capture device screenshot",
|
||||
) from exc
|
||||
|
||||
if not isinstance(screenshot, bytes) or not screenshot:
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail="device returned an empty screenshot",
|
||||
)
|
||||
return Response(
|
||||
content=screenshot,
|
||||
media_type="image/png",
|
||||
headers={
|
||||
"Cache-Control": "no-store",
|
||||
"Pragma": "no-cache",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
},
|
||||
)
|
||||
|
||||
@app.post("/devices/save")
|
||||
async def devices_save(
|
||||
request: Request,
|
||||
|
||||
Reference in New Issue
Block a user