Skip to content

Commit e15b794

Browse files
authored
Merge pull request googleapis#98 from hellbe/master
Support for fluent shared mailboxes, 401 error msg and more
2 parents 06eff41 + 58f8cdd commit e15b794

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

O365/connection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ def get_response(request_url, **kwargs):
217217

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

220+
if response.status_code == 401:
221+
raise RuntimeError('API returned status code 401 Unauthorized, check the connection credentials')
222+
220223
response_json = response.json()
221224
if 'value' not in response_json:
222225
raise RuntimeError('Something went wrong, received an unexpected result \n{}'.format(response_json))

O365/fluent_inbox.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ class FluentInbox(object):
1212
'1.0': 'https://outlook.office365.com/api/v1.0/me/messages',
1313
'2.0': 'https://graph.microsoft.com/v1.0/me/messages',
1414
},
15-
1615
'folders': {
1716
'1.0': 'https://outlook.office365.com/api/v1.0/me/Folders',
1817
'2.0': 'https://graph.microsoft.com/v1.0/me/MailFolders',
1918
},
20-
2119
'folder': {
2220
'1.0': 'https://outlook.office365.com/api/v1.0/me/Folders/{folder_id}/messages',
2321
'2.0': 'https://graph.microsoft.com/v1.0/me/MailFolders/{folder_id}/messages',
@@ -26,6 +24,14 @@ class FluentInbox(object):
2624
'1.0': 'https://outlook.office365.com/api/v1.0/me/Folders/{folder_id}/childfolders',
2725
'2.0': 'https://graph.microsoft.com/v1.0/me/MailFolders/{folder_id}/childfolders',
2826
},
27+
'user_folders': {
28+
'1.0': 'https://outlook.office365.com/api/v1.0/users/{user_id}/Folders',
29+
'2.0': 'https://graph.microsoft.com/v1.0/users/{user_id}/MailFolders',
30+
},
31+
'user_folder': {
32+
'1.0': 'https://outlook.office365.com/api/v1.0/users/{user_id}/Folders/{folder_id}/messages',
33+
'2.0': 'https://graph.microsoft.com/v1.0/users/{user_id}/MailFolders/{folder_id}/messages',
34+
}
2935
}
3036

3137
def __init__(self, verify=True):
@@ -40,35 +46,28 @@ def __init__(self, verify=True):
4046
self.verify = verify
4147
self.messages = []
4248

43-
def from_folder(self, folder_name):
49+
def from_folder(self, folder_name, user_id=None):
4450
""" Configure to use this folder for fetching the mails
4551
4652
:param folder_name: name of the outlook folder
53+
:param user_id: user id the folder belongs to (shared mailboxes)
4754
"""
4855
self._reset()
49-
response = Connection.get_response(FluentInbox._get_url('folders'),
50-
verify=self.verify,
51-
params={'$top': 100})
52-
53-
folder_id = None
54-
all_folders = []
55-
56-
for folder in response:
57-
if folder['displayName'] == folder_name:
58-
folder_id = folder['id']
59-
break
6056

61-
all_folders.append(folder['displayName'])
57+
folder_id = self.get_folder(value=folder_name,
58+
by='DisplayName',
59+
user_id=user_id)['Id']
6260

63-
if not folder_id:
64-
raise RuntimeError('Folder "{}" is not found, available folders '
65-
'are {}'.format(folder_name, all_folders))
66-
67-
self.url = FluentInbox._get_url('folder').format(folder_id=folder_id)
61+
if user_id:
62+
self.url = FluentInbox._get_url('user_folder').format(
63+
user_id=user_id, folder_id=folder_id)
64+
else:
65+
self.url = FluentInbox._get_url('folder').format(
66+
folder_id=folder_id)
6867

6968
return self
7069

71-
def get_folder(self, value, by='Id', parent_id=None):
70+
def get_folder(self, value, by='Id', parent_id=None, user_id=None):
7271
"""
7372
Return a folder by a given attribute. If multiple folders exist by
7473
this attribute, only the first will be returned
@@ -84,11 +83,15 @@ def get_folder(self, value, by='Id', parent_id=None):
8483
8584
:param value: Value that we are searching for
8685
:param by: Search on this key (default: Id)
86+
:param user_id: user id the folder belongs to (shared mailboxes)
8787
:returns: Single folder data
8888
"""
8989
if parent_id:
9090
folders_url = FluentInbox._get_url('child_folders').format(
9191
folder_id=parent_id)
92+
elif user_id:
93+
folders_url = FluentInbox._get_url('user_folders').format(
94+
user_id=user_id)
9295
else:
9396
folders_url = FluentInbox._get_url('folders')
9497

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

113-
def list_folders(self, parent_id=None):
116+
def list_folders(self, parent_id=None, user_id=None):
114117
"""
115118
:param parent_id: Id of parent folder to list. Default to top folder
116119
:return: List of all folder data
117120
"""
118121
if parent_id:
119122
folders_url = FluentInbox._get_url('child_folders').format(
120123
folder_id=parent_id)
124+
elif user_id:
125+
folders_url = FluentInbox._get_url('user_folders').format(
126+
user_id=user_id)
121127
else:
122128
folders_url = FluentInbox._get_url('folders')
123129

O365/fluent_message.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Message(object):
3030
setRecipients -- sets the list of recipients.
3131
setSubject -- sets the subject line.
3232
setBody -- sets the body.
33+
setCategory -- sets the email's category
3334
3435
Variables:
3536
att_url -- url for requestiong attachments. takes message GUID
@@ -279,13 +280,16 @@ def setBodyHTML(self, val=None):
279280
except:
280281
self.json['Body'] = {}
281282

283+
def setCategory(self, category_name, **kwargs):
284+
"Sets the email's category"
285+
self.update_category(self, category_name, **kwargs)
286+
282287
def update_category(self, category_name, **kwargs):
283288
category = '{{"Categories":["{}"]}}'.format(category_name)
284289
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
285290
try:
286291
response = requests.patch(self.update_url.format(
287292
self.json['Id']), category, headers=headers, auth=self.auth, verify=self.verify, **kwargs)
288-
print(response.url)
289293
except:
290294
return False
291295
return True

0 commit comments

Comments
 (0)