Skip to content

Commit 97b802c

Browse files
Update getLinksAndShow.py
1 parent 0ac4857 commit 97b802c

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

Uses-Cases/Links/get/getLinksAndShow.py

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,58 +37,44 @@ 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 show_links_array(self, links, prefix):
6051
for item in links:
6152
logging.info(f"{prefix} Link ID: '{item.id}' - Link Action: '{item.action}'")
6253

6354
def get_all_links(self):
6455
"""Get all hyperlink annotations for a specific PDF document."""
65-
if not self._ensure_api_initialized():
66-
return
67-
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: {e}")
56+
if self.pdf_api:
57+
try:
58+
response = self.pdf_api.get_page_link_annotations( Config.PDF_DOCUMENT_NAME, Config.PAGE_NUMBER)
59+
if response.code == 200:
60+
self.show_links_array(response.links.list, "All: ")
61+
else:
62+
logging.error(f"Failed to add link to the page. Response code: {response.code}")
63+
except Exception as e:
64+
logging.error(f"Error while adding link: {e}")
7665

7766
def get_link_by_id(self, link_id: str):
7867
"""Get hyperlink annotation using the specific Id in PDF document."""
79-
if not self._ensure_api_initialized():
80-
return
81-
82-
try:
83-
result_link = self.pdf_api.get_link_annotation(Config.PDF_DOCUMENT_NAME, link_id)
84-
if result_link.code == 200:
85-
self.show_links_array([result_link.link], "Find: ")
86-
except Exception as e:
87-
logging.error(f"Error while adding link: {e}")
88-
68+
if self.pdf_api:
69+
try:
70+
result_link = self.pdf_api.get_link_annotation(Config.PDF_DOCUMENT_NAME, link_id)
71+
if result_link.code == 200:
72+
self.show_links_array([result_link.link], "Find: ")
73+
except Exception as e:
74+
logging.error(f"Error while adding link: {e}")
8975

9076
if __name__ == "__main__":
9177
pdf_links = PdfLinks()
9278
pdf_links.upload_document()
9379
pdf_links.get_all_links()
94-
pdf_links.get_link_by_id(Config.LINK_FIND_ID)
80+
pdf_links.get_link_by_id(Config.LINK_FIND_ID)

0 commit comments

Comments
 (0)