def test_405_response(self): """ Test that an APIError is raised """ mock = Mock() mock.status_code = 500 with self.assertRaises(APIError): check_response(mock)
def messages(self): """ Retrieves the messages in this Folder, returning a list of :class:`Messages <pyOutlook.core.message.Message>`.""" headers = self.headers r = requests.get('https://outlook.office.com/api/v2.0/me/MailFolders/' + self.id + '/messages', headers=headers) check_response(r) return Message._json_to_messages(self.account, r.json())
def test_403_response(self): """ Test that an AuthError is raised """ mock = Mock() mock.status_code = 403 with self.assertRaises(AuthError): check_response(mock)
def test_400_response(self): """ Test that a RequestError is raised """ mock = Mock() mock.status_code = 400 with self.assertRaises(RequestError): check_response(mock)
def _move_to(self, destination): endpoint = 'https://outlook.office.com/api/v2.0/me/messages/' + self.message_id + '/move' payload = '{ "DestinationId": "' + destination + '"}' r = requests.post(endpoint, data=payload, headers=self.account._headers) check_response(r) data = r.json() self.message_id = data.get('Id', self.message_id)
def auto_reply_message(self): """ The account's Internal auto reply message. Setting the value will change the auto reply message of the account, automatically setting the status to enabled (but not altering the schedule). """ if self._auto_reply is None: r = requests.get('https://outlook.office.com/api/v2.0/me/MailboxSettings/AutomaticRepliesSetting', headers=self._headers) check_response(r) self._auto_reply = r.json().get('InternalReplyMessage') return self._auto_reply
def contact_overrides(self): endpoint = 'https://outlook.office.com/api/v2.0/me/InferenceClassification/Overrides' if self._contact_overrides is None: r = requests.get(endpoint, headers=self._headers) check_response(r) self._contact_overrides = Contact._json_to_contacts(r.json()) return self._contact_overrides
def delete(self): """Deletes this Folder. Raises: AuthError: Raised if Outlook returns a 401, generally caused by an invalid or expired access token. """ headers = self.headers endpoint = 'https://outlook.office.com/api/v2.0/me/MailFolders/' + self.id r = requests.delete(endpoint, headers=headers) check_response(r)
def _get_messages_from_folder_name(self, folder_name): """ Retrieves all messages from a folder, specified by its name. This only works with "Well Known" folders, such as 'Inbox' or 'Drafts'. Args: folder_name (str): The name of the folder to retrieve Returns: List[:class:`Message <pyOutlook.core.message.Message>` ] """ r = requests.get('https://outlook.office.com/api/v2.0/me/MailFolders/' + folder_name + '/messages', headers=self._headers) check_response(r) return Message._json_to_messages(self, r.json())
def get_message(self, message_id): """Gets message matching provided id. the Outlook email matching the provided message_id. Args: message_id: A string for the intended message, provided by Outlook Returns: :class:`Message <pyOutlook.core.message.Message>` """ r = requests.get('https://outlook.office.com/api/v2.0/me/messages/' + message_id, headers=self._headers) check_response(r) return Message._json_to_message(self, r.json())
def get_folder_by_id(self, folder_id): """ Retrieve a Folder by its Outlook ID Args: folder_id: The ID of the :class:`Folder <pyOutlook.core.folder.Folder>` to retrieve Returns: :class:`Folder <pyOutlook.core.folder.Folder>` """ endpoint = 'https://outlook.office.com/api/v2.0/me/MailFolders/' + folder_id r = requests.get(endpoint, headers=self._headers) check_response(r) return_folder = r.json() return Folder._json_to_folder(self, return_folder)
def set_focused(self, account, is_focused): # type: (OutlookAccount, bool) -> bool """ Emails from this contact will either always be put in the Focused inbox, or always put in Other, based on the value of is_focused. Args: account (OutlookAccount): The :class:`OutlookAccount <pyOutlook.core.main.OutlookAccount>` the override should be set for is_focused (bool): Whether this contact should be set to Focused, or Other. Returns: True if the request was successful """ endpoint = 'https://outlook.office.com/api/v2.0/me/InferenceClassification/Overrides' if is_focused: classification = 'Focused' else: classification = 'Other' data = dict(ClassifyAs=classification, SenderEmailAddress=dict(Address=self.email)) r = requests.post(endpoint, headers=account._headers, data=json.dumps(data)) # Will raise an error if necessary, otherwise returns True result = check_response(r) self.focused = is_focused return result
def move_into(self, destination_folder): # type: (Folder) -> Folder """Move the Folder into a different folder. This makes the Folder provided a child folder of the destination_folder. Raises: AuthError: Raised if Outlook returns a 401, generally caused by an invalid or expired access token. Args: destination_folder: A :class:`Folder <pyOutlook.core.folder.Folder>` that should become the parent Returns: A new :class:`Folder <pyOutlook.core.folder.Folder>` that is now inside of the destination_folder. """ headers = self.headers endpoint = 'https://outlook.office.com/api/v2.0/me/MailFolders/' + self.id + '/move' payload = '{ "DestinationId": "' + destination_folder.id + '"}' r = requests.post(endpoint, headers=headers, data=payload) if check_response(r): return_folder = r.json() return self._json_to_folder(self.account, return_folder)
def _make_api_call(self, http_type, endpoint, extra_headers=None, data=None): # type: (str, str, dict, Any) -> None """ Internal method to handle making calls to the Outlook API and logging both the request and response Args: http_type: (str) 'post' or 'delete' endpoint: (str) The endpoint the request will be made to headers: A dict of headers to send to the requests module in addition to Authorization and Content-Type data: The data to provide to the requests module Raises: MiscError: For errors that aren't a 401 AuthError: For 401 errors """ headers = { "Authorization": "Bearer " + self.account.access_token, "Content-Type": "application/json" } if extra_headers is not None: headers.update(extra_headers) log.debug( 'Making Outlook API request for message (ID: {}) with Headers: {} Data: {}' .format(self.message_id, headers, data)) if http_type == 'post': r = requests.post(endpoint, headers=headers, data=data) elif http_type == 'delete': r = requests.delete(endpoint, headers=headers) elif http_type == 'patch': r = requests.patch(endpoint, headers=headers, data=data) else: raise NotImplemented check_response(r)
def get_folders(self): """ Returns a list of all folders for this account Returns: List[:class:`Folder <pyOutlook.core.folder.Folder>`] """ endpoint = 'https://outlook.office.com/api/v2.0/me/MailFolders/' r = requests.get(endpoint, headers=self._headers) if check_response(r): return Folder._json_to_folders(self, r.json())
def get_messages(self, page=0): """Get first 10 messages in account, across all folders. Keyword Args: page (int): Integer representing the 'page' of results to fetch Returns: List[:class:`Message <pyOutlook.core.message.Message>`] """ endpoint = 'https://outlook.office.com/api/v2.0/me/messages' if page > 0: endpoint = endpoint + '/?%24skip=' + str(page) + '0' log.debug('Getting messages from endpoint: {} with Headers: {}'.format(endpoint, self._headers)) r = requests.get(endpoint, headers=self._headers) check_response(r) return Message._json_to_messages(self, r.json())
def get_subfolders(self): """Retrieve all child Folders inside of this Folder. Raises: AuthError: Raised if Outlook returns a 401, generally caused by an invalid or expired access token. Returns: List[:class:`Folder <pyOutlook.core.folder.Folder>`] """ headers = self.headers endpoint = 'https://outlook.office.com/api/v2.0/me/MailFolders/' + self.id + '/childfolders' r = requests.get(endpoint, headers=headers) if check_response(r): return self._json_to_folders(self.account, r.json())
def create_child_folder(self, folder_name): """Creates a child folder within the Folder it is called from and returns the new Folder object. Args: folder_name: The name of the folder to create Returns: :class:`Folder <pyOutlook.core.folder.Folder>` """ headers = self.headers endpoint = 'https://outlook.office.com/api/v2.0/me/MailFolders/' + self.id + '/childfolders' payload = '{ "DisplayName": "' + folder_name + '"}' r = requests.post(endpoint, headers=headers, data=payload) if check_response(r): return_folder = r.json() return self._json_to_folder(self.account, return_folder)
def attachments(self): # type: () -> [Attachment] if not self._has_attachments: return [] if self._attachments: return self._attachments endpoint = 'https://outlook.office.com/api/v2.0/me/messages/{}/attachments'.format( self.message_id) r = requests.get(endpoint, headers=self.account._headers) if check_response(r): data = r.json() self._attachments = Attachment.json_to_attachments( self.account, data) return self._attachments
def focused(self, value): if not isinstance(value, bool): raise TypeError('Message.focused must be a boolean value') endpoint = "https://outlook.office.com/api/v2.0/me/messages('{}')".format( self.message_id) if value: data = dict(InferenceClassification='Focused') else: data = dict(InferenceClassification='Other') r = requests.patch(endpoint, data=json.dumps(data), headers=self.account._headers) if check_response(r): self._focused = value
def rename(self, new_folder_name): """Renames the Folder to the provided name. Args: new_folder_name: A string of the replacement name. Raises: AuthError: Raised if Outlook returns a 401, generally caused by an invalid or expired access token. Returns: A new Folder representing the folder with the new name on Outlook. """ headers = self.headers endpoint = 'https://outlook.office.com/api/v2.0/me/MailFolders/' + self.id payload = '{ "DisplayName": "' + new_folder_name + '"}' r = requests.patch(endpoint, headers=headers, data=payload) if check_response(r): return_folder = r.json() return self._json_to_folder(self.account, return_folder)
def copy_into(self, destination_folder): # type: (Folder) -> Folder """Copies the Folder into the provided destination folder. Raises: AuthError: Raised if Outlook returns a 401, generally caused by an invalid or expired access token. Args: destination_folder: The Folder that this Folder should be copied to. Returns: A new :class:`Folder <pyOutlook.core.folder.Folder>` representing the newly created folder. """ headers = self.headers endpoint = 'https://outlook.office.com/api/v2.0/me/MailFolders/' + self.id + '/copy' payload = '{ "DestinationId": "' + destination_folder.id + '"}' r = requests.post(endpoint, headers=headers, data=payload) if check_response(r): return_folder = r.json() return self._json_to_folder(self.account, return_folder)