Skip to content

Commit 1900cbe

Browse files
Update appendNewPage.py
1 parent 96bb199 commit 1900cbe

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

Uses-Cases/Pages/add/appendNewPage.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,36 @@ def _init_api(self, credentials_file: Path):
3333
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
3434
logging.error(f"init_api(): Failed to load credentials: {e}")
3535

36-
def _ensure_api_initialized(self):
37-
""" Check if the API is initialized before making API calls. """
38-
if not self.pdf_api:
39-
logging.error("ensure_api_initialized(): PDF API is not initialized. Operation aborted.")
40-
return False
41-
return True
42-
4336
def upload_document(self):
4437
""" Upload a PDF document to the Aspose Cloud server. """
45-
if not self._ensure_api_initialized():
46-
return
47-
48-
file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
49-
try:
50-
self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
51-
logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
52-
except Exception as e:
53-
logging.error(f"upload_document(): Failed to upload file: {e}")
38+
if self.pdf_api:
39+
file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
40+
try:
41+
self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
42+
logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
43+
except Exception as e:
44+
logging.error(f"upload_document(): Failed to upload file: {e}")
5445

5546
def download_result(self):
5647
""" Download the processed PDF document from the Aspose Cloud server. """
57-
if not self._ensure_api_initialized():
58-
return
59-
60-
try:
61-
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
62-
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
63-
shutil.move(temp_file, str(local_path))
64-
logging.info(f"download_result(): File successfully downloaded: {local_path}")
65-
except Exception as e:
66-
logging.error(f"download_result(): Failed to download file: {e}")
48+
if self.pdf_api:
49+
try:
50+
temp_file = self.pdf_api.download_file(Config.PDF_DOCUMENT_NAME)
51+
local_path = Config.LOCAL_FOLDER / Config.LOCAL_RESULT_DOCUMENT_NAME
52+
shutil.move(temp_file, str(local_path))
53+
logging.info(f"download_result(): File successfully downloaded: {local_path}")
54+
except Exception as e:
55+
logging.error(f"download_result(): Failed to download file: {e}")
6756

6857
def add_new_page(self):
6958
""" Add new page to end of the PDF document. """
70-
if not self._ensure_api_initialized():
71-
return
72-
73-
result_pages: DocumentPagesResponse = self.pdf_api.put_add_new_page(Config.PDF_DOCUMENT_NAME)
74-
75-
if result_pages.code == 200 and result_pages.pages:
76-
logging.info(f"Added a new page: {result_pages.pages.list[-1]}")
77-
else:
78-
logging.error("Failed to add a new page.")
59+
if self.pdf_api:
60+
result_pages: DocumentPagesResponse = self.pdf_api.put_add_new_page(Config.PDF_DOCUMENT_NAME)
7961

62+
if result_pages.code == 200 and result_pages.pages:
63+
logging.info(f"Added a new page: {result_pages.pages.list[-1]}")
64+
else:
65+
logging.error("Failed to add a new page.")
8066

8167
if __name__ == "__main__":
8268
pdf_pages = PdfPages()

0 commit comments

Comments
 (0)