diff --git a/Uses-Cases/Attachments/add/appendAttachments.py b/Uses-Cases/Attachments/add/appendAttachments.py index 388462dd..765f412a 100644 --- a/Uses-Cases/Attachments/add/appendAttachments.py +++ b/Uses-Cases/Attachments/add/appendAttachments.py @@ -36,24 +36,15 @@ def _init_api(self, credentials_file: Path): except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: logging.error(f"init_api(): Failed to load credentials: {e}") - def _ensure_api_initialized(self): - """Check if the API is initialized before making API calls.""" - if not self.pdf_api: - logging.error("ensure_api_initialized(): PDF API is not initialized. Operation aborted.") - return False - return True - def upload_file(self, fileName: str): """ Upload a local fileName to the Aspose Cloud server. """ - if not self._ensure_api_initialized(): - return - - file_path = Config.LOCAL_FOLDER / fileName - try: - self.pdf_api.upload_file(fileName, str(file_path)) - logging.info(f"upload_file(): File '{fileName}' uploaded successfully.") - except Exception as e: - logging.error(f"upload_document(): Failed to upload file: {e}") + if self.pdf_api: + file_path = Config.LOCAL_FOLDER / fileName + try: + self.pdf_api.upload_file(fileName, str(file_path)) + logging.info(f"upload_file(): File '{fileName}' uploaded successfully.") + except Exception as e: + logging.error(f"upload_document(): Failed to upload file: {e}") def upload_document(self): """ Upload a PDF document to the Aspose Cloud server. """ @@ -61,37 +52,33 @@ def upload_document(self): def download_result(self): """ Download the processed PDF document from the Aspose Cloud server. """ - if not self._ensure_api_initialized(): - return - - try: - file_path = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME) - local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME - shutil.move(file_path, str(local_path)) - logging.info(f"download_result(): File successfully downloaded: {local_path}") - except Exception as e: - logging.error(f"download_result(): Failed to download file: {e}") + if self.pdf_api: + try: + temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME) + local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME + shutil.move(temp_file, str(local_path)) + logging.info(f"download_result(): File successfully downloaded: {local_path}") + except Exception as e: + logging.error(f"download_result(): Failed to download file: {e}") def append_attachmnet(self): """Append a new attachment to the PDF document.""" - if not self._ensure_api_initialized(): - return - - new_attachment = AttachmentInfo( - path = Config.NEW_ATTACHMENT_FILE, - description = 'This is a sample attachment', - mime_type = Config.NEW_ATTACHMENT_MIME, - name = Config.NEW_ATTACHMENT_FILE - ) - - try: - response = self.pdf_api.post_add_document_attachment(Config.PDF_DOCUMENT_NAME, new_attachment) - if response.code == 200: - logging.info(f"append_attachment(): attachment '{Config.NEW_ATTACHMENT_FILE}' added to the document '{Config.PDF_DOCUMENT_NAME}'.") - else: - logging.error(f"append_attachment(): Failed to add attachment to the document. Response code: {response.code}") - except Exception as e: - logging.error(f"append_attachment(): Error while adding attachment: {e}") + if self.pdf_api: + new_attachment = AttachmentInfo( + path = Config.NEW_ATTACHMENT_FILE, + description = 'This is a sample attachment', + mime_type = Config.NEW_ATTACHMENT_MIME, + name = Config.NEW_ATTACHMENT_FILE + ) + + try: + response = self.pdf_api.post_add_document_attachment(Config.PDF_DOCUMENT_NAME, new_attachment) + if response.code == 200: + logging.info(f"append_attachment(): attachment '{Config.NEW_ATTACHMENT_FILE}' added to the document '{Config.PDF_DOCUMENT_NAME}'.") + else: + logging.error(f"append_attachment(): Failed to add attachment to the document. Response code: {response.code}") + except Exception as e: + logging.error(f"append_attachment(): Error while adding attachment: {e}") if __name__ == "__main__": @@ -99,4 +86,4 @@ def append_attachmnet(self): pdf_attachments.upload_document() pdf_attachments.upload_file(Config.NEW_ATTACHMENT_FILE) pdf_attachments.append_attachmnet() - pdf_attachments.download_result() \ No newline at end of file + pdf_attachments.download_result() diff --git a/Uses-Cases/Attachments/get/getAttachmentAndSave.py b/Uses-Cases/Attachments/get/getAttachmentAndSave.py index f9a29d6b..864d132b 100644 --- a/Uses-Cases/Attachments/get/getAttachmentAndSave.py +++ b/Uses-Cases/Attachments/get/getAttachmentAndSave.py @@ -7,7 +7,6 @@ # Configure logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") - class Config: """Configuration parameters.""" CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json") @@ -33,61 +32,48 @@ def _init_api(self, credentials_file: Path): except (FileNotFoundError, json.JSONDecodeError, ValueError) as e: logging.error(f"init_api(): Failed to load credentials: {e}") - def _ensure_api_initialized(self): - """Check if the API is initialized before making API calls.""" - if not self.pdf_api: - logging.error("ensure_api_initialized(): PDF API is not initialized. Operation aborted.") - return False - return True - def upload_document(self): """Upload a PDF document to the Aspose Cloud server.""" - if not self._ensure_api_initialized(): - return - - file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME - try: - self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path)) - logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.") - except Exception as e: - logging.error(f"upload_document(): Failed to upload file: {e}") + if self.pdf_api: + file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME + try: + self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path)) + logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.") + except Exception as e: + logging.error(f"upload_document(): Failed to upload file: {e}") def get_attachments(self): """Get attachments for the PDF document.""" - if not self._ensure_api_initialized(): - return - - try: - response : AttachmentsResponse = self.pdf_api.get_document_attachments(Config.PDF_DOCUMENT_NAME) - if response.code == 200: - logging.info(f"get_attachmnets(): attachments '{response.attachments}' for the document '{Config.PDF_DOCUMENT_NAME}'.") - Config.ATTACHMENT_PATH = response.attachments.list[0].links[0].href; - else: - logging.error(f"get_attachmnets(): Failed to get attachments to the document. Response code: {response.code}") - except Exception as e: - logging.error(f"get_attachmnets(): Error while adding attachment: {e}") + if self.pdf_api: + try: + response : AttachmentsResponse = self.pdf_api.get_document_attachments(Config.PDF_DOCUMENT_NAME) + if response.code == 200: + logging.info(f"get_attachmnets(): attachments '{response.attachments}' for the document '{Config.PDF_DOCUMENT_NAME}'.") + Config.ATTACHMENT_PATH = response.attachments.list[0].links[0].href + else: + logging.error(f"get_attachmnets(): Failed to get attachments to the document. Response code: {response.code}") + except Exception as e: + logging.error(f"get_attachmnets(): Error while adding attachment: {e}") def get_attachment_by_id(self): """Get attachment by Id for the PDF document and save it to local file.""" - if not self._ensure_api_initialized(): - return - - try: - response : AttachmentResponse = self.pdf_api.get_document_attachment_by_index(Config.PDF_DOCUMENT_NAME, Config.ATTACHMENT_PATH) - if response.code == 200: - attachment: Attachment = response.attachment - file_path = self.pdf_api.get_download_document_attachment_by_index(Config.PDF_DOCUMENT_NAME, Config.ATTACHMENT_PATH) - local_path = Config.LOCAL_FOLDER / attachment.name - shutil.copy(file_path, local_path) - logging.info(f"get_attachmnets(): attachments '{file_path}' for the document '{Config.PDF_DOCUMENT_NAME}' successfuly saved.") - else: - logging.error(f"get_attachmnets(): Failed to get attachments to the document. Response code: {response.code}") - except Exception as e: - logging.error(f"get_attachmnets(): Error while get attachment: {e}") + if self.pdf_api: + try: + response : AttachmentResponse = self.pdf_api.get_document_attachment_by_index(Config.PDF_DOCUMENT_NAME, Config.ATTACHMENT_PATH) + if response.code == 200: + attachment: Attachment = response.attachment + temp_file = self.pdf_api.get_download_document_attachment_by_index(Config.PDF_DOCUMENT_NAME, Config.ATTACHMENT_PATH) + local_path = Config.LOCAL_FOLDER / attachment.name + shutil.copy(temp_file, local_path) + logging.info(f"get_attachment_by_id(): attachment '{local_path}' for the document '{Config.PDF_DOCUMENT_NAME}' successfuly saved.") + else: + logging.error(f"get_attachment_by_id(): Failed to get attachment for the document '{Config.PDF_DOCUMENT_NAME}'. Response code: {response.code}") + except Exception as e: + logging.error(f"get_attachment_by_id(): Error while get attachment: {e}") if __name__ == "__main__": pdf_attachments = PdfAttachments() pdf_attachments.upload_document() pdf_attachments.get_attachments() - pdf_attachments.get_attachment_by_id() + pdf_attachments.get_attachment_by_id() \ No newline at end of file