@@ -37,62 +37,49 @@ def _init_api(self, credentials_file: Path):
3737 except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
3838 logging .error (f"init_api(): 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 ("ensure_api_initialized(): 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"upload_document(): File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
56- except Exception as e :
57- logging .error (f"upload_document(): 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"upload_document(): File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
47+ except Exception as e :
48+ logging .error (f"upload_document(): 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- file_path = self .pdf_api .download_file (Config .PDF_DOCUMENT_NAME )
66- local_path = Config .LOCAL_FOLDER / Config .LOCAL_RESULT_DOCUMENT_NAME
67- shutil .move (file_path , 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 append_link (self ):
7362 """Append a new hyperlink annotation to a specific page in the PDF document."""
74- if not self ._ensure_api_initialized ():
75- return
76-
77- link_annotation = LinkAnnotation (
78- links = [Link (href = Config .NEW_LINK_ACTION )],
79- action_type = LinkActionType .GOTOURIACTION ,
80- action = Config .NEW_LINK_ACTION ,
81- highlighting = LinkHighlightingMode .INVERT ,
82- color = Color (a = 255 , r = 0 , g = 255 , b = 0 ),
83- rect = Config .LINK_RECT ,
84- )
85-
86- try :
87- response = self .pdf_api .post_page_link_annotations (
88- Config .PDF_DOCUMENT_NAME , Config .PAGE_NUMBER , [link_annotation ]
63+ if self .pdf_api :
64+ link_annotation = LinkAnnotation (
65+ links = [Link (href = Config .NEW_LINK_ACTION )],
66+ action_type = LinkActionType .GOTOURIACTION ,
67+ action = Config .NEW_LINK_ACTION ,
68+ highlighting = LinkHighlightingMode .INVERT ,
69+ color = Color (a = 255 , r = 0 , g = 255 , b = 0 ),
70+ rect = Config .LINK_RECT ,
8971 )
90- if response .code == 200 :
91- logging .info (f"append_link(): Link '{ Config .NEW_LINK_ACTION } ' added to page #{ Config .PAGE_NUMBER } ." )
92- else :
93- logging .error (f"append_link(): Failed to add link to the page. Response code: { response .code } " )
94- except Exception as e :
95- logging .error (f"append_link(): Error while adding link: { e } " )
72+
73+ try :
74+ response = self .pdf_api .post_page_link_annotations (
75+ Config .PDF_DOCUMENT_NAME , Config .PAGE_NUMBER , [link_annotation ]
76+ )
77+ if response .code == 200 :
78+ logging .info (f"append_link(): Link '{ Config .NEW_LINK_ACTION } ' added to page #{ Config .PAGE_NUMBER } ." )
79+ else :
80+ logging .error (f"append_link(): Failed to add link to the page. Response code: { response .code } " )
81+ except Exception as e :
82+ logging .error (f"append_link(): Error while adding link: { e } " )
9683
9784
9885if __name__ == "__main__" :
0 commit comments