Skip to content

Commit

Permalink
Fixed issue googleapis#5. made a few bugs in the process. inbox is no…
Browse files Browse the repository at this point in the history
…t passing

it's test suite. So I'll need to sort that out. but the day is now
over. time to go spend some time with my Fiancee.
  • Loading branch information
Toben Archer committed May 8, 2015
1 parent 9852ba6 commit f0d61f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
35 changes: 30 additions & 5 deletions O365/inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Inbox( object ):
inbox_url -- url used for fetching emails.
'''
#url for fetching emails. Takes a flag for whether they are read or not.
inbox_url = 'https://outlook.office365.com/api/v1.0/me/messages?$filter=IsRead eq {0}'
inbox_url = 'https://outlook.office365.com/api/v1.0/me/messages'

def __init__(self, email, password,getNow=True):
'''
Expand All @@ -43,12 +43,15 @@ def __init__(self, email, password,getNow=True):
log.debug('creating inbox for the email %s',email)
self.auth = (email,password)
self.messages = []

self.filters = ''

if getNow:
self.filters = 'IsRead eq false'
self.getMessages()


def getMessages(self,IsRead=False):
def getMessages(self):
'''
Downloads messages to local memory.
Expand All @@ -57,11 +60,12 @@ def getMessages(self,IsRead=False):
init method, so it's kind of pointless for you. Unless you think new
messages have come in.
IsRead: Set this as True if you want to include messages that have been read.
You can filter only certain emails by setting filters. See the set and
get filters methods for more information.
'''

log.debug('fetching messages.')
response = requests.get(self.inbox_url.format(str(IsRead).lower()),auth=self.auth)
log.debug('fetching messages.')
response = requests.get(self.inbox_url,auth=self.auth,params={'$filter':self.filters})
log.info('Response from O365: %s', str(response))

for message in response.json()['value']:
Expand All @@ -83,4 +87,25 @@ def getMessages(self,IsRead=False):
log.debug('all messages retrieved and put in to the list.')
return True

def getFilter(self):
'''get the value set for a specific filter, if exists, else None'''
return self.filters

def setFilter(self,f_string):
'''
Set the value of a filter. More information on what filters are available
can be found here:
https://msdn.microsoft.com/office/office365/APi/complex-types-for-mail-contacts-calendar#RESTAPIResourcesMessage
I may in the future have the ability to add these in yourself. but right now that is to complicated.
Arguments:
f_string -- The string that represents the filters you want to enact.
should be something like: (HasAttachments eq true) and (IsRead eq false)
or just: IsRead eq false
test your filter stirng here: https://outlook.office365.com/api/v1.0/me/messages?$filter=
if that accepts it then you know it works.
'''
self.filters = f_string
return True

#To the King!
33 changes: 21 additions & 12 deletions tests/test_inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ def json(self):
un_rep = open('unread_message.json','r').read()


def get(url,**params):
if url == 'https://outlook.office365.com/api/v1.0/me/messages?$filter=IsRead eq true':
ret = Resp(read_rep)
elif url == 'https://outlook.office365.com/api/v1.0/me/messages?$filter=IsRead eq false':
ret = Resp(un_rep)
def get(url,auth,params):
if url == 'https://outlook.office365.com/api/v1.0/me/messages':
print params
if params == {'$filter': 'IsRead eq false'}:
print 'getting the unread'
ret = Resp(un_rep)
else:
print 'getting the read'
ret = Resp(read_rep)
else:
raise
if params['auth'][0] != 'test@unit.com':
raise
if params['auth'][1] != 'pass':
raise
raise Exception('Wrong URL')
if auth[0] != 'test@unit.com':
raise Exception('Wrong Email')
if auth[1] != 'pass':
raise Exception('Wrong Password')

return ret

Expand All @@ -51,6 +55,7 @@ def test_getMessages(self):
#test to see what happens when they try to download again. this specifically
#addresses an issue raised in on github for issue #3
self.preFetch.getMessages()
self.JITFetch.setFilter('IsRead eq false')
self.JITFetch.getMessages()
self.assertEqual(len(self.preFetch.messages),1)
self.assertEqual(len(self.JITFetch.messages),1)
Expand All @@ -63,8 +68,9 @@ def test_getUnread(self):


#now fetch the un-read emails. prefetch should still have one extra.
self.preFetch.getMessages(IsRead=True)
self.JITFetch.getMessages(IsRead=True)
self.preFetch.getMessages()
self.JITFetch.setFilter('IsRead eq false')
self.JITFetch.getMessages()
self.assertEqual(len(self.preFetch.messages),5)
self.assertEqual(len(self.JITFetch.messages),4)

Expand All @@ -76,6 +82,9 @@ def test_auth(self):
self.assertEqual('test@unit.com',self.JITFetch.auth[0])
self.assertEqual('pass',self.JITFetch.auth[1])

def test_filters(self):
pass


if __name__ == '__main__':
unittest.main()

0 comments on commit f0d61f4

Please sign in to comment.