def get_command_result(cb: CbResponseAPI, session_id: str, command_id: str): """Get results of a LR session command.""" try: return cb.get_object(f"{CBLR_BASE}/session/{session_id}/command/{command_id}") except ObjectNotFoundError: LOGGER.warning(f"no live resonse session and/or command combination for {session_id}:{command_id}") return None
def get_session_commands(cb: CbResponseAPI, session_id: str): """List commands for this session.""" try: return cb.get_object(f"{CBLR_BASE}/session/{session_id}/command") except ObjectNotFoundError: LOGGER.warning(f"no live resonse session by ID={session_id}") return None
def get_session_by_id(cb: CbResponseAPI, session_id): """Get a LR session object by id.""" try: return cb.get_object(f"{CBLR_BASE}/session/{session_id}") except ObjectNotFoundError: LOGGER.warning(f"no live resonse session by ID={session_id}") return None
def get_file_content(cb: CbResponseAPI, session_id: str, file_id: str): """Get file content stored in LR session and write the file locally.""" from cbinterface.helpers import get_os_independent_filepath try: file_metadata = cb.get_object(f"{CBLR_BASE}/session/{session_id}/file/{file_id}") if file_metadata: filepath = get_os_independent_filepath(file_metadata["file_name"]) filename = f"{session_id}_{filepath.name}" result = cb.session.get(f"{CBLR_BASE}/session/{session_id}/file/{file_id}/content", stream=True) if result.status_code != 200: LOGGER.error( f"got {result.status_code} from server getting file {file_id} content for session {session_id}" ) return with open(filename, "wb") as fp: for chunk in result.iter_content(io.DEFAULT_BUFFER_SIZE): fp.write(chunk) if os.path.exists(filename): LOGGER.info(f"wrote: {filename}") return os.path.exists(filename) except ObjectNotFoundError: LOGGER.warning(f"no file {file_id} content with session {session_id}") return
def active_live_response_sessions(cb: CbResponseAPI) -> List: """Return active LR sessions.""" return [sesh for sesh in cb.get_object(f"{CBLR_BASE}/session?active_only=true")]
def all_live_response_sessions(cb: CbResponseAPI) -> List: """List all LR sessions still in server memory.""" return [sesh for sesh in cb.get_object(f"{CBLR_BASE}/session")]