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 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) exception.message = exception.__str__() 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]) exception.message = exception.__str__() raise exception
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
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]