Skip to content

Commit ebb42f5

Browse files
update-attachments-get-uses-cases
1 parent 4ea1703 commit ebb42f5

File tree

1 file changed

+31
-45
lines changed

1 file changed

+31
-45
lines changed

Uses-Cases/Attachments/get/getAttachmentAndSave.py

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# Configure logging
88
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
99

10-
1110
class Config:
1211
"""Configuration parameters."""
1312
CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json")
@@ -33,61 +32,48 @@ def _init_api(self, credentials_file: Path):
3332
except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
3433
logging.error(f"init_api(): Failed to load credentials: {e}")
3534

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-
4335
def upload_document(self):
4436
"""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}")
5444

5545
def get_attachments(self):
5646
"""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}")
6957

7058
def get_attachment_by_id(self):
7159
"""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}")
8773

8874

8975
if __name__ == "__main__":
9076
pdf_attachments = PdfAttachments()
9177
pdf_attachments.upload_document()
9278
pdf_attachments.get_attachments()
93-
pdf_attachments.get_attachment_by_id()
79+
pdf_attachments.get_attachment_by_id()

0 commit comments

Comments
 (0)