2
2
import json
3
3
import logging
4
4
from pathlib import Path
5
- from asposepdfcloud import ApiClient , PdfApi , Bookmarks , BookmarksResponse
5
+ from asposepdfcloud import ApiClient , PdfApi , BookmarksResponse
6
6
7
7
# Configure logging
8
8
logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s" )
@@ -33,24 +33,15 @@ def _init_api(self, credentials_file: Path):
33
33
except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
34
34
logging .error (f"Failed to load credentials: { e } " )
35
35
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 ("PDF API is not initialized. Operation aborted." )
40
- return False
41
- return True
42
-
43
36
def upload_document (self ):
44
37
"""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"File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
52
- except Exception as e :
53
- logging .error (f"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"File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
43
+ except Exception as e :
44
+ logging .error (f"Failed to upload file: { e } " )
54
45
55
46
def show_bookmarks_array (self , bookmarks , prefix ):
56
47
for item in bookmarks .list :
@@ -60,19 +51,17 @@ def show_bookmarks_array(self, bookmarks, prefix):
60
51
61
52
def get_all_bookmarks (self ):
62
53
"""Get all bookmarks for a specific PDF document."""
63
- if not self ._ensure_api_initialized ():
64
- return
65
-
66
- try :
67
- response : BookmarksResponse = self .pdf_api .get_document_bookmarks ( Config .PDF_DOCUMENT_NAME )
68
- if response .code == 200 :
69
- self .show_bookmarks_array (response .bookmarks , "All" )
70
- else :
71
- logging .error (f"Failed to get bookmarks for the document. Response code: { response .code } " )
72
- except Exception as e :
73
- logging .error (f"Error while retrieving bookmarks array: { e } " )
54
+ if self .pdf_api :
55
+ try :
56
+ response : BookmarksResponse = self .pdf_api .get_document_bookmarks ( Config .PDF_DOCUMENT_NAME )
57
+ if response .code == 200 :
58
+ self .show_bookmarks_array (response .bookmarks , "All" )
59
+ else :
60
+ logging .error (f"Failed to get bookmarks for the document. Response code: { response .code } " )
61
+ except Exception as e :
62
+ logging .error (f"Error while retrieving bookmarks array: { e } " )
74
63
75
64
if __name__ == "__main__" :
76
65
pdf_bookmarks = PdfBookmarks ()
77
66
pdf_bookmarks .upload_document ()
78
- pdf_bookmarks .get_all_bookmarks ()
67
+ pdf_bookmarks .get_all_bookmarks ()
0 commit comments