def __delete_fields(module): """ The method to delete fields of the given module from the current user's fields JSON file. Parameters: module(str): A string representing the module. Raises: SDKException """ try: record_field_details_path = os.path.join( ModuleFieldsHandler.__get_directory(), Converter.get_encoded_file_name()) if os.path.exists(record_field_details_path): record_field_details_json = Initializer.get_json( record_field_details_path) Utility.delete_fields(record_field_details_json, module) with open(record_field_details_path, mode="w") as file: json.dump(record_field_details_json, file) file.flush() file.close() except Exception as e: sdk_exception = SDKException(cause=e) raise sdk_exception
def refresh_fields(module): """ The method to force-refresh fields of a module. Parameters: module(str): A string representing the module. Raises: SDKException """ with ModuleFieldsHandler.lock: try: ModuleFieldsHandler.__delete_fields(module) Utility.get_fields_info(module) except SDKException as ex: ModuleFieldsHandler.logger.info( Constants.REFRESH_SINGLE_MODULE_FIELDS_ERROR + module + ex.__str__()) raise ex except Exception as e: sdk_exception = SDKException(cause=e) ModuleFieldsHandler.logger.info( Constants.REFRESH_SINGLE_MODULE_FIELDS_ERROR + module + sdk_exception.__str__()) raise sdk_exception
def update_record(self, id, module_api_name, request): """ The method to update record Parameters: id (int) : An int representing the id module_api_name (string) : A string representing the module_api_name request (BodyWrapper) : An instance of BodyWrapper Returns: APIResponse: An instance of APIResponse Raises: SDKException """ try: from zcrmsdk.src.com.zoho.crm.api.record.body_wrapper import BodyWrapper except Exception: from .body_wrapper import BodyWrapper if not isinstance(id, int): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: id EXPECTED TYPE: int', None, None) if not isinstance(module_api_name, str): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: module_api_name EXPECTED TYPE: str', None, None) if request is not None and not isinstance(request, BodyWrapper): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: request EXPECTED TYPE: BodyWrapper', None, None) handler_instance = CommonAPIHandler() api_path = '' api_path = api_path + '/crm/v2/' api_path = api_path + str(module_api_name) api_path = api_path + '/' api_path = api_path + str(id) handler_instance.set_api_path(api_path) handler_instance.set_http_method(Constants.REQUEST_METHOD_PUT) handler_instance.set_category_method(Constants.REQUEST_CATEGORY_UPDATE) handler_instance.set_content_type('application/json') handler_instance.set_request(request) Utility.get_fields(module_api_name) handler_instance.set_module_api_name(module_api_name) try: from zcrmsdk.src.com.zoho.crm.api.record.action_handler import ActionHandler except Exception: from .action_handler import ActionHandler return handler_instance.api_call(ActionHandler.__module__, 'application/json')
def get_records(self, module_api_name, param_instance=None, header_instance=None): """ The method to get records Parameters: module_api_name (string) : A string representing the module_api_name param_instance (ParameterMap) : An instance of ParameterMap header_instance (HeaderMap) : An instance of HeaderMap Returns: APIResponse: An instance of APIResponse Raises: SDKException """ if not isinstance(module_api_name, str): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: module_api_name EXPECTED TYPE: str', None, None) if param_instance is not None and not isinstance( param_instance, ParameterMap): raise SDKException( Constants.DATA_TYPE_ERROR, 'KEY: param_instance EXPECTED TYPE: ParameterMap', None, None) if header_instance is not None and not isinstance( header_instance, HeaderMap): raise SDKException( Constants.DATA_TYPE_ERROR, 'KEY: header_instance EXPECTED TYPE: HeaderMap', None, None) handler_instance = CommonAPIHandler() api_path = '' api_path = api_path + '/crm/v2/' api_path = api_path + str(module_api_name) handler_instance.set_api_path(api_path) handler_instance.set_http_method(Constants.REQUEST_METHOD_GET) handler_instance.set_category_method(Constants.REQUEST_CATEGORY_READ) handler_instance.set_param(param_instance) handler_instance.set_header(header_instance) Utility.get_fields(module_api_name) handler_instance.set_module_api_name(module_api_name) try: from zcrmsdk.src.com.zoho.crm.api.record.response_handler import ResponseHandler except Exception: from .response_handler import ResponseHandler return handler_instance.api_call(ResponseHandler.__module__, 'application/json')
def mass_update_records(self, module_api_name, request): """ The method to mass update records Parameters: module_api_name (string) : A string representing the module_api_name request (MassUpdateBodyWrapper) : An instance of MassUpdateBodyWrapper Returns: APIResponse: An instance of APIResponse Raises: SDKException """ try: from zcrmsdk.src.com.zoho.crm.api.record.mass_update_body_wrapper import MassUpdateBodyWrapper except Exception: from .mass_update_body_wrapper import MassUpdateBodyWrapper if not isinstance(module_api_name, str): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: module_api_name EXPECTED TYPE: str', None, None) if request is not None and not isinstance(request, MassUpdateBodyWrapper): raise SDKException( Constants.DATA_TYPE_ERROR, 'KEY: request EXPECTED TYPE: MassUpdateBodyWrapper', None, None) handler_instance = CommonAPIHandler() api_path = '' api_path = api_path + '/crm/v2/' api_path = api_path + str(module_api_name) api_path = api_path + '/actions/mass_update' handler_instance.set_api_path(api_path) handler_instance.set_http_method(Constants.REQUEST_METHOD_POST) handler_instance.set_category_method(Constants.REQUEST_CATEGORY_UPDATE) handler_instance.set_content_type('application/json') handler_instance.set_request(request) handler_instance.set_mandatory_checker(True) Utility.get_fields(module_api_name) handler_instance.set_module_api_name(module_api_name) try: from zcrmsdk.src.com.zoho.crm.api.record.mass_update_action_handler import MassUpdateActionHandler except Exception: from .mass_update_action_handler import MassUpdateActionHandler return handler_instance.api_call(MassUpdateActionHandler.__module__, 'application/json')
def upload_photo(self, id, module_api_name, request): """ The method to upload photo Parameters: id (int) : An int representing the id module_api_name (string) : A string representing the module_api_name request (FileBodyWrapper) : An instance of FileBodyWrapper Returns: APIResponse: An instance of APIResponse Raises: SDKException """ try: from zcrmsdk.src.com.zoho.crm.api.record.file_body_wrapper import FileBodyWrapper except Exception: from .file_body_wrapper import FileBodyWrapper if not isinstance(id, int): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: id EXPECTED TYPE: int', None, None) if not isinstance(module_api_name, str): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: module_api_name EXPECTED TYPE: str', None, None) if request is not None and not isinstance(request, FileBodyWrapper): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: request EXPECTED TYPE: FileBodyWrapper', None, None) handler_instance = CommonAPIHandler() api_path = '' api_path = api_path + '/crm/v2/' api_path = api_path + str(module_api_name) api_path = api_path + '/' api_path = api_path + str(id) api_path = api_path + '/photo' handler_instance.set_api_path(api_path) handler_instance.set_http_method(Constants.REQUEST_METHOD_POST) handler_instance.set_category_method(Constants.REQUEST_CATEGORY_CREATE) handler_instance.set_content_type('multipart/form-data') handler_instance.set_request(request) handler_instance.set_mandatory_checker(True) Utility.verify_photo_support(module_api_name) try: from zcrmsdk.src.com.zoho.crm.api.record.file_handler import FileHandler except Exception: from .file_handler import FileHandler return handler_instance.api_call(FileHandler.__module__, 'application/json')
def get_related_record(self, related_record_id, header_instance=None): """ The method to get related record Parameters: related_record_id (int) : An int representing the related_record_id header_instance (HeaderMap) : An instance of HeaderMap Returns: APIResponse: An instance of APIResponse Raises: SDKException """ if not isinstance(related_record_id, int): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: related_record_id EXPECTED TYPE: int', None, None) if header_instance is not None and not isinstance( header_instance, HeaderMap): raise SDKException( Constants.DATA_TYPE_ERROR, 'KEY: header_instance EXPECTED TYPE: HeaderMap', None, None) handler_instance = CommonAPIHandler() api_path = '' api_path = api_path + '/crm/v2/' api_path = api_path + str(self.__module_api_name) api_path = api_path + '/' api_path = api_path + str(self.__record_id) api_path = api_path + '/' api_path = api_path + str(self.__related_list_api_name) api_path = api_path + '/' api_path = api_path + str(related_record_id) handler_instance.set_api_path(api_path) handler_instance.set_http_method(Constants.REQUEST_METHOD_GET) handler_instance.set_category_method(Constants.REQUEST_CATEGORY_READ) handler_instance.set_header(header_instance) Utility.get_related_lists(self.__related_list_api_name, self.__module_api_name, handler_instance) try: from zcrmsdk.src.com.zoho.crm.api.related_records.response_handler import ResponseHandler except Exception: from .response_handler import ResponseHandler return handler_instance.api_call(ResponseHandler.__module__, 'application/json')
def convert_lead(self, id, request): """ The method to convert lead Parameters: id (int) : An int representing the id request (ConvertBodyWrapper) : An instance of ConvertBodyWrapper Returns: APIResponse: An instance of APIResponse Raises: SDKException """ try: from zcrmsdk.src.com.zoho.crm.api.record.convert_body_wrapper import ConvertBodyWrapper except Exception: from .convert_body_wrapper import ConvertBodyWrapper if not isinstance(id, int): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: id EXPECTED TYPE: int', None, None) if request is not None and not isinstance(request, ConvertBodyWrapper): raise SDKException( Constants.DATA_TYPE_ERROR, 'KEY: request EXPECTED TYPE: ConvertBodyWrapper', None, None) handler_instance = CommonAPIHandler() api_path = '' api_path = api_path + '/crm/v2/Leads/' api_path = api_path + str(id) api_path = api_path + '/actions/convert' handler_instance.set_api_path(api_path) handler_instance.set_http_method(Constants.REQUEST_METHOD_POST) handler_instance.set_category_method(Constants.REQUEST_CATEGORY_CREATE) handler_instance.set_content_type('application/json') handler_instance.set_request(request) handler_instance.set_mandatory_checker(True) Utility.get_fields("Deals") try: from zcrmsdk.src.com.zoho.crm.api.record.convert_action_handler import ConvertActionHandler except Exception: from .convert_action_handler import ConvertActionHandler return handler_instance.api_call(ConvertActionHandler.__module__, 'application/json')
def update_related_records(self, request): """ The method to update related records Parameters: request (BodyWrapper) : An instance of BodyWrapper Returns: APIResponse: An instance of APIResponse Raises: SDKException """ try: from zcrmsdk.src.com.zoho.crm.api.related_records.body_wrapper import BodyWrapper except Exception: from .body_wrapper import BodyWrapper if request is not None and not isinstance(request, BodyWrapper): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: request EXPECTED TYPE: BodyWrapper', None, None) handler_instance = CommonAPIHandler() api_path = '' api_path = api_path + '/crm/v2/' api_path = api_path + str(self.__module_api_name) api_path = api_path + '/' api_path = api_path + str(self.__record_id) api_path = api_path + '/' api_path = api_path + str(self.__related_list_api_name) handler_instance.set_api_path(api_path) handler_instance.set_http_method(Constants.REQUEST_METHOD_PUT) handler_instance.set_category_method(Constants.REQUEST_CATEGORY_UPDATE) handler_instance.set_content_type('application/json') handler_instance.set_request(request) handler_instance.set_mandatory_checker(True) Utility.get_related_lists(self.__related_list_api_name, self.__module_api_name, handler_instance) try: from zcrmsdk.src.com.zoho.crm.api.related_records.action_handler import ActionHandler except Exception: from .action_handler import ActionHandler return handler_instance.api_call(ActionHandler.__module__, 'application/json')
def get_all_contact_roles_of_deal(self, deal_id, param_instance=None): """ The method to get all contact roles of deal Parameters: deal_id (int) : An int representing the deal_id param_instance (ParameterMap) : An instance of ParameterMap Returns: APIResponse: An instance of APIResponse Raises: SDKException """ if not isinstance(deal_id, int): raise SDKException(Constants.DATA_TYPE_ERROR, 'KEY: deal_id EXPECTED TYPE: int', None, None) if param_instance is not None and not isinstance( param_instance, ParameterMap): raise SDKException( Constants.DATA_TYPE_ERROR, 'KEY: param_instance EXPECTED TYPE: ParameterMap', None, None) handler_instance = CommonAPIHandler() api_path = '' api_path = api_path + '/crm/v2.1/Deals/' api_path = api_path + str(deal_id) api_path = api_path + '/Contact_Roles' handler_instance.set_api_path(api_path) handler_instance.set_http_method(Constants.REQUEST_METHOD_GET) handler_instance.set_category_method(Constants.REQUEST_CATEGORY_READ) handler_instance.set_param(param_instance) handler_instance.set_module_api_name("Contacts") Utility.get_fields("Contacts", handler_instance) try: from zcrmsdk.src.com.zoho.crm.api.contact_roles.record_response_handler import RecordResponseHandler except Exception: from .record_response_handler import RecordResponseHandler return handler_instance.api_call(RecordResponseHandler.__module__, 'application/json')
def refresh_all_modules(): """ The method to force-refresh fields of all the available modules. Raises: SDKException """ with ModuleFieldsHandler.lock: try: Utility.refresh_modules() except SDKException as ex: ModuleFieldsHandler.logger.info( Constants.REFRESH_ALL_MODULE_FIELDS_ERROR + ex.__str__()) raise ex except Exception as e: sdk_exception = SDKException(cause=e) ModuleFieldsHandler.logger.info( Constants.REFRESH_ALL_MODULE_FIELDS_ERROR + sdk_exception.__str__()) raise sdk_exception
def __get_module_detail_from_user_spec_json(self, module): try: import os from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: import os from ..initializer import Initializer record_field_details_path = os.path.join( Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY, Converter.get_encoded_file_name()) record_field_details = Initializer.get_json(record_field_details_path) module_detail = Utility.get_json_object(record_field_details, module) return module_detail
def is_record_response(self, response_json, class_detail, pack): try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer response_json = dict(response_json) record_instance = JSONConverter.__get_instance_from_name(pack) module_api_name = self.common_api_handler.get_module_api_name() module_detail = {} class_name = Constants.RECORD_NAMESPACE.rpartition('.')[-1] if module_api_name is not None: self.common_api_handler.set_module_api_name(None) full_detail = Utility.search_json_details(module_api_name) if full_detail is not None: module_detail = full_detail[Constants.MODULEDETAILS] class_name = str(full_detail[ Constants.MODULEPACKAGENAME]).rpartition('.')[-1] record_instance = JSONConverter.__get_instance_from_name( full_detail[Constants.MODULEPACKAGENAME]) else: module_detail = self.__get_module_detail_from_user_spec_json( module_api_name) for key, value in class_detail.items(): module_detail[key] = value record_detail = Initializer.json_details[Constants.RECORD_NAMESPACE] class_list = record_instance.__class__.mro() for each_class in class_list: if Constants.RECORD_TYPE in str(each_class): class_name = Constants.RECORD_CLASS_NAME break key_values = {} for key_name in response_json.keys(): member_name = self.build_name(key_name) key_detail = {} if len(module_detail) > 0 and (key_name in module_detail or member_name in module_detail): key_detail = module_detail[ key_name] if key_name in module_detail.keys( ) else module_detail[member_name] elif member_name in record_detail: key_detail = record_detail[member_name] key_data = response_json[key_name] key_value = None if len(key_detail) > 0: key_name = key_detail[Constants.NAME] key_value = self.get_data(key_data, key_detail) else: key_value = self.redirector_for_json_to_object(key_data) key_values[key_name] = key_value setattr( record_instance, self.construct_private_member(class_name, Constants.KEY_VALUES), key_values) return record_instance
def is_record_request(self, record_instance, class_detail, instance_number, member_detail): try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer lookup = False skip_mandatory = False class_member_name = None if member_detail is not None: lookup = member_detail[ Constants. LOOKUP] if Constants.LOOKUP in member_detail else False skip_mandatory = member_detail[ Constants. SKIP_MANDATORY] if Constants.SKIP_MANDATORY in member_detail else False class_member_name = member_detail[Constants.NAME] request_json, module_detail = {}, {} module_api_name = self.common_api_handler.get_module_api_name() class_name = record_instance.__class__.__name__ if module_api_name is not None: self.common_api_handler.set_module_api_name(None) full_detail = Utility.search_json_details(module_api_name) if full_detail is not None: module_detail = full_detail[Constants.MODULEDETAILS] else: module_detail = self.__get_module_detail_from_user_spec_json( module_api_name) else: module_detail = class_detail class_detail = Initializer.json_details[Constants.RECORD_NAMESPACE] class_list = record_instance.__class__.mro() for each_class in class_list: if Constants.RECORD_TYPE in str(each_class): class_name = Constants.RECORD_CLASS_NAME break key_values = getattr( record_instance, self.construct_private_member(class_name, Constants.KEY_VALUES)) key_modified = getattr( record_instance, self.construct_private_member(class_name, Constants.KEY_MODIFIED)) required_keys, primary_keys = {}, {} if not skip_mandatory: for key_name, key_detail in module_detail.items(): name = key_detail[Constants.NAME] if Constants.REQUIRED in key_detail and key_detail[ Constants.REQUIRED]: required_keys[name] = True if Constants.PRIMARY in key_detail and key_detail[ Constants.PRIMARY]: primary_keys[name] = True for key_name, key_detail in class_detail.items(): name = key_detail[Constants.NAME] if Constants.REQUIRED in key_detail and key_detail[ Constants.REQUIRED]: required_keys[name] = True if Constants.PRIMARY in key_detail and key_detail[ Constants.PRIMARY]: primary_keys[name] = True for key_name in key_modified.keys(): if key_modified.get(key_name) != 1: continue key_detail = {} key_value = key_values.get( key_name) if key_name in key_values else None json_value = None member_name = self.build_name(key_name) if len(module_detail) > 0 and (member_name in module_detail or key_name in module_detail): key_detail = module_detail[ key_name] if key_name in module_detail else module_detail[ member_name] elif member_name in class_detail: key_detail = class_detail[member_name] if len(key_detail) > 0: if Constants.READ_ONLY in key_detail or (Constants.NAME not in key_detail): continue if self.value_checker(class_name, key_name, key_detail, key_value, self.unique_dict, instance_number): json_value = self.set_data(key_detail, key_value) else: json_value = self.redirector_for_object_to_json(key_value) if key_value is not None: required_keys.pop(key_name, None) primary_keys.pop(key_name, None) request_json[key_name] = json_value if skip_mandatory or self.check_exception( class_member_name, record_instance, instance_number, lookup, required_keys, primary_keys, {}) is True: return request_json
def value_checker(self, class_name, member_name, key_details, value, unique_values_map, instance_number): """ This method is to validate if the input values satisfy the constraints for the respective fields. :param class_name: A str containing the class name. :param member_name: A str containing the member name. :param key_details: A JSON object containing the key JSON details. :param value: A object containing the key value. :param unique_values_map: A list containing the construct objects. :param instance_number: An int containing the POJO class instance list number. :return: A bool representing the key value is expected pattern, unique, length, and values. """ try: from zcrmsdk.src.com.zoho.crm.api.exception import SDKException from zcrmsdk.src.com.zoho.crm.api.util.constants import Constants from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..exception import SDKException from .constants import Constants from ..initializer import Initializer details_jo = {} name = key_details[Constants.NAME] data_type = key_details[Constants.TYPE] check = True given_type = None if value is not None: if Constants.INTERFACE in key_details and key_details[ Constants.INTERFACE]: interface_details = Initializer.get_initializer().json_details[ key_details[Constants.STRUCTURE_NAME]] classes = interface_details[Constants.CLASSES] check = False for each_class in classes: path_split = str( value.__class__.__module__).rpartition(".") class_name = self.module_to_class(path_split[-1]) pack = path_split[0] + "." + class_name if pack == each_class: check = True break else: given_type = value.__class__.__module__ if data_type in Constants.DATA_TYPE: if isinstance(value, list) and Constants.STRUCTURE_NAME in key_details: structure_name = key_details[Constants.STRUCTURE_NAME] index = 0 path_split = str(structure_name).rpartition('.') imported_module = importlib.import_module(path_split[0]) class_holder = getattr(imported_module, path_split[-1]) for each_instance in value: if not isinstance(each_instance, class_holder): check = False instance_number = index data_type = Constants.LIST_KEY + '[' + structure_name + ']' given_type = each_instance.__module__ break index = index + 1 else: check = Utility.check_data_type(value=value, type=data_type) elif value is not None and data_type.lower() != Constants.OBJECT_KEY: path_split = str(data_type).rpartition('.') imported_module = importlib.import_module(path_split[0]) class_holder = getattr(imported_module, path_split[-1]) if not isinstance(value, class_holder): check = False if not check: details_jo[Constants.FIELD] = name details_jo[Constants.CLASS] = class_name details_jo[Constants.ACCEPTED_TYPE] = data_type details_jo[Constants.GIVEN_TYPE] = given_type if instance_number is not None: details_jo[Constants.INDEX] = instance_number raise SDKException(code=Constants.TYPE_ERROR, details=details_jo) if Constants.VALUES in key_details and \ (Constants.PICKLIST not in key_details or (key_details[Constants.PICKLIST] and Initializer.get_initializer().sdk_config.get_pick_list_validation())): values_ja = key_details[Constants.VALUES] if isinstance(value, Choice): value = value.get_value() if value not in values_ja: details_jo[Constants.FIELD] = member_name details_jo[Constants.CLASS] = class_name details_jo[Constants.GIVEN_VALUE] = value details_jo[Constants.ACCEPTED_VALUES] = values_ja if instance_number is not None: details_jo[Constants.INDEX] = instance_number raise SDKException(code=Constants.UNACCEPTED_VALUES_ERROR, details=details_jo) if Constants.UNIQUE in key_details: if name not in unique_values_map: unique_values_map[name] = [] values_array = unique_values_map[name] if value in values_array: details_jo[Constants.FIELD] = member_name details_jo[Constants.CLASS] = class_name details_jo[ Constants.FIRST_INDEX] = values_array.index(value) + 1 details_jo[Constants.NEXT_INDEX] = instance_number raise SDKException(code=Constants.UNIQUE_KEY_ERROR, details=details_jo) else: unique_values_map[name].append(value) if Constants.MIN_LENGTH in key_details or Constants.MAX_LENGTH in key_details: count = len(str(value)) if isinstance(value, list): count = len(value) if Constants.MAX_LENGTH in key_details and count > key_details[ Constants.MAX_LENGTH]: details_jo[Constants.FIELD] = member_name details_jo[Constants.CLASS] = class_name details_jo[Constants.GIVEN_LENGTH] = count details_jo[Constants.MAXIMUM_LENGTH] = key_details[ Constants.MAX_LENGTH] raise SDKException(code=Constants.MAXIMUM_LENGTH_ERROR, details=details_jo) if Constants.MIN_LENGTH in key_details and count < key_details[ Constants.MIN_LENGTH]: details_jo[Constants.FIELD] = member_name details_jo[Constants.CLASS] = class_name details_jo[Constants.GIVEN_LENGTH] = count details_jo[Constants.MINIMUM_LENGTH] = key_details[ Constants.MIN_LENGTH] raise SDKException(code=Constants.MINIMUM_LENGTH_ERROR, details=details_jo) if Constants.REGEX in key_details: if re.search(value, key_details[Constants.REGEX]) is None: details_jo[Constants.FIELD] = member_name details_jo[Constants.CLASS] = class_name details_jo[Constants.INSTANCE_NUMBER] = instance_number raise SDKException(code=Constants.REGEX_MISMATCH_ERROR, details=details_jo) return True