def file_exists_flow(module_api_name, record_field_details_path, last_modified_time): with Utility.lock: record_field_details_json = Initializer.get_json(record_field_details_path) initializer = Initializer.get_initializer() if initializer.sdk_config.get_auto_refresh_fields() and not Utility.new_file and not Utility.get_modified_modules and (Constants.FIELDS_LAST_MODIFIED_TIME not in record_field_details_json or Utility.force_refresh or (time.time() * 1000 - record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME]) > 3600000): Utility.get_modified_modules = True last_modified_time = record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] if not Utility.force_refresh and Constants.FIELDS_LAST_MODIFIED_TIME in record_field_details_json else None Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False elif not Initializer.get_initializer().sdk_config.get_auto_refresh_fields() and Utility.force_refresh and not Utility.get_modified_modules: Utility.get_modified_modules = True Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False record_field_details_json = Initializer.get_json(record_field_details_path) if module_api_name is None or (module_api_name.lower() in record_field_details_json and record_field_details_json[module_api_name.lower()] is not None): return else: Utility.fill_data_type() record_field_details_json[module_api_name.lower()] = {} Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module_api_name) record_field_details_json = Initializer.get_json(record_field_details_path) record_field_details_json[module_api_name.lower()] = field_details Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json)
def set_api_url(self, connector): try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer api_path = '' if Constants.HTTP in self.__api_path: if Constants.CONTENT_API_URL in self.__api_path: api_path = Initializer.get_initializer( ).environment.file_upload_url try: url_parse = urllib3.util.parse_url(self.__api_path) path = url_parse.path except Exception as ex: raise SDKException(code=Constants.INVALID_URL_ERROR, cause=ex) api_path = api_path + path else: if str(self.__api_path)[:1].__eq__('/'): self.__api_path = self.__api_path[1:] api_path = api_path + self.__api_path else: api_path = Initializer.get_initializer().environment.url api_path = api_path + self.__api_path connector.url = api_path
def generate_id(self): id = "" email = str(Initializer.get_initializer().user.get_email()) environment = str(Initializer.get_initializer().environment.name) id += Constants.PYTHON + email[0:email.find(Constants.AT )] + Constants.UNDERSCORE id += environment + Constants.UNDERSCORE + self.__refresh_token[ len(self.__refresh_token) - 4:] self.__id = id return self.__id
def refresh_access_token(self, user, store): try: url = Initializer.get_initializer().environment.accounts_url body = { Constants.REFRESH_TOKEN: self.__refresh_token, Constants.CLIENT_ID: self.__client_id, Constants.CLIENT_SECRET: self.__client_secret, Constants.GRANT_TYPE: Constants.REFRESH_TOKEN } response = requests.post(url, data=body, params=None, headers=None, allow_redirects=False).json() self.parse_response(response) if self.__id is None: self.generate_id() store.save_token(user, self) except SDKException as ex: raise ex except Exception as ex: raise SDKException(code=Constants.SAVE_TOKEN_ERROR, cause=ex) return self
def authenticate(self, url_connection): with OAuthToken.lock: initializer = Initializer.get_initializer() store = initializer.store user = initializer.user if self.__access_token is None: if self.__id is not None: oauth_token = initializer.store.get_token_by_id( self.__id, self) else: oauth_token = initializer.store.get_token(user, self) else: oauth_token = self if oauth_token is None: token = self.generate_access_token( user, store).get_access_token() if ( self.__refresh_token is None ) else self.refresh_access_token(user, store).get_access_token() elif oauth_token.get_expires_in() is not None and int( oauth_token.get_expires_in()) - int( time.time() * 1000) < 5000: OAuthToken.logger.info(Constants.REFRESH_TOKEN_MESSAGE) token = oauth_token.refresh_access_token( user, store).get_access_token() else: token = oauth_token.get_access_token() url_connection.add_header(Constants.AUTHORIZATION, Constants.OAUTH_HEADER_PREFIX + token)
def generate_access_token(self, user, store): try: url = Initializer.get_initializer().environment.accounts_url body = { Constants.GRANT_TYPE: Constants.GRANT_TYPE_AUTH_CODE, Constants.CLIENT_ID: self.client_id, Constants.CLIENT_SECRET: self.client_secret, Constants.REDIRECT_URL: self.redirect_url, Constants.CODE: self.grant_token } response = requests.post(url, data=body, params=None, headers=None, allow_redirects=True).json() store.save_token(user, self.parse_response(response=response)) except SDKException as ex: raise ex except Exception as ex: raise SDKException(code=Constants.SAVE_TOKEN_ERROR, cause=ex) return self
def get_module_names(): module_data = {} resources_path = os.path.join(Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY) if not os.path.exists(resources_path): os.makedirs(resources_path) record_field_details_path = Utility.get_file_name() is_null = False if os.path.exists(record_field_details_path): json = Initializer.get_json(record_field_details_path) if Constants.SDK_MODULE_METADATA not in json: is_null = True elif json[Constants.SDK_MODULE_METADATA] is None or len(json[Constants.SDK_MODULE_METADATA])==0: is_null = True if not os.path.exists(record_field_details_path) or is_null: module_data = Utility.get_modules(None) Utility.write_module_meta_data(record_field_details_path, module_data) return module_data record_field_details_json = Initializer.get_json(record_field_details_path) module_data = record_field_details_json[Constants.SDK_MODULE_METADATA] return module_data
def run(self): try: Initializer.switch_user(self.user, self.environment, self.token, self.sdk_config, self.proxy) print('Getting records for User: '******'Status Code: ' + str(response.get_status_code())) if response.get_status_code() in [204, 304]: print('No Content' if response.get_status_code() == 204 else 'Not Modified') return # Get object from response response_object = response.get_object() if response_object is not None: # Check if expected ResponseWrapper instance is received. if isinstance(response_object, ResponseWrapper): # Get the list of obtained Record instances record_list = response_object.get_data() for record in record_list: for key, value in record.get_key_values().items(): print(key + " : " + str(value)) # Check if the request returned an exception elif isinstance(response_object, APIException): # Get the Status print("Status: " + response_object.get_status().get_value()) # Get the Code print("Code: " + response_object.get_code().get_value()) print("Details") # Get the details dict details = response_object.get_details() for key, value in details.items(): print(key + ' : ' + str(value)) # Get the Message print("Message: " + response_object.get_message().get_value()) except Exception as e: print(e)
def __get_directory(): """ The method to obtain resources directory path. Returns: str: A String representing the directory's absolute path. """ return os.path.join(Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY)
def get_encoded_file_name(cls): """ The method to get the module field JSON details file name. :return: A str representing the module field JSON details file name. """ try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer file_name = Initializer.get_initializer().user.email file_name = file_name.split( "@", 1)[0] + Initializer.get_initializer().environment.url input_bytes = file_name.encode("UTF-8") encoded_string = base64.b64encode(input_bytes) encoded_string = str(encoded_string.decode("UTF-8")) return encoded_string + '.json'
def get_related_lists(related_module_name, module_api_name, common_api_handler): with Utility.lock: try: is_new_data = False key = (module_api_name + Constants.UNDERSCORE + Constants.RELATED_LISTS).lower() resources_path = os.path.join( Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY) if not os.path.exists(resources_path): os.makedirs(resources_path) record_field_details_path = Utility.get_file_name() if not os.path.exists(record_field_details_path) or ( os.path.exists(record_field_details_path) and key not in Initializer.get_json(record_field_details_path) ): is_new_data = True related_list_values = Utility.get_related_list_details( module_api_name) record_field_details_json = Initializer.get_json( record_field_details_path) if os.path.exists( record_field_details_path) else {} record_field_details_json[key] = related_list_values Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) record_field_details_json = Initializer.get_json( record_field_details_path) module_related_list = record_field_details_json[key] if not Utility.check_related_list_exists( related_module_name, module_related_list, common_api_handler) and not is_new_data: del record_field_details_json[key] Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) Utility.get_related_lists(related_module_name, module_api_name, common_api_handler) except SDKException as e: Utility.logger.error(Constants.EXCEPTION + e.__str__()) raise e except Exception as e: sdk_exception = SDKException(cause=e) Utility.logger.error(Constants.EXCEPTION + sdk_exception.__str__()) raise sdk_exception
def run(self): try: Initializer.switch_user(self.user, self.environment, self.token, self.sdk_config) print('Getting records for User: ' + Initializer.get_initializer().user.email) response = RecordOperations().get_records(self.module_api_name) print(response) except Exception as e: print(e)
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 fire_request(self, converter_instance): """ This method makes a request to the Zoho CRM Rest API Parameters: converter_instance (Converter) : A Converter class instance to call append_to_request method. Returns: requests.Response : An object of requests.Response """ response = None proxies = None logger = logging.getLogger('SDKLogger') if self.content_type is not None: self.set_content_type_header() if Initializer.get_initializer().request_proxy is not None: request_proxy = Initializer.get_initializer().request_proxy auth = "" if request_proxy.user is not None: auth = request_proxy.user + ':' + request_proxy.password + '@' if Constants.HTTP in request_proxy.host: host_split = request_proxy.host.split('://') scheme = host_split[0] proxies = { scheme: scheme + '://' + auth + host_split[1] + ':' + str(request_proxy.port) } else: proxies = { Constants.HTTP: Constants.HTTP + '://' + auth + request_proxy.host + ':' + str(request_proxy.port), Constants.HTTPS: Constants.HTTPS + '://' + auth + request_proxy.host + ':' + str(request_proxy.port) } logger.info(self.proxy_log(request_proxy)) logger.info(self.__str__()) if self.request_method == Constants.REQUEST_METHOD_GET: response = requests.get(url=self.url, headers=self.headers, params=self.parameters, allow_redirects=False, proxies=proxies) elif self.request_method == Constants.REQUEST_METHOD_PUT: data = None if self.request_body is not None: data = converter_instance.append_to_request(self, self.request_body) response = requests.put(url=self.url, data=data, params=self.parameters, headers=self.headers, allow_redirects=False, proxies=proxies) elif self.request_method == Constants.REQUEST_METHOD_POST: data = None if self.request_body is not None: data = converter_instance.append_to_request(self, self.request_body) if self.file: response = requests.post(url=self.url, files=data, headers=self.headers, allow_redirects=False, data={}, proxies=proxies) else: response = requests.post(url=self.url, data=data, params=self.parameters, headers=self.headers, allow_redirects=False, proxies=proxies) elif self.request_method == Constants.REQUEST_METHOD_PATCH: data = None if self.request_body is not None: data = converter_instance.append_to_request(self, self.request_body) response = requests.patch(url=self.url, data=data, headers=self.headers, params=self.parameters, allow_redirects=False, proxies=proxies) elif self.request_method == Constants.REQUEST_METHOD_DELETE: response = requests.delete(url=self.url, headers=self.headers, params=self.parameters, allow_redirects=False, proxies=proxies) return response
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 expected_type = None if data_type in Constants.TYPE_VS_DATATYPE: 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 = False if not isinstance( value, Constants.TYPE_VS_DATATYPE.get(data_type)) else True data_type = Constants.TYPE_VS_DATATYPE.get(data_type).__name__ given_type = type(value).__name__ 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 given_type = type(value).__name__ 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.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
def get_fields_info(module_api_name, handler_instance=None): """ This method to fetch field details of the current module for the current user and store the result in a JSON file. Parameters: module_api_name (str) : A string containing the CRM module API name. handler_instance (CommonAPIHandler) : A CommonAPIHandler instance. """ try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer last_modified_time = None record_field_details_path = None try: with Utility.lock: if module_api_name is not None and Utility.search_json_details(module_api_name) is not None: return initializer = Initializer.get_initializer() resources_path = os.path.join(initializer.resource_path, Constants.FIELD_DETAILS_DIRECTORY) if not os.path.exists(resources_path): os.makedirs(resources_path) module_api_name = Utility.verify_module_api_name(module_api_name) Utility.set_handler_api_path(module_api_name, handler_instance) if handler_instance is not None and handler_instance.get_module_api_name() is None and module_api_name.lower() not in Constants.SKIP_MODULES: return record_field_details_path = Utility.get_file_name() if os.path.exists(record_field_details_path): Utility.file_exists_flow(module_api_name, record_field_details_path, last_modified_time) elif initializer.sdk_config.get_auto_refresh_fields(): Utility.new_file = True Utility.fill_data_type() if len(Utility.api_supported_modules) == 0: Utility.api_supported_modules = Utility.get_modules(None) record_field_details_json = Initializer.get_json(record_field_details_path) if os.path.exists(record_field_details_path) else {} record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] = time.time() * 1000 if len(Utility.api_supported_modules) > 0: for module in Utility.api_supported_modules: if module.lower() not in record_field_details_json: module_data = Utility.api_supported_modules[module] record_field_details_json[module.lower()] = {} Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module_data[Constants.API_NAME]) record_field_details_json = Initializer.get_json(record_field_details_path) record_field_details_json[module.lower()] = field_details Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) Utility.new_file = False elif Utility.force_refresh and not Utility.get_modified_modules: Utility.get_modified_modules = True record_field_details_json = {} Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False else: Utility.fill_data_type() record_field_details_json = {module_api_name.lower(): {}} Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module_api_name) record_field_details_json = Initializer.get_json(record_field_details_path) record_field_details_json[module_api_name.lower()] = field_details Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) except Exception as e: if record_field_details_path is not None and os.path.exists(record_field_details_path): try: record_field_details_json = Initializer.get_json(record_field_details_path) if module_api_name is not None and module_api_name.lower() in record_field_details_json: del record_field_details_json[module_api_name.lower()] if Utility.new_file: if Constants.FIELDS_LAST_MODIFIED_TIME in record_field_details_json: del record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] Utility.new_file = False if Utility.get_modified_modules or Utility.force_refresh: Utility.get_modified_modules = False Utility.force_refresh = False if last_modified_time is not None: record_field_details_json[Constants.FIELDS_LAST_MODIFIED_TIME] = last_modified_time Utility.write_to_file(file_path=record_field_details_path, file_contents=record_field_details_json) except Exception as ex: sdk_exception = SDKException(cause=ex) Utility.logger.error(Constants.EXCEPTION + sdk_exception.__str__()) raise sdk_exception if not isinstance(e, SDKException): e = SDKException(cause=e) Utility.logger.info(Constants.EXCEPTION + e.__str__()) raise e
def get_file_name(): return os.path.join(Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY, Converter.get_encoded_file_name())
def get_file_name(): import zcrmsdk.src.com.zoho.crm.api.util.converter as Converter return os.path.join(Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY, Converter.Converter.get_encoded_file_name())
def api_call(self, class_name, encode_type): """ The method to construct API request and response details. To make the Zoho CRM API calls. Parameters: class_name(str): A str containing the method return type. encode_type(str): A str containing the expected API response content type. Returns: APIResponse: An instance of APIResponse representing the Zoho CRM API response instance Raises: SDKException """ try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer if Initializer.get_initializer() is None: raise SDKException(code=Constants.SDK_UNINITIALIZATION_ERROR, message=Constants.SDK_UNINITIALIZATION_MESSAGE) connector = APIHTTPConnector() try: self.set_api_url(connector) except SDKException as e: CommonAPIHandler.logger.error(Constants.SET_API_URL_EXCEPTION + e.__str__()) raise e except Exception as e: sdk_exception = SDKException(cause=e) CommonAPIHandler.logger.error(Constants.SET_API_URL_EXCEPTION + sdk_exception.__str__()) raise sdk_exception connector.request_method = self.__http_method connector.content_type = self.__content_type if self.__header is not None and len( self.__header.request_headers) > 0: connector.headers = self.__header.request_headers if self.__param is not None and len( self.__param.request_parameters) > 0: connector.parameters = self.__param.request_parameters try: Initializer.get_initializer().token.authenticate(connector) except SDKException as e: CommonAPIHandler.logger.info(Constants.AUTHENTICATION_EXCEPTION + e.__str__()) raise e except Exception as e: sdk_exception = SDKException(cause=e) CommonAPIHandler.logger.error(Constants.AUTHENTICATION_EXCEPTION + sdk_exception.__str__()) raise sdk_exception convert_instance = None if self.__content_type is not None and self.__http_method in [ Constants.REQUEST_METHOD_PATCH, Constants.REQUEST_METHOD_POST, Constants.REQUEST_METHOD_PUT ]: try: convert_instance = self.get_converter_class_instance( self.__content_type.lower()) request = convert_instance.form_request( self.__request, self.__request.__class__.__module__, None, None) except SDKException as e: CommonAPIHandler.logger.info(Constants.FORM_REQUEST_EXCEPTION + e.__str__()) raise e except Exception as e: sdk_exception = SDKException(cause=e) CommonAPIHandler.logger.error( Constants.FORM_REQUEST_EXCEPTION + sdk_exception.__str__()) raise sdk_exception connector.request_body = request try: connector.headers[Constants.ZOHO_SDK] = platform.system( ) + "/" + platform.release( ) + " python/" + platform.python_version( ) + ":" + Constants.SDK_VERSION response = connector.fire_request(convert_instance) return_object = None if Constants.CONTENT_TYPE in response.headers: content_type = response.headers[Constants.CONTENT_TYPE] if ";" in content_type: content_type = content_type.rpartition(";")[0] convert_instance = self.get_converter_class_instance( str(content_type).lower()) return_object = convert_instance.get_wrapped_response( response, class_name) else: CommonAPIHandler.logger.info(response.__str__()) return APIResponse(response.headers, response.status_code, return_object) except SDKException as e: CommonAPIHandler.logger.info(Constants.API_CALL_EXCEPTION + e.__str__()) except Exception as e: sdk_exception = SDKException(cause=e) CommonAPIHandler.logger.error(Constants.API_CALL_EXCEPTION + sdk_exception.__str__()) raise sdk_exception
def remove(self): try: Initializer.get_initializer().store.delete_token(self) return True except Exception: return False
def get_fields_info(module_api_name): """ This method to fetch field details of the current module for the current user and store the result in a JSON file. Parameters: module_api_name (str) : A string containing the CRM module API name. """ try: from zcrmsdk.src.com.zoho.crm.api.initializer import Initializer except Exception: from ..initializer import Initializer last_modified_time = None try: with Utility.lock: resources_path = os.path.join( Initializer.get_initializer().resource_path, Constants.FIELD_DETAILS_DIRECTORY) if not os.path.exists(resources_path): if module_api_name is not None and Utility.search_json_details( module_api_name) is not None: return os.makedirs(resources_path) record_field_details_path = Utility.get_file_name() if os.path.exists(record_field_details_path): record_field_details_json = Initializer.get_json( record_field_details_path) if Initializer.get_initializer( ).sdk_config.get_auto_refresh_fields( ) and not Utility.new_file and not Utility.get_modified_modules and ( Constants.FIELDS_LAST_MODIFIED_TIME not in record_field_details_json or Utility.force_refresh or (time.time() * 1000 - record_field_details_json[ Constants.FIELDS_LAST_MODIFIED_TIME]) > 3600000): Utility.get_modified_modules = True last_modified_time = record_field_details_json[ Constants. FIELDS_LAST_MODIFIED_TIME] if Constants.FIELDS_LAST_MODIFIED_TIME in record_field_details_json else None Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False elif not Initializer.get_initializer( ).sdk_config.get_auto_refresh_fields( ) and Utility.force_refresh and not Utility.get_modified_modules: Utility.get_modified_modules = True Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False record_field_details_json = Initializer.get_json( record_field_details_path) if module_api_name is None or module_api_name.lower( ) in record_field_details_json: return else: Utility.fill_data_type() record_field_details_json[module_api_name.lower()] = {} Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details( module_api_name) record_field_details_json = Initializer.get_json( record_field_details_path) record_field_details_json[ module_api_name.lower()] = field_details Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) elif Initializer.get_initializer( ).sdk_config.get_auto_refresh_fields(): Utility.new_file = True Utility.fill_data_type() module_api_names = Utility.get_modules(None) record_field_details_json = { Constants.FIELDS_LAST_MODIFIED_TIME: time.time() * 1000 } for module in module_api_names: if module.lower() not in record_field_details_json: record_field_details_json[module.lower()] = {} Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module) record_field_details_json = Initializer.get_json( record_field_details_path) record_field_details_json[ module.lower()] = field_details Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) Utility.new_file = False elif Utility.force_refresh and not Utility.get_modified_modules: Utility.get_modified_modules = True record_field_details_json = {} Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) Utility.modify_fields(record_field_details_path, last_modified_time) Utility.get_modified_modules = False else: Utility.fill_data_type() record_field_details_json = {module_api_name.lower(): {}} Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) field_details = Utility.get_fields_details(module_api_name) record_field_details_json = Initializer.get_json( record_field_details_path) record_field_details_json[ module_api_name.lower()] = field_details Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) except Exception as e: if record_field_details_path is not None and os.path.exists( record_field_details_path): try: record_field_details_json = Initializer.get_json( record_field_details_path) if module_api_name is not None and module_api_name.lower( ) in record_field_details_json: del record_field_details_json[module_api_name.lower()] if Utility.new_file: if Constants.FIELDS_LAST_MODIFIED_TIME in record_field_details_json: del record_field_details_json[ Constants.FIELDS_LAST_MODIFIED_TIME] Utility.new_file = False if Utility.get_modified_modules or Utility.force_refresh: Utility.get_modified_modules = False Utility.force_refresh = False if last_modified_time is not None: record_field_details_json[ Constants. FIELDS_LAST_MODIFIED_TIME] = last_modified_time Utility.write_to_file( file_path=record_field_details_path, file_contents=record_field_details_json) except Exception as ex: sdk_exception = SDKException(cause=ex) Utility.logger.error(Constants.EXCEPTION + sdk_exception.__str__()) raise sdk_exception if not isinstance(e, SDKException): e = SDKException(cause=e) Utility.logger.info(Constants.EXCEPTION + e.__str__()) raise e