async def session_command_execute_handler( session_id: route_models.IdentifierType, command_request: route_models.CommandRequest, session_manager: SessionManager = Depends(get_session_manager), ) -> route_models.CommandResponse: """ Execute a session command """ session_obj = get_session(manager=session_manager, session_id=session_id, api_router=router) if not session_manager.is_active(session_obj.meta.identifier): raise RobotServerError( status_code=http_status_codes.HTTP_403_FORBIDDEN, error=Error( title=f"Session '{session_id}' is not active", detail="Only the active session can execute commands", )) try: command = create_command(command_request.data.attributes.command, command_request.data.attributes.data) command_result = await session_obj.command_executor.execute(command) log.debug(f"Command completed {command}") # TODO: Amit 07/30/2020 - SessionCommandException should be a # RobotServerError. Customized by raiser with status code and rest of # Error attributes. The job here would be to log and re-raise. except CommandExecutionConflict as e: log.exception("Failed to execute command due to conflict") raise RobotServerError(status_code=http_status_codes.HTTP_409_CONFLICT, error=Error( title="Command execution conflict", detail=str(e), )) except SessionCommandException as e: log.exception("Failed to execute command") raise RobotServerError( status_code=http_status_codes.HTTP_400_BAD_REQUEST, error=Error( title="Command execution error", detail=str(e), )) return route_models.CommandResponse(data=ResponseDataModel.create( attributes=route_models.SessionCommand( data=command_result.content.data, command=command_result.content.name, status=command_result.result.status, createdAt=command_result.meta.created_at, startedAt=command_result.result.started_at, completedAt=command_result.result.completed_at), resource_id=command_result.meta.identifier), links=get_valid_session_links( session_id, router))
async def delete_session_handler( session_id: route_models.IdentifierType, session_manager: SessionManager = Depends(get_session_manager)) \ -> route_models.SessionResponse: """Delete a session""" session_obj = get_session(manager=session_manager, session_id=session_id, api_router=router) try: await session_manager.remove(session_obj.meta.identifier) # TODO: Amit 07/30/2020 - SessionException should be a RobotServerError. # Customized by raiser with status code and rest of # Error attributes. The job here would be to log and re-raise. except SessionException as e: log.exception("Failed to remove a session session") raise RobotServerError( status_code=http_status_codes.HTTP_400_BAD_REQUEST, error=Error( title="Removal Failed", detail=f"Failed to remove session " f"'{session_id}': {str(e)}.", )) return route_models.SessionResponse( data=ResponseDataModel.create( attributes=session_obj.get_response_model(), resource_id=session_id), links={ "POST": ResourceLink( href=router.url_path_for(create_session_handler.__name__)), })
async def create_session_handler( create_request: route_models.SessionCreateRequest, session_manager: SessionManager = Depends(get_session_manager)) \ -> route_models.SessionResponse: """Create a session""" session_type = create_request.data.attributes.sessionType create_params = create_request.data.attributes.createParams try: new_session = await session_manager.add( session_type=session_type, session_meta_data=SessionMetaData(create_params=create_params)) # TODO: Amit 07/30/2020 - SessionCreationException should be a # RobotServerError. Customized by raiser with status code and rest of # Error attributes. The job here would be to log and re-raise. except SessionCreationException as e: log.exception("Failed to create session") raise RobotServerError( status_code=http_status_codes.HTTP_400_BAD_REQUEST, error=Error( title="Creation Failed", detail=f"Failed to create session of type " f"'{session_type}': {str(e)}.", )) return route_models.SessionResponse(data=ResponseDataModel.create( attributes=new_session.get_response_model(), resource_id=new_session.meta.identifier), links=get_valid_session_links( new_session.meta.identifier, router))
async def delete_specific_labware_calibration( calibrationId: cal_types.CalibrationID): try: delete.delete_offset_file(calibrationId) except (FileNotFoundError, KeyError): error = Error(title='{calibrationId} does not exist.') raise RobotServerError(status_code=status.HTTP_404_NOT_FOUND, error=error)
async def session_command_execute_handler( session_id: route_models.IdentifierType, command_request: route_models.CommandRequest, session_manager: SessionManager = Depends(get_session_manager), ) -> route_models.CommandResponse: """ Execute a session command """ session_obj = get_session(manager=session_manager, session_id=session_id, api_router=router) if not session_manager.is_active(session_obj.meta.identifier): raise RobotServerError( status_code=http_status_codes.HTTP_403_FORBIDDEN, error=Error( title=f"Session '{session_id}' is not active", detail="Only the active session can execute commands", )) try: command = create_command(command_request.data.attributes.command, command_request.data.attributes.data) command_result = await session_obj.command_executor.execute(command) log.debug(f"Command completed {command}") except SessionCommandException as e: log.exception("Failed to execute command") raise RobotServerError( status_code=http_status_codes.HTTP_400_BAD_REQUEST, error=Error( title="Command execution error", detail=str(e), )) return route_models.CommandResponse(data=ResponseDataModel.create( attributes=route_models.SessionCommand( data=command_result.content.data, command=command_result.content.name, status=command_result.result.status), resource_id=command_result.meta.identifier), links=get_valid_session_links( session_id, router))
async def get_specific_labware_calibration( calibrationId: str) -> lw_models.SingleCalibrationResponse: calibration: Optional[cal_types.CalibrationInformation] = None for cal in get_cal.get_all_calibrations(): if calibrationId == cal.labware_id: calibration = cal break if not calibration: error = Error(title='{calibrationId} does not exist.') raise RobotServerError(status_code=status.HTTP_404_NOT_FOUND, error=error) formatted_calibrations = _format_calibrations([calibration]) return lw_models.SingleCalibrationResponse( data=formatted_calibrations[0])
def get_session(manager: SessionManager, session_id: route_models.IdentifierType, api_router: APIRouter) -> BaseSession: """Get the session or raise a RobotServerError""" found_session = manager.get_by_id(session_id) if not found_session: # There is no session raise error raise RobotServerError( status_code=http_status_codes.HTTP_404_NOT_FOUND, error=Error(title="No session", detail=f"Cannot find session with id '{session_id}'.", links={ "POST": api_router.url_path_for( create_session_handler.__name__) })) return found_session
async def create_session_handler( create_request: route_models.SessionCreateRequest, session_manager: SessionManager = Depends(get_session_manager)) \ -> route_models.SessionResponse: """Create a session""" session_type = create_request.data.attributes.sessionType try: new_session = await session_manager.add(session_type) except SessionCreationException as e: log.exception("Failed to create session") raise RobotServerError( status_code=http_status_codes.HTTP_400_BAD_REQUEST, error=Error( title="Creation Failed", detail=f"Failed to create session of type " f"'{session_type}': {str(e)}.", )) return route_models.SessionResponse(data=ResponseDataModel.create( attributes=new_session.get_response_model(), resource_id=new_session.meta.identifier), links=get_valid_session_links( new_session.meta.identifier, router))
async def put_control(scope: access.ControlScope, token: access.TokenType) \ -> access.AccessTokenResponse: raise RobotServerError(status_code=status_codes.HTTP_501_NOT_IMPLEMENTED, error=Error(title="Not implemented"))
async def delete_access_token(access_token: access.TokenType) \ -> access.AccessTokenResponse: raise RobotServerError(status_code=status_codes.HTTP_501_NOT_IMPLEMENTED, error=Error(title="Not implemented"))
async def get_access_tokens() -> access.MultipleAccessTokenResponse: raise RobotServerError(status_code=status_codes.HTTP_501_NOT_IMPLEMENTED, error=Error(title="Not implemented"))