Skip to content

Commit bd9a51e

Browse files
committed
Adds some examples in README.md. Improvements in create_contact. Some minor fixes
1 parent 0077ab3 commit bd9a51e

File tree

5 files changed

+98
-41
lines changed

5 files changed

+98
-41
lines changed

README.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ response = client.mail.get_message(message_id)
6868

6969
#### Send mail
7070
```
71-
response = client.mail.send_mail(subject, content, to_recipients)
71+
data = {
72+
subject="Meet for lunch?",
73+
content="The new cafeteria is open.",
74+
content_type="text",
75+
to_recipients=["[email protected]"],
76+
cc_recipients=None,
77+
save_to_sent_items=True,
78+
}
79+
response = client.mail.send_mail(**data)
7280
```
7381

7482
### Notes
@@ -110,7 +118,24 @@ response = client.calendar.get_event(event_id)
110118

111119
#### Create calendar event
112120
```
113-
response = client.calendar.create_event(subject, content, start_datetime,start_timezone, end_datetime, end_timezone, location, calendar, content_type)
121+
from datetime import datetime, timedelta
122+
123+
start_datetime = datetime.now() + timedelta(days=1) # tomorrow
124+
end_datetime = datetime.now() + timedelta(days=1, hours=1) # tomorrow + one hour
125+
timezone = "America/Bogota"
126+
127+
data = {
128+
"calendar_id": "CALENDAR_ID",
129+
"subject": "Let's go for lunch",
130+
"content": "Does noon work for you?",
131+
"content_type": "text",
132+
"start_datetime": start_datetime,
133+
"start_timezone": timezone,
134+
"end_datetime": end_datetime,
135+
"end_timezone": timezone,
136+
"location": "Harry's Bar",
137+
}
138+
response = client.calendar.create_event(**data)
114139
```
115140

116141
#### Get calendars
@@ -136,12 +161,21 @@ response = client.contacts.list_contacts()
136161

137162
#### Create contact
138163
```
139-
response = client.contacts.create_contact()
140-
```
141-
142-
#### Create contact in specific folder
143-
```
144-
response = client.contacts.create_contact_in_folder(folder_id)
164+
data = {
165+
"given_name": "Pavel",
166+
"surname": "Bansky",
167+
"email_addresses": [
168+
{
169+
"address": "[email protected]",
170+
"name": "Pavel Bansky"
171+
}
172+
],
173+
"business_phones": [
174+
"+1 732 555 0102"
175+
],
176+
"folder_id": None,
177+
}
178+
response = client.contacts.create_contact(**data)
145179
```
146180

147181
#### Get contact folders

microsoftgraph/calendar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def create_event(
101101
},
102102
"location": {"displayName": location},
103103
}
104+
body.update(kwargs)
104105
url = "me/calendars/{}/events".format(calendar_id) if calendar_id is not None else "me/events"
105106
return self._client._post(self._client.base_url + url, json=body)
106107

microsoftgraph/contacts.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from microsoftgraph.decorators import token_required
23
from microsoftgraph.response import Response
34

@@ -26,8 +27,7 @@ def get_contact(self, contact_id: str, params: dict = None) -> Response:
2627
Returns:
2728
Response: Microsoft Graph Response.
2829
"""
29-
url = "{0}me/contacts/{1}".format(self._client.base_url, contact_id)
30-
return self._client._get(url, params=params)
30+
return self._client._get(self._client.base_url + "me/contacts/{}".format(contact_id), params=params)
3131

3232
@token_required
3333
def list_contacts(self, params: dict = None) -> Response:
@@ -41,35 +41,47 @@ def list_contacts(self, params: dict = None) -> Response:
4141
Returns:
4242
Response: Microsoft Graph Response.
4343
"""
44-
url = "{0}me/contacts".format(self._client.base_url)
45-
return self._client._get(url, params=params)
44+
return self._client._get(self._client.base_url + "me/contacts", params=params)
4645

