Skip to content

Commit

Permalink
Merge pull request googleapis#108 from Psynbiotik/MessagesWithOauth
Browse files Browse the repository at this point in the history
Use oauth in message calls
  • Loading branch information
Narcolapser authored Jul 20, 2018
2 parents cf536d2 + dc5fbf0 commit 9f84dbe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion O365/fluent_inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ def fetch_next(self, count=1):
params=params)
self.fetched_count += count

connection = Connection()
messages = []
for message in response:
messages.append(Message(message, Connection().auth))
messages.append(Message(message, connection.auth, oauth=connection.oauth))

return messages

Expand Down
28 changes: 14 additions & 14 deletions O365/fluent_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Message(object):
draft_url = 'https://outlook.office365.com/api/v1.0/me/folders/{folder_id}/messages'
update_url = 'https://outlook.office365.com/api/v1.0/me/messages/{0}'

def __init__(self, json=None, auth=None, verify=True):
def __init__(self, json=None, auth=None, verify=True, oauth=None):
'''
Makes a new message wrapper for sending and receiving messages.
Expand All @@ -67,16 +67,16 @@ def __init__(self, json=None, auth=None, verify=True):
self.auth = auth
self.attachments = []
self.receiver = None

self.verify = verify
self.oauth = oauth

def fetchAttachments(self,**kwargs):
'''kicks off the process that downloads attachments locally.'''
if not self.hasAttachments:
log.debug('message has no attachments, skipping out early.')
return False

response = requests.get(self.att_url.format(
response = (self.oauth, requests)[self.oauth is None].get(self.att_url.format(
self.json['Id']), auth=self.auth, verify=self.verify, **kwargs)
log.info('response from O365 for retriving message attachments: %s', str(response))
json = response.json()
Expand All @@ -97,7 +97,7 @@ def sendMessage(self, user_id=None, **kwargs):
:param user_id: User id (email) if sending as other user
'''

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
headers = {'Content-Type': 'application/json', 'Accept': 'text/plain'}

try:
data = {'Message': {'Body': {}}}
Expand All @@ -118,7 +118,7 @@ def sendMessage(self, user_id=None, **kwargs):
url = self.send_as_url.format(user_id=user_id)
else:
usl = self.send_url
response = requests.post(
response = (self.oauth, requests)[self.oauth is None].post(
url, data, headers=headers, auth=self.auth, verify=self.verify, **kwargs)
log.debug('response from server for sending message:' + str(response))
log.debug("respnse body: {}".format(response.text))
Expand All @@ -130,13 +130,13 @@ def sendMessage(self, user_id=None, **kwargs):
def markAsRead(self):
'''marks analogous message as read in the cloud.'''
read = '{"IsRead":true}'
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
try:
response = requests.patch(self.update_url.format(
response = (self.oauth, requests)[self.oauth is None].patch(self.update_url.format(
self.json['Id']), read, headers=headers, auth=self.auth, verify=self.verify)
except:
return False
return True
return response.ok

def moveToFolder(self, folder_id):
"""
Expand All @@ -146,17 +146,17 @@ def moveToFolder(self, folder_id):
:returns: True on success
"""
move_url = 'https://outlook.office365.com/api/v1.0/me/messages/{0}/move'
headers = {'Content-type': 'application/json',
headers = {'Content-Type': 'application/json',
'Accept': 'application/json'}
post_data = {"DestinationId": folder_id}
try:
response = requests.post(move_url.format(self.json['Id']),
response = (self.oauth, requests)[self.oauth is None].post(move_url.format(self.json['Id']),
json=post_data, headers=headers,
auth=self.auth, verify=self.verify)
except:
return False

return True
return response.ok

def getSender(self):
'''get all available information for the sender of the email.'''
Expand Down Expand Up @@ -295,12 +295,12 @@ def setCategory(self, category_name, **kwargs):

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

# To the King!

0 comments on commit 9f84dbe

Please sign in to comment.