-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.py
114 lines (86 loc) · 3.65 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
__author__ = '[email protected] (Igor Fridman)'
import logging
import requests
logger = logging.getLogger(__name__)
class OutlookService():
OUTLOOK_SERVICE_URL = "https://outlook.office365.com/api/v1.0/users('{0}')"
def __init__(self, credentials):
self.credentials = credentials
def messages(self):
return self.Messages(self)
def folders(self):
return self.Folders(self)
class Folders():
def __init__(self, outer):
self.outer = outer
self.url = outer.OUTLOOK_SERVICE_URL + "/folders"
# GET https://outlook.office365.com/api/v1.0/me/folders
#GET https://outlook.office365.com/api/v1.0/me/folders/{folder_id}/childfolders
def list(self, user_context, folder_id=None):
'''
Fetch list of folders
:param user_context: user to fetch
:param folder_id: optional list sub-folders
:return:
'''
url = self.url.format(user_context)
r = requests.get(url, auth=self.outer.credentials, verify=True)
logger.debug('Response: {0}'.format(r))
if r.status_code == requests.codes.ok:
return r.json()['value']
return None
class Messages():
def __init__(self, outer):
self.outer = outer
self.url = outer.OUTLOOK_SERVICE_URL + "/messages"
# Retrieves a set of messages from the user's Inbox
# parameters:
# user_id: user identifier
# parameters: string. An optional string containing query parameters to filter, sort, etc.
# http://msdn.microsoft.com/office/office365/APi/complex-types-for-mail-contacts-calendar#UseODataqueryparameters
def list(self, user_context, parameters=None):
logger.debug('Entering list messages.')
url = self.url.format(user_context)
if parameters:
url = url + parameters
logger.debug('url: {0}'.format(url))
r = requests.get(url, auth=self.outer.credentials, verify=True)
logger.debug('Response: {0}'.format(r))
if r.status_code == requests.codes.ok:
return r.json()
return None
def count(self, user_context, parameters=None):
"""
Return count of messages
:param user_context:
:param parameters:
:return:
"""
logger.debug('entering count message')
url = self.url.format(user_context) + "/$count"
if parameters:
url = url + parameters;
logger.debug('url: {0}'.format(url))
r = requests.get(url, auth=self.outer.credentials, verify=True)
logger.debug('Response: {0}'.format(r))
if r.status_code == requests.codes.ok:
return r.content
return None
def get(self, user_context, message_id, parameters=None):
"""
Retrieve a single message by id
:param user_context:
:param message_id:
:param parameters:
:return:
"""
logger.debug('entering get message.')
logger.debug('message id {}'.format(message_id))
url = self.url.format(user_context) + "/{0}".format(message_id)
if parameters:
url = url + parameters
logger.debug('url: {0}'.format(url))
r = requests.get(url, auth=self.outer.credentials, verify=True)
if r.status_code == requests.codes.ok:
return r.json()
return None