Skip to content

Commit c60dbbe

Browse files
authored
Merge pull request #21 from GearPlug/rev-2021
New revision
2 parents b17f09a + f038047 commit c60dbbe

File tree

14 files changed

+1444
-663
lines changed

14 files changed

+1444
-663
lines changed

README.md

Lines changed: 155 additions & 85 deletions
Large diffs are not rendered by default.

microsoftgraph/calendar.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
from datetime import datetime
2+
3+
from microsoftgraph.decorators import token_required
4+
from microsoftgraph.response import Response
5+
from microsoftgraph.utils import format_time
6+
7+
8+
class Calendar(object):
9+
def __init__(self, client) -> None:
10+
"""Working with Outlook Calendar.
11+
12+
https://docs.microsoft.com/en-us/graph/api/resources/calendar?view=graph-rest-1.0
13+
14+
Args:
15+
client (Client): Library Client.
16+
"""
17+
self._client = client
18+
19+
@token_required
20+
def list_events(self, calendar_id: str = None, params: dict = None) -> Response:
21+
"""Get a list of event objects in the user's mailbox. The list contains single instance meetings and series
22+
masters.
23+
24+
https://docs.microsoft.com/en-us/graph/api/user-list-events?view=graph-rest-1.0&tabs=http
25+
26+
Args:
27+
calendar_id (str): Calendar ID.
28+
params (dict, optional): Query. Defaults to None.
29+
30+
Returns:
31+
Response: Microsoft Graph Response.
32+
"""
33+
url = "me/calendars/{}/events".format(calendar_id) if calendar_id else "me/events"
34+
return self._client._get(self._client.base_url + url, params=params)
35+
36+
@token_required
37+
def get_event(self, event_id: str, params: dict = None) -> Response:
38+
"""Get the properties and relationships of the specified event object.
39+
40+
https://docs.microsoft.com/en-us/graph/api/event-get?view=graph-rest-1.0&tabs=http
41+
42+
Args:
43+
event_id (str): Event ID.
44+
params (dict, optional): Query. Defaults to None.
45+
46+
Returns:
47+
Response: Microsoft Graph Response.
48+
"""
49+
return self._client._get(self._client.base_url + "me/events/{}".format(event_id), params=params)
50+
51+
@token_required
52+
def create_event(
53+
self,
54+
subject: str,
55+
content: str,
56+
start_datetime: datetime,
57+
start_timezone: str,
58+
end_datetime: datetime,
59+
end_timezone: str,
60+
location: str,
61+
calendar_id: str = None,
62+
content_type: str = "HTML",
63+
**kwargs,
64+
) -> Response:
65+
"""Create an event in the user's default calendar or specified calendar.
66+
67+
https://docs.microsoft.com/en-us/graph/api/user-post-events?view=graph-rest-1.0&tabs=http
68+
69+
Additional time zones: https://docs.microsoft.com/en-us/graph/api/resources/datetimetimezone?view=graph-rest-1.0
70+
71+
Args:
72+
subject (str): The text of the event's subject line.
73+
content (str): The body of the message associated with the event.
74+
start_datetime (datetime): A single point of time in a combined date and time representation ({date}T{time};
75+
start_timezone (str): Represents a time zone, for example, "Pacific Standard Time".
76+
end_datetime (datetime): A single point of time in a combined date and time representation ({date}T{time}; for
77+
end_timezone (str): Represents a time zone, for example, "Pacific Standard Time".
78+
location (str): The location of the event.
79+
calendar_id (str, optional): Calendar ID. Defaults to None.
80+
content_type (str, optional): It can be in HTML or text format. Defaults to HTML.
81+
82+
Returns:
83+
Response: Microsoft Graph Response.
84+
"""
85+
if isinstance(start_datetime, datetime):
86+
start_datetime = format_time(start_datetime)
87+
if isinstance(end_datetime, datetime):
88+
end_datetime = format_time(end_datetime)
89+
90+
body = {
91+
"subject": subject,
92+
"body": {
93+
"contentType": content_type,
94+
"content": content,
95+
},
96+
"start": {
97+
"dateTime": start_datetime,
98+
"timeZone": start_timezone,
99+
},
100+
"end": {
101+
"dateTime": end_datetime,
102+
"timeZone": end_timezone,
103+
},
104+
"location": {"displayName": location},
105+
}
106+
body.update(kwargs)
107+
url = "me/calendars/{}/events".format(calendar_id) if calendar_id is not None else "me/events"
108+
return self._client._post(self._client.base_url + url, json=body)
109+
110+
@token_required
111+
def list_calendars(self, params: dict = None) -> Response:
112+
"""Get all the user's calendars (/calendars navigation property), get the calendars from the default calendar
113+
group or from a specific calendar group.
114+
115+
https://docs.microsoft.com/en-us/graph/api/user-list-calendars?view=graph-rest-1.0&tabs=http
116+
117+
Args:
118+
params (dict, optional): Query. Defaults to None.
119+
120+
Returns:
121+
Response: Microsoft Graph Response.
122+
"""
123+
return self._client._get(self._client.base_url + "me/calendars", params=params)
124+
125+
@token_required
126+
def create_calendar(self, name: str) -> Response:
127+
"""Create a new calendar for a user.
128+
129+
https://docs.microsoft.com/en-us/graph/api/user-post-calendars?view=graph-rest-1.0&tabs=http
130+
131+
Args:
132+
name (str): The calendar name.
133+
134+
Returns:
135+
Response: Microsoft Graph Response.
136+
"""
137+
body = {"name": "{}".format(name)}
138+
return self._client._post(self._client.base_url + "me/calendars", json=body)

0 commit comments

Comments
 (0)