Skip to content

Commit 0f0a966

Browse files
Update removeLink.py
1 parent 97b802c commit 0f0a966

File tree

1 file changed

+30
-45
lines changed

1 file changed

+30
-45
lines changed

Uses-Cases/Links/remove/removeLink.py

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,66 +37,51 @@ def _init_api(self, credentials_file: Path):
3737
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
3838
logging.error(f"Failed to load credentials: {e}")
3939

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

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

7261
def show_links_array(self, links, prefix):
7362
for item in links:
7463
logging.info(f"{prefix} Link ID: '{item.id}' - Link Action: '{item.action}'")
7564

7665
def get_all_links(self):
7766
"""Get all hyperlink annotations for a specific PDF document."""
78-
if not self._ensure_api_initialized():
79-
return
80-
81-
try:
82-
response = self.pdf_api.get_page_link_annotations( Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
83-
if response.code == 200:
84-
self.show_links_array(response.links.list, "All: ")
85-
else:
86-
logging.error(f"Failed to add link to the page. Response code: {response.code}")
87-
except Exception as e:
88-
logging.error(f"Error while adding link: No links found - {e}")
67+
if self.pdf_api:
68+
try:
69+
response = self.pdf_api.get_page_link_annotations( Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
70+
if response.code == 200:
71+
self.show_links_array(response.links.list, "All: ")
72+
else:
73+
logging.error(f"Failed to add link to the page. Response code: {response.code}")
74+
except Exception as e:
75+
logging.error(f"Error while adding link: No links found - {e}")
8976

9077
def remove_link_by_id(self):
91-
if not self.pdf_api:
92-
return
78+
if self.pdf_api:
79+
response: AsposeResponse = self.pdf_api.delete_link_annotation(Config.PDF_DOCUMENT_NAME, Config.LINK_FIND_ID)
9380

94-
response: AsposeResponse = self.pdf_api.delete_link_annotation(Config.PDF_DOCUMENT_NAME, Config.LINK_FIND_ID)
95-
96-
if response.code == 200:
97-
logging.info("Link annotation with ID " + Config.LINK_FIND_ID + " has been removed.")
98-
else:
99-
logging.erro("Failed to remove link annotation with ID " + Config.LINK_FIND_ID)
81+
if response.code == 200:
82+
logging.info("Link annotation with ID " + Config.LINK_FIND_ID + " has been removed.")
83+
else:
84+
logging.erro("Failed to remove link annotation with ID " + Config.LINK_FIND_ID)
10085

10186
if __name__ == "__main__":
10287
pdf_links = PdfLinks()

0 commit comments

Comments
 (0)