smartem_backend.api_client#
Members
Handles conversions between EPU data model and API request/response models.  | 
|
SSE client for agents to receive real-time instructions from the backend.  | 
|
SmartEM API client that provides synchronous HTTP interface.  | 
- class smartem_backend.api_client.EntityConverter[source]#
 Handles conversions between EPU data model and API request/response models. Separating this conversion logic keeps the main client code cleaner.
- static acquisition_to_request(entity: AcquisitionData) AcquisitionCreateRequest[source]#
 Convert EPU session data to acquisition request model
- static grid_to_request(entity: GridData, lowmag: bool = False) GridCreateRequest[source]#
 Convert Grid data to grid request model
- static gridsquare_to_request(entity: GridSquareData, lowmag: bool = False) GridSquareCreateRequest[source]#
 Convert GridSquareData to grid square request model
- static foilhole_to_request(entity: FoilHoleData) FoilHoleCreateRequest[source]#
 Convert FoilHoleData to foil hole request model
- static micrograph_to_request(entity: MicrographData) MicrographCreateRequest[source]#
 Convert MicrographData to micrograph request model
- static atlas_to_request(entity: AtlasData) AtlasCreateRequest[source]#
 Convert AtlasData to atlas request model
- class smartem_backend.api_client.SmartEMAPIClient(base_url: str, timeout: float = 10.0, logger=None)[source]#
 SmartEM API client that provides synchronous HTTP interface.
This client handles all API communication with the SmartEM Core API, provides data conversion between EPU data models and API request/response models, and maintains a cache of entity IDs.
Initialize the SmartEM API client
- Parameters:
 base_url – Base URL for the API
timeout – Request timeout in seconds
logger – Optional custom logger instance
- create_acquisition(acquisition: AcquisitionData) AcquisitionResponse[source]#
 Create a new acquisition
- get_acquisition_grids(acquisition_uuid: str) list[GridResponse][source]#
 Get all grids for a specific acquisition
- create_acquisition_grid(grid: GridData) GridResponse[source]#
 Create a new grid for a specific acquisition
- get_atlas_tiles_by_atlas(atlas_uuid: str) list[AtlasTileResponse][source]#
 Get all tiles for a specific atlas
- create_atlas_tile_for_atlas(tile: AtlasTileData) AtlasTileResponse[source]#
 Create a new tile for a specific atlas
- link_atlas_tile_and_gridsquare(gridsquare_position: AtlasTileGridSquarePositionData) AtlasTileGridSquarePositionResponse[source]#
 Link a grid square to a tile
- link_atlas_tile_and_gridsquares(gridsquare_positions: list[AtlasTileGridSquarePositionData]) list[AtlasTileGridSquarePositionResponse][source]#
 Link multiple grid squares to a tile
- update_gridsquare(gridsquare: GridSquareData, lowmag: bool = False) GridSquareResponse[source]#
 Update a grid square
- get_grid_gridsquares(grid_uuid: str) list[GridSquareResponse][source]#
 Get all grid squares for a specific grid
- create_grid_gridsquare(gridsquare: GridSquareData, lowmag: bool = False) GridSquareResponse[source]#
 Create a new grid square for a specific grid
- get_gridsquare_foilholes(gridsquare_uuid: str) list[FoilHoleResponse][source]#
 Get all foil holes for a specific grid square
- create_gridsquare_foilholes(gridsquare_uuid: str, foilholes: list[FoilHoleData], allow_on_grid_bar: bool = False) list[FoilHoleResponse][source]#
 Create a new foil hole for a specific grid square
- get_foilhole_micrographs(foilhole_id: str) list[MicrographResponse][source]#
 Get all micrographs for a specific foil hole
- create_foilhole_micrograph(micrograph: MicrographData) MicrographResponse[source]#
 Create a new micrograph for a specific foil hole
- acknowledge_instruction(agent_id: str, session_id: str, instruction_id: str, acknowledgement: AgentInstructionAcknowledgement) AgentInstructionAcknowledgementResponse[source]#
 Acknowledge an instruction from the agent
- class smartem_backend.api_client.SSEAgentClient(base_url: str, agent_id: str, session_id: str, timeout: int = 30, max_retries: int = 10, initial_retry_delay: float = 1.0, max_retry_delay: float = 60.0)[source]#
 SSE client for agents to receive real-time instructions from the backend. This is separate from the main ApiClient as it handles long-lived connections.
Initialize SSE client for agent communication
- Parameters:
 base_url – Base URL of the API server
agent_id – Unique identifier for this agent/microscope
session_id – Current microscopy session ID
timeout – Connection timeout in seconds
max_retries – Maximum number of reconnection attempts
initial_retry_delay – Initial delay between retries in seconds
max_retry_delay – Maximum delay between retries in seconds
- stream_instructions(instruction_callback: Callable[[dict], None], connection_callback: Callable[[dict], None] | None = None, error_callback: Callable[[Exception], None] | None = None) None[source]#
 Start streaming instructions via SSE (blocking)
- Parameters:
 instruction_callback – Called when an instruction is received
connection_callback – Called when connection events occur (optional)
error_callback – Called when errors occur (optional)
- async stream_instructions_async(instruction_callback: Callable[[dict], None], connection_callback: Callable[[dict], None] | None = None, error_callback: Callable[[Exception], None] | None = None, auto_retry: bool = True) None[source]#
 Start streaming instructions via SSE (async with auto-retry and exponential backoff)
- Parameters:
 instruction_callback – Called when an instruction is received
connection_callback – Called when connection events occur (optional)
error_callback – Called when errors occur (optional)
auto_retry – Whether to automatically retry on connection failures
- acknowledge_instruction(instruction_id: str, status: str, result: str | None = None, error_message: str | None = None, processing_time_ms: int | None = None, retry_count: int = 3) AgentInstructionAcknowledgementResponse[source]#
 Acknowledge an instruction with retry logic
- Parameters:
 instruction_id – ID of the instruction to acknowledge
status – Status of acknowledgement (‘received’, ‘processed’, ‘failed’, ‘declined’)
result – Optional result message
error_message – Optional error message if status is ‘failed’
processing_time_ms – Time taken to process the instruction in milliseconds
retry_count – Number of retry attempts for acknowledgement