This repository was archived by the owner on Mar 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathoutlookservice.py
More file actions
105 lines (82 loc) · 4.15 KB
/
outlookservice.py
File metadata and controls
105 lines (82 loc) · 4.15 KB
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
# Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See LICENSE.txt in the project root for license information.
import requests
import uuid
import json
graph_endpoint = 'https://graph.microsoft.com/v1.0{0}'
# Generic API Sending
def make_api_call(method, url, token, payload = None, parameters = None):
# Send these headers with all API calls
headers = { 'User-Agent' : 'python_tutorial/1.0',
'Authorization' : 'Bearer {0}'.format(token),
'Accept' : 'application/json' }
# Use these headers to instrument calls. Makes it easier
# to correlate requests and responses in case of problems
# and is a recommended best practice.
request_id = str(uuid.uuid4())
instrumentation = { 'client-request-id' : request_id,
'return-client-request-id' : 'true' }
headers.update(instrumentation)
response = None
if (method.upper() == 'GET'):
response = requests.get(url, headers = headers, params = parameters)
elif (method.upper() == 'DELETE'):
response = requests.delete(url, headers = headers, params = parameters)
elif (method.upper() == 'PATCH'):
headers.update({ 'Content-Type' : 'application/json' })
response = requests.patch(url, headers = headers, data = json.dumps(payload), params = parameters)
elif (method.upper() == 'POST'):
headers.update({ 'Content-Type' : 'application/json' })
response = requests.post(url, headers = headers, data = json.dumps(payload), params = parameters)
return response
def get_me(access_token):
get_me_url = graph_endpoint.format('/me')
# Use OData query parameters to control the results
# - Only return the displayName and mail fields
query_parameters = {'$select': 'displayName,mail'}
r = make_api_call('GET', get_me_url, access_token, "", parameters = query_parameters)
if (r.status_code == requests.codes.ok):
return r.json()
else:
return "{0}: {1}".format(r.status_code, r.text)
def get_my_messages(access_token):
get_messages_url = graph_endpoint.format('/me/mailfolders/inbox/messages')
# Use OData query parameters to control the results
# - Only first 10 results returned
# - Only return the ReceivedDateTime, Subject, and From fields
# - Sort the results by the ReceivedDateTime field in descending order
query_parameters = {'$top': '10',
'$select': 'receivedDateTime,subject,from',
'$orderby': 'receivedDateTime DESC'}
r = make_api_call('GET', get_messages_url, access_token, parameters = query_parameters)
if (r.status_code == requests.codes.ok):
return r.json()
else:
return "{0}: {1}".format(r.status_code, r.text)
def get_my_events(access_token):
get_events_url = graph_endpoint.format('/me/events')
# Use OData query parameters to control the results
# - Only first 10 results returned
# - Only return the Subject, Start, and End fields
# - Sort the results by the Start field in ascending order
query_parameters = {'$top': '10',
'$select': 'subject,start,end',
'$orderby': 'start/dateTime ASC'}
r = make_api_call('GET', get_events_url, access_token, parameters = query_parameters)
if (r.status_code == requests.codes.ok):
return r.json()
else:
return "{0}: {1}".format(r.status_code, r.text)
def get_my_contacts(access_token):
get_contacts_url = graph_endpoint.format('/me/contacts')
# Use OData query parameters to control the results
# - Only first 10 results returned
# - Only return the GivenName, Surname, and EmailAddresses fields
# - Sort the results by the GivenName field in ascending order
query_parameters = {'$top': '10',
'$select': 'givenName,surname,emailAddresses',
'$orderby': 'givenName ASC'}
r = make_api_call('GET', get_contacts_url, access_token, parameters = query_parameters)
if (r.status_code == requests.codes.ok):
return r.json()
else:
return "{0}: {1}".format(r.status_code, r.text)