7
7
# Configure logging
8
8
logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s" )
9
9
10
-
11
10
class Config :
12
11
"""Configuration parameters."""
13
12
CREDENTIALS_FILE = Path (r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json" )
@@ -33,61 +32,48 @@ def _init_api(self, credentials_file: Path):
33
32
except (FileNotFoundError , json .JSONDecodeError , ValueError ) as e :
34
33
logging .error (f"init_api(): Failed to load credentials: { e } " )
35
34
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 ("ensure_api_initialized(): PDF API is not initialized. Operation aborted." )
40
- return False
41
- return True
42
-
43
35
def upload_document (self ):
44
36
"""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"upload_document(): File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
52
- except Exception as e :
53
- logging .error (f"upload_document(): Failed to upload file: { e } " )
37
+ if self .pdf_api :
38
+ file_path = Config .LOCAL_FOLDER / Config .PDF_DOCUMENT_NAME
39
+ try :
40
+ self .pdf_api .upload_file (Config .PDF_DOCUMENT_NAME , str (file_path ))
41
+ logging .info (f"upload_document(): File { Config .PDF_DOCUMENT_NAME } uploaded successfully." )
42
+ except Exception as e :
43
+ logging .error (f"upload_document(): Failed to upload file: { e } " )
54
44
55
45
def get_attachments (self ):
56
46
"""Get attachments for the PDF document."""
57
- if not self ._ensure_api_initialized ():
58
- return
59
-
60
- try :
61
- response : AttachmentsResponse = self .pdf_api .get_document_attachments (Config .PDF_DOCUMENT_NAME )
62
- if response .code == 200 :
63
- logging .info (f"get_attachmnets(): attachments '{ response .attachments } ' for the document '{ Config .PDF_DOCUMENT_NAME } '." )
64
- Config .ATTACHMENT_PATH = response .attachments .list [0 ].links [0 ].href ;
65
- else :
66
- logging .error (f"get_attachmnets(): Failed to get attachments to the document. Response code: { response .code } " )
67
- except Exception as e :
68
- logging .error (f"get_attachmnets(): Error while adding attachment: { e } " )
47
+ if self .pdf_api :
48
+ try :
49
+ response : AttachmentsResponse = self .pdf_api .get_document_attachments (Config .PDF_DOCUMENT_NAME )
50
+ if response .code == 200 :
51
+ logging .info (f"get_attachmnets(): attachments '{ response .attachments } ' for the document '{ Config .PDF_DOCUMENT_NAME } '." )
52
+ Config .ATTACHMENT_PATH = response .attachments .list [0 ].links [0 ].href
53
+ else :
54
+ logging .error (f"get_attachmnets(): Failed to get attachments to the document. Response code: { response .code } " )
55
+ except Exception as e :
56
+ logging .error (f"get_attachmnets(): Error while adding attachment: { e } " )
69
57
70
58
def get_attachment_by_id (self ):
71
59
"""Get attachment by Id for the PDF document and save it to local file."""
72
- if not self ._ensure_api_initialized ():
73
- return
74
-
75
- try :
76
- response : AttachmentResponse = self .pdf_api .get_document_attachment_by_index (Config .PDF_DOCUMENT_NAME , Config .ATTACHMENT_PATH )
77
- if response .code == 200 :
78
- attachment : Attachment = response .attachment
79
- file_path = self .pdf_api .get_download_document_attachment_by_index (Config .PDF_DOCUMENT_NAME , Config .ATTACHMENT_PATH )
80
- local_path = Config .LOCAL_FOLDER / attachment .name
81
- shutil .copy (file_path , local_path )
82
- logging .info (f"get_attachmnets(): attachments '{ file_path } ' for the document '{ Config .PDF_DOCUMENT_NAME } ' successfuly saved." )
83
- else :
84
- logging .error (f"get_attachmnets(): Failed to get attachments to the document. Response code: { response .code } " )
85
- except Exception as e :
86
- logging .error (f"get_attachmnets(): Error while get attachment: { e } " )
60
+ if self .pdf_api :
61
+ try :
62
+ response : AttachmentResponse = self .pdf_api .get_document_attachment_by_index (Config .PDF_DOCUMENT_NAME , Config .ATTACHMENT_PATH )
63
+ if response .code == 200 :
64
+ attachment : Attachment = response .attachment
65
+ temp_file = self .pdf_api .get_download_document_attachment_by_index (Config .PDF_DOCUMENT_NAME , Config .ATTACHMENT_PATH )
66
+ local_path = Config .LOCAL_FOLDER / attachment .name
67
+ shutil .copy (temp_file , local_path )
68
+ logging .info (f"get_attachment_by_id(): attachment '{ local_path } ' for the document '{ Config .PDF_DOCUMENT_NAME } ' successfuly saved." )
69
+ else :
70
+ logging .error (f"get_attachment_by_id(): Failed to get attachment for the document '{ Config .PDF_DOCUMENT_NAME } '. Response code: { response .code } " )
71
+ except Exception as e :
72
+ logging .error (f"get_attachment_by_id(): Error while get attachment: { e } " )
87
73
88
74
89
75
if __name__ == "__main__" :
90
76
pdf_attachments = PdfAttachments ()
91
77
pdf_attachments .upload_document ()
92
78
pdf_attachments .get_attachments ()
93
- pdf_attachments .get_attachment_by_id ()
79
+ pdf_attachments .get_attachment_by_id ()
0 commit comments