forked from O365/python-o365
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.py
166 lines (129 loc) · 5.28 KB
/
contact.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import requests
import base64
import json
import logging
import time
logging.basicConfig(filename='o365.log',level=logging.DEBUG)
log = logging.getLogger(__name__)
class Contact( object ):
'''
Contact manages lists of events on an associated contact on office365.
Methods:
getName - Returns the name of the contact.
getContactId - returns the GUID that identifies the contact on office365
getId - synonym of getContactId
getContacts - kicks off the process of fetching contacts.
Variable:
events_url - the url that is actually called to fetch events. takes an ID, start, and end.
time_string - used for converting between struct_time and json's time format.
'''
con_url = 'https://outlook.office365.com/api/v1.0/me/contacts/{0}'
time_string = '%Y-%m-%dT%H:%M:%SZ'
def __init__(self, json=None, auth=None):
'''
Wraps all the informaiton for managing contacts.
'''
self.json = json
self.auth = auth
if json:
log.debug('translating contact information into local variables.')
self.contactId = json['Id']
self.name = json['DisplayName']
else:
log.debug('there was no json, putting in some dumby info.')
self.json = {'DisplayName':'Jebediah Kerman'}
def delete(self):
'''delete's a contact. cause who needs that guy anyway?'''
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
log.debug('preparing to delete contact.')
response = requests.delete(self.con_url.format(str(self.contactId)),headers=headers,auth=self.auth)
log.debug('response from delete attempt: {0}'.format(str(response)))
return response.status_code == 204
def update(self):
'''updates a contact with information in the local json.'''
if not self.auth:
log.debug('no authentication information, cannot update')
return false
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
data = json.dumps(self.json)
response = None
try:
response = requests.patch(self.con_url.format(str(self.contactId)),data,headers=headers,auth=self.auth)
log.debug('sent update request')
except Exception as e:
if response:
log.debug('response to contact update: {0}'.format(str(response)))
else:
log.error('No response, something is very wrong with update: {0}'.format(str(e)))
return False
log.debug('Response to contact update: {0}'.format(str(response)))
return Contact(response.json(),self.auth)
def create(self):
'''create a contact with information in the local json.'''
if not self.auth:
log.debug('no authentication information, cannot create')
return false
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
data = json.dumps(self.json)
response = None
try:
response = requests.post(self.con_url.format(str(self.contactId)),data,headers=headers,auth=self.auth)
log.debug('sent create request')
except Exception as e:
if response:
log.debug('response to contact create: {0}'.format(str(response)))
else:
log.error('No response, something is very wrong with create: {0}'.format(str(e)))
return False
log.debug('Response to contact create: {0}'.format(str(response)))
return Contact(response.json(),self.auth)
def getContactId(self):
'''Get contact's GUID for office 365. mostly used interally in this library.'''
return self.json['Id']
def getId(self):
'''Get contact's GUID for office 365. mostly used interally in this library.'''
return self.getContactId()
def getName(self):
'''Get the contact's Name.'''
return self.json['DisplayName']
def setName(self,val):
'''sets the display name of the contact.'''
self.json['DisplayName'] = val
def getFirstEmailAddress(self):
'''Get the contact's first Email address. returns just the email address.'''
return self.json['EmailAddresses'][0]['Address']
def getEmailAdresses(self):
'''Get's all the contacts email addresses. returns a list of strings.'''
ret = []
for e in self.json['EmailAddresses']:
ret.append(e['Address'])
def getEmailAddress(self,loc):
'''
This method will return the email address, text only, from the specified location.
As the order in which the addresses may have downloaded is non-deterministic, it can
not be garunteed that the nth address will be in the same position each time.
'''
return self.json['EmailAddresses'][loc]['Address']
def setEmailAddress(self,val,loc):
'''
Sets the email address of the specified index. The download of this information may
not be the same each time, so besure you know which address you are editing before
you use this method.
'''
self.json['EmailAddress'][loc]['Address']
def getFirstEmailInfo(self):
'''gets an email address and it's associated date for the first email address.'''
return self.json['EmailAddresses'][0]
def getAllEmailInfo(self):
'''Gets email addresses and any data that goes with it such as name, returns dict'''
return self.json['EmaillAddresses']
def setEmailInfo(self,val):
'''set the list of email addresses. Must be formated as such:
[{"Address":"[email protected]","Name","your name"},{and the next]
this replaces current inplace email address information.
'''
self.json['EmailAddresses'] = val
def addEmail(self,address,name=None):
'''takes a plain string email, and optionally name, and appends it to list.'''
ins = {'Address':address,'Name':None}
#To the King!