def handle_faulty_responses(self): if (self.status_code == APIConstants.RESPONSECODE_NO_CONTENT): errorMsg = APIConstants.INVALID_DATA + "-" + APIConstants.INVALID_ID_MSG exception = ZCRMException(self.url, self.status_code, errorMsg, APIConstants.NO_CONTENT, None, errorMsg) raise exception else: responseJSON = self.response_json exception = ZCRMException(self.url, self.status_code, responseJSON[APIConstants.MESSAGE], responseJSON[APIConstants.CODE], responseJSON[APIConstants.DETAILS], responseJSON[APIConstants.MESSAGE]) raise exception
def download_attachment(self): try: self.authenticate_request() connector=HTTPConnector.get_instance(self.url, self.request_params, self.request_headers, self.request_body, self.request_method, self.request_api_key, True) response=connector.trigger_request() if response.status_code==APIConstants.RESPONSECODE_OK: file_res= FileAPIResponse(response,response.status_code,self.url) file_res.status=APIConstants.STATUS_SUCCESS content_disp=response.headers['Content-Disposition'] start_index=content_disp.rindex("'") file_res.file_name=content_disp[start_index+1:] file_res.response_headers=response.headers return file_res elif(response.status_code==APIConstants.RESPONSECODE_NO_CONTENT): errorMsg=APIConstants.INVALID_DATA+"-"+APIConstants.INVALID_ID_MSG exception=ZCRMException(self.url,response.status_code,errorMsg,APIConstants.NO_CONTENT,None,errorMsg) exception.message = exception.__str__() raise exception else: responseJSON=response.json() exception=ZCRMException(self.url,response.status_code,responseJSON[APIConstants.MESSAGE],responseJSON[APIConstants.CODE],responseJSON[APIConstants.DETAILS],responseJSON[APIConstants.MESSAGE]) exception.message = exception.__str__() raise exception except ZCRMException as ex: raise ex except Exception as ex: try: from .Utility import CommonUtil except ImportError: from Utility import CommonUtil import traceback CommonUtil.raise_exception(self.url,ex.message,traceback.format_stack())
def raise_exception(url,message,details,content=None): zcrm_exception=ZCRMException(url,APIConstants.RESPONSECODE_INVALID_INPUT,message,APIConstants.STATUS_ERROR,details,content) import logging try: from .CLException import Logger except ImportError: from CLException import Logger Logger.add_log(message,logging.ERROR,zcrm_exception) raise zcrm_exception
def get_access_token(self): try: from .RestClient import ZCRMRestClient except ImportError: from RestClient import ZCRMRestClient userEmail=ZCRMRestClient.get_instance().get_current_user_email_id() if(userEmail==None and (ZCRMConfigUtil.config_prop_dict['currentUserEmail']==None or ZCRMConfigUtil.config_prop_dict['currentUserEmail'].strip()=='')): raise ZCRMException('fetching current user email',400,'Current user should either be set in ZCRMRestClient or in configuration.properties file',APIConstants.STATUS_ERROR) elif(userEmail==None): userEmail=ZCRMConfigUtil.config_prop_dict['currentUserEmail'] clientIns=ZohoOAuth.get_client_instance() return clientIns.get_access_token(userEmail)
def process_response_data(self): respJson = self.response_json if (self.api_key in respJson): respJson = self.response_json[self.api_key] if (isinstance(respJson, list)): respJson = respJson[0] if (APIConstants.STATUS in respJson and (respJson[APIConstants.STATUS] == APIConstants.STATUS_ERROR)): exception = ZCRMException(self.url, self.status_code, respJson[APIConstants.MESSAGE], respJson[APIConstants.CODE], respJson[APIConstants.DETAILS], respJson[APIConstants.STATUS]) exception.message = exception.__str__() raise exception elif (APIConstants.STATUS in respJson and (respJson[APIConstants.STATUS] == APIConstants.STATUS_SUCCESS)): self.status = respJson[APIConstants.STATUS] self.code = respJson[APIConstants.CODE] self.message = respJson[APIConstants.MESSAGE] self.details = respJson[APIConstants.DETAILS]
def handle_faulty_responses(self): if (self.status_code == APIConstants.RESPONSECODE_NO_CONTENT or self.status_code == APIConstants.RESPONSECODE_NOT_MODIFIED): errorMsg = APIConstants.INVALID_DATA + "-" + APIConstants.INVALID_ID_MSG if self.status_code == APIConstants.RESPONSECODE_NO_CONTENT else APIConstants.NOT_MODIFIED exception_code = APIConstants.NO_CONTENT if self.status_code == APIConstants.RESPONSECODE_NO_CONTENT else APIConstants.NOT_MODIFIED exception = ZCRMException(self.url, self.status_code, errorMsg, exception_code, None, errorMsg) exception.message = exception.__str__() raise exception else: responseJSON = self.response_json exception = ZCRMException(self.url, self.status_code, responseJSON['message'], responseJSON['code'], responseJSON['details'], responseJSON['message']) exception.message = exception.__str__() raise exception