Skip to content

Commit

Permalink
Merge pull request googleapis#98 from hellbe/master
Browse files Browse the repository at this point in the history
Support for fluent shared mailboxes, 401 error msg and more
  • Loading branch information
Narcolapser authored Jun 29, 2018
2 parents 06eff41 + 58f8cdd commit e15b794
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
3 changes: 3 additions & 0 deletions O365/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ def get_response(request_url, **kwargs):

log.info('Received response from URL {}'.format(response.url))

if response.status_code == 401:
raise RuntimeError('API returned status code 401 Unauthorized, check the connection credentials')

response_json = response.json()
if 'value' not in response_json:
raise RuntimeError('Something went wrong, received an unexpected result \n{}'.format(response_json))
Expand Down
50 changes: 28 additions & 22 deletions O365/fluent_inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ class FluentInbox(object):
'1.0': 'https://outlook.office365.com/api/v1.0/me/messages',
'2.0': 'https://graph.microsoft.com/v1.0/me/messages',
},

'folders': {
'1.0': 'https://outlook.office365.com/api/v1.0/me/Folders',
'2.0': 'https://graph.microsoft.com/v1.0/me/MailFolders',
},

'folder': {
'1.0': 'https://outlook.office365.com/api/v1.0/me/Folders/{folder_id}/messages',
'2.0': 'https://graph.microsoft.com/v1.0/me/MailFolders/{folder_id}/messages',
Expand All @@ -26,6 +24,14 @@ class FluentInbox(object):
'1.0': 'https://outlook.office365.com/api/v1.0/me/Folders/{folder_id}/childfolders',
'2.0': 'https://graph.microsoft.com/v1.0/me/MailFolders/{folder_id}/childfolders',
},
'user_folders': {
'1.0': 'https://outlook.office365.com/api/v1.0/users/{user_id}/Folders',
'2.0': 'https://graph.microsoft.com/v1.0/users/{user_id}/MailFolders',
},
'user_folder': {
'1.0': 'https://outlook.office365.com/api/v1.0/users/{user_id}/Folders/{folder_id}/messages',
'2.0': 'https://graph.microsoft.com/v1.0/users/{user_id}/MailFolders/{folder_id}/messages',
}
}

def __init__(self, verify=True):
Expand All @@ -40,35 +46,28 @@ def __init__(self, verify=True):
self.verify = verify
self.messages = []

def from_folder(self, folder_name):
def from_folder(self, folder_name, user_id=None):
""" Configure to use this folder for fetching the mails
:param folder_name: name of the outlook folder
:param user_id: user id the folder belongs to (shared mailboxes)
"""
self._reset()
response = Connection.get_response(FluentInbox._get_url('folders'),
verify=self.verify,
params={'$top': 100})

folder_id = None
all_folders = []

for folder in response:
if folder['displayName'] == folder_name:
folder_id = folder['id']
break

all_folders.append(folder['displayName'])
folder_id = self.get_folder(value=folder_name,
by='DisplayName',
user_id=user_id)['Id']

if not folder_id:
raise RuntimeError('Folder "{}" is not found, available folders '
'are {}'.format(folder_name, all_folders))

self.url = FluentInbox._get_url('folder').format(folder_id=folder_id)
if user_id:
self.url = FluentInbox._get_url('user_folder').format(
user_id=user_id, folder_id=folder_id)
else:
self.url = FluentInbox._get_url('folder').format(
folder_id=folder_id)

return self

def get_folder(self, value, by='Id', parent_id=None):
def get_folder(self, value, by='Id', parent_id=None, user_id=None):
"""
Return a folder by a given attribute. If multiple folders exist by
this attribute, only the first will be returned
Expand All @@ -84,11 +83,15 @@ def get_folder(self, value, by='Id', parent_id=None):
:param value: Value that we are searching for
:param by: Search on this key (default: Id)
:param user_id: user id the folder belongs to (shared mailboxes)
:returns: Single folder data
"""
if parent_id:
folders_url = FluentInbox._get_url('child_folders').format(
folder_id=parent_id)
elif user_id:
folders_url = FluentInbox._get_url('user_folders').format(
user_id=user_id)
else:
folders_url = FluentInbox._get_url('folders')

Expand All @@ -110,14 +113,17 @@ def get_folder(self, value, by='Id', parent_id=None):
'Folder "{}" is not found by "{}", available folders '
'are {}'.format(value, by, all_folders))

def list_folders(self, parent_id=None):
def list_folders(self, parent_id=None, user_id=None):
"""
:param parent_id: Id of parent folder to list. Default to top folder
:return: List of all folder data
"""
if parent_id:
folders_url = FluentInbox._get_url('child_folders').format(
folder_id=parent_id)
elif user_id:
folders_url = FluentInbox._get_url('user_folders').format(
user_id=user_id)
else:
folders_url = FluentInbox._get_url('folders')

Expand Down
6 changes: 5 additions & 1 deletion O365/fluent_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Message(object):
setRecipients -- sets the list of recipients.
setSubject -- sets the subject line.
setBody -- sets the body.
setCategory -- sets the email's category
Variables:
att_url -- url for requestiong attachments. takes message GUID
Expand Down Expand Up @@ -279,13 +280,16 @@ def setBodyHTML(self, val=None):
except:
self.json['Body'] = {}

def setCategory(self, category_name, **kwargs):
"Sets the email's category"
self.update_category(self, category_name, **kwargs)

def update_category(self, category_name, **kwargs):
category = '{{"Categories":["{}"]}}'.format(category_name)
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
try:
response = requests.patch(self.update_url.format(
self.json['Id']), category, headers=headers, auth=self.auth, verify=self.verify, **kwargs)
print(response.url)
except:
return False
return True
Expand Down

0 comments on commit e15b794

Please sign in to comment.