""" REST handler for `aicommander` command """ import traceback import cexc from ai_commander.ai_commander_util import AICommanderUtil from ai_commander.constants import ( PAYLOAD, STATUS, AI_COMMANDER_EDIT_CONFIG_CAPABILITIES, AI_COMMANDER_READ_CONFIG_CAPABILITIES, ) from util.searchinfo_util import searchinfo_from_request logger = cexc.get_logger(__name__) class Aicommander(object): @classmethod def handle_post(cls, request: dict, path_parts: list) -> dict: """ Handles the POST request to update AICommander service settings. Args: request (dict): The HTTP request containing the payload with updated configuration. path_parts (list): The list of path parts from the request URL. Returns: dict: A response dictionary containing: - 'payload': A message indicating success or failure. - 'status': HTTP status code (200 for success, 403 for unauthorized access, 500 for errors). """ searchinfo = searchinfo_from_request(request) is_user_eligible = AICommanderUtil(searchinfo=searchinfo).check_user_role_eligibility( required_capabilities=AI_COMMANDER_EDIT_CONFIG_CAPABILITIES ) if not is_user_eligible: return { PAYLOAD: { 'error_message': 'User is not eligible to update the AICommander Connection Management Settings' }, STATUS: 403, } try: updated_config = AICommanderUtil(searchinfo).update_conf_file(request[PAYLOAD]) return { PAYLOAD: { 'message': 'Service settings updated successfully', 'updated_config': updated_config, # Return updated config 'status': 'success', }, STATUS: 200, } except Exception as e: logger.error(f"Error updating configuration: {str(traceback.format_exc())}") return { PAYLOAD: {'error_message': "Failed to update service settings."}, STATUS: 500, } @classmethod def handle_get(cls, request: dict, path_parts: list) -> dict: """ Handles the GET request to retrieve AICommander service settings. Args: request (dict): The HTTP request object. path_parts (list): The list of path parts from the request URL. Returns: dict: A response dictionary containing: - 'payload': The AICommander configuration or an error message. - 'status': HTTP status code (200 for success, 403 for unauthorized access). """ try: searchinfo = searchinfo_from_request(request) is_user_eligible = AICommanderUtil( searchinfo=searchinfo ).check_user_role_eligibility( required_capabilities=AI_COMMANDER_EDIT_CONFIG_CAPABILITIES ) if is_user_eligible: return { PAYLOAD: AICommanderUtil(searchinfo).get_conf(is_token_required=False), STATUS: 200, } elif AICommanderUtil(searchinfo=searchinfo).check_user_role_eligibility( required_capabilities=AI_COMMANDER_READ_CONFIG_CAPABILITIES ): return { PAYLOAD: AICommanderUtil(searchinfo).get_conf(is_token_required=False), STATUS: 200, } else: return { PAYLOAD: { 'error_message': 'User is not eligible to view the AICommander Connection Management Settings' }, STATUS: 403, } except Exception as e: logger.error(f"Error retrieving configuration: {str(traceback.format_exc())}") return { PAYLOAD: {'error_message': "Failed to retrieve service settings"}, STATUS: 500, }