| 44 | |
| 45 | |
| 46 | class MessageAttachments(BaseAttachments): |
| 47 | _endpoints = { |
| 48 | 'attachments': '/messages/{id}/attachments', |
| 49 | 'attachment': '/messages/{id}/attachments/{ida}', |
| 50 | 'get_mime': '/messages/{id}/attachments/{ida}/$value', |
| 51 | 'create_upload_session': '/messages/{id}/attachments/createUploadSession' |
| 52 | |
| 53 | } |
| 54 | _attachment_constructor = MessageAttachment |
| 55 | |
| 56 | def save_as_eml(self, attachment, to_path=None): |
| 57 | """ Saves this message as and EML to the file system |
| 58 | :param MessageAttachment attachment: the MessageAttachment to store as eml. |
| 59 | :param Path or str to_path: the path where to store this file |
| 60 | """ |
| 61 | mime_content = self.get_mime_content(attachment) |
| 62 | if not mime_content: |
| 63 | return False |
| 64 | |
| 65 | if to_path is None: |
| 66 | to_path = Path('message_eml.eml') |
| 67 | else: |
| 68 | if not isinstance(to_path, Path): |
| 69 | to_path = Path(to_path) |
| 70 | |
| 71 | if not to_path.suffix: |
| 72 | to_path = to_path.with_suffix('.eml') |
| 73 | |
| 74 | with to_path.open('wb') as file_obj: |
| 75 | file_obj.write(mime_content) |
| 76 | return True |
| 77 | |
| 78 | def get_mime_content(self, attachment): |
| 79 | """ Returns the MIME contents of this attachment """ |
| 80 | if not attachment or not isinstance(attachment, MessageAttachment) \ |
| 81 | or attachment.attachment_id is None or attachment.attachment_type != 'item': |
| 82 | raise ValueError('Must provide a saved "item" attachment of type MessageAttachment') |
| 83 | |
| 84 | msg_id = self._parent.object_id |
| 85 | if msg_id is None: |
| 86 | raise RuntimeError('Attempting to get the mime contents of an unsaved message') |
| 87 | |
| 88 | url = self.build_url(self._endpoints.get('get_mime').format(id=msg_id, ida=attachment.attachment_id)) |
| 89 | |
| 90 | response = self._parent.con.get(url) |
| 91 | |
| 92 | if not response: |
| 93 | return None |
| 94 | |
| 95 | return response.content |
| 96 | |
| 97 | |
| 98 | class MessageFlag(ApiComponent): |