4746
@token_required
48-
def create_contact(self, **kwargs) -> Response:
49-
"""Add a contact to the root Contacts folder.
50-
51-
https://docs.microsoft.com/en-us/graph/api/user-post-contacts?view=graph-rest-1.0&tabs=http
52-
53-
Returns:
54-
Response: Microsoft Graph Response.
55-
"""
56-
url = "{0}me/contacts".format(self._client.base_url)
57-
return self._client._post(url, **kwargs)
58-
59-
@token_required
60-
def create_contact_in_folder(self, folder_id: str, **kwargs) -> Response:
61-
"""Add a contact to another contact folder.
47+
def create_contact(
48+
self,
49+
given_name: str,
50+
surname: str,
51+
email_addresses: list,
52+
business_phones: list,
53+
folder_id: str = None,
54+
**kwargs
55+
) -> Response:
56+
"""Add a contact to the root Contacts folder or to the contacts endpoint of another contact folder.
6257
6358
https://docs.microsoft.com/en-us/graph/api/user-post-contacts?view=graph-rest-1.0&tabs=http
6459
6560
Args:
66-
folder_id (str): Unique identifier of the contact folder.
61+
given_name (str): The contact's given name.
62+
surname (str): The contact's surname.
63+
email_addresses (list): The contact's email addresses.
64+
business_phones (list): The contact's business phone numbers.
65+
folder_id (str, optional): Unique identifier of the contact folder. Defaults to None.
6766
6867
Returns:
6968
Response: Microsoft Graph Response.
7069
"""
71-
url = "{0}me/contactFolders/{1}/contacts".format(self._client.base_url, folder_id)
72-
return self._client._post(url, **kwargs)
70+
if isinstance(email_addresses, str):
71+
email_addresses = [{"address": email_addresses, "name": "{} {}".format(given_name, surname)}]
72+
73+
if isinstance(business_phones, str):
74+
business_phones = [business_phones]
75+
76+
body = {
77+
"givenName": given_name,
78+
"surname": surname,
79+
"emailAddresses": email_addresses,
80+
"businessPhones": business_phones,
81+
}
82+
body.update(kwargs)
83+
url = "me/contactFolders/{}/contacts".format(folder_id) if folder_id else "me/contacts"
84+
return self._client._post(self._client.base_url + url, json=body)
7385

7486
@token_required
7587
def list_contact_folders(self, params: dict = None) -> Response:
@@ -83,8 +95,7 @@ def list_contact_folders(self, params: dict = None) -> Response:
8395
Returns:
8496
Response: Microsoft Graph Response.
8597
"""
86-
url = "{0}me/contactFolders".format(self._client.base_url)
87-
return self._client._get(url, params=params)
98+
return self._client._get(self._client.base_url + "me/contactFolders", params=params)
8899

89100
@token_required
90101
def create_contact_folder(self, **kwargs) -> Response:
@@ -95,5 +106,4 @@ def create_contact_folder(self, **kwargs) -> Response:
95106
Returns:
96107
Response: Microsoft Graph Response.
97108
"""
98-
url = "{0}me/contactFolders".format(self._client.base_url)
99-
return self._client._post(url, **kwargs)
109+
return self._client._post(self._client.base_url + "me/contactFolders", **kwargs)

microsoftgraph/mail.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def send_mail(
4141
content_type: str = "HTML",
4242
attachments: list = None,
4343
save_to_sent_items: bool = True,
44+
**kwargs,
4445
) -> Response:
4546
"""Send the message specified in the request body using either JSON or MIME format.
4647
@@ -59,11 +60,21 @@ def send_mail(
5960
Response: Microsoft Graph Response.
6061
"""
6162
# Create recipient list in required format.
62-
to_recipient_list = [{"EmailAddress": {"Address": address}} for address in to_recipients]
63-
64-
cc_recipient_list = []
65-
if cc_recipients:
66-
cc_recipient_list = [{"EmailAddress": {"Address": address}} for address in cc_recipients]
63+
if isinstance(to_recipients, list):
64+
if all([isinstance(e, str) for e in to_recipients]):
65+
to_recipients = [{"EmailAddress": {"Address": address}} for address in to_recipients]
66+
elif isinstance(to_recipients, str):
67+
to_recipients = [{"EmailAddress": {"Address": to_recipients}}]
68+
else:
69+
raise Exception("to_recipients value is invalid.")
70+
71+
if cc_recipients and isinstance(cc_recipients, list):
72+
if all([isinstance(e, str) for e in cc_recipients]):
73+
cc_recipients = [{"EmailAddress": {"Address": address}} for address in cc_recipients]
74+
elif cc_recipients and isinstance(cc_recipients, str):
75+
cc_recipients = [{"EmailAddress": {"Address": cc_recipients}}]
76+
else:
77+
cc_recipients = []
6778

6879
# Create list of attachments in required format.
6980
attached_files = []
@@ -86,12 +97,13 @@ def send_mail(
8697
"Message": {
8798
"Subject": subject,
8899
"Body": {"ContentType": content_type, "Content": content},
89-
"ToRecipients": to_recipient_list,
90-
"ccRecipients": cc_recipient_list,
100+
"ToRecipients": to_recipients,
101+
"ccRecipients": cc_recipients,
91102
"Attachments": attached_files,
92103
},
93104
"SaveToSentItems": save_to_sent_items,
94105
}
106+
email_msg.update(kwargs)
95107

96108
# Do a POST to Graph's sendMail API and return the response.
97109
return self._client._post(self._client.base_url + "me/sendMail", json=email_msg)

microsoftgraph/webhooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def renew_subscription(self, subscription_id: str, expiration_datetime: datetime
6666
Response: Microsoft Graph Response.
6767
"""
6868
if isinstance(expiration_datetime, datetime):
69-
expiration_datetime = expiration_datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")
69+
expiration_datetime = format_time(expiration_datetime, is_webhook=True)
7070

7171
data = {"expirationDateTime": expiration_datetime}
7272
return self._client._patch(self._client.base_url + "subscriptions/{}".format(subscription_id), json=data)

0 commit comments

Comments
 (0)