@@ -37,58 +37,44 @@ def _init_api(self, credentials_file: Path):
37
37
except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
38
38
logging .error (f"Failed to load credentials: { e } " )
39
39
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
-
47
40
def upload_document (self ):
48
41
"""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 } " )
58
49
59
50
def show_links_array (self , links , prefix ):
60
51
for item in links :
61
52
logging .info (f"{ prefix } Link ID: '{ item .id } ' - Link Action: '{ item .action } '" )
62
53
63
54
def get_all_links (self ):
64
55
"""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 } " )
76
65
77
66
def get_link_by_id (self , link_id : str ):
78
67
"""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 } " )
89
75
90
76
if __name__ == "__main__" :
91
77
pdf_links = PdfLinks ()
92
78
pdf_links .upload_document ()
93
79
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