Skip to content

Commit 878d115

Browse files
committed
Fix datetime parse
1 parent 7695a50 commit 878d115

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

microsoftgraph/calendar.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from datetime import datetime
2+
13
from microsoftgraph.decorators import token_required
24
from microsoftgraph.response import Response
3-
from datetime import datetime
5+
from microsoftgraph.utils import format_time
46

57

68
class Calendar(object):
@@ -79,9 +81,9 @@ def create_event(
7981
Response: Microsoft Graph Response.
8082
"""
8183
if isinstance(start_datetime, datetime):
82-
start_datetime = start_datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")
84+
start_datetime = format_time(start_datetime)
8385
if isinstance(end_datetime, datetime):
84-
end_datetime = end_datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")
86+
end_datetime = format_time(end_datetime)
8587

8688
body = {
8789
"subject": subject,

microsoftgraph/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from datetime import datetime
2+
3+
4+
def format_time(value: datetime, is_webhook: bool = False) -> str:
5+
if is_webhook:
6+
return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
7+
return value.strftime("%Y-%m-%dT%H:%M:%S")

microsoftgraph/webhooks.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from datetime import datetime
2+
13
from microsoftgraph.decorators import token_required
24
from microsoftgraph.response import Response
5+
from microsoftgraph.utils import format_time
36

47

58
class Webhooks(object):
@@ -19,7 +22,7 @@ def create_subscription(
1922
change_type: str,
2023
notification_url: str,
2124
resource: str,
22-
expiration_datetime: str,
25+
expiration_datetime: datetime,
2326
client_state: str = None,
2427
) -> Response:
2528
"""Creates a subscription to start receiving notifications for a resource.
@@ -31,12 +34,15 @@ def create_subscription(
3134
updated on marking a message read.
3235
notification_url (str): Url to receive notifications.
3336
resource (str): The URI of the resource relative to https://graph.microsoft.com.
34-
expiration_datetime (str): The expiration time for the subscription.
37+
expiration_datetime (datetime): The expiration time for the subscription.
3538
client_state (str, optional): The clientState property specified in the subscription request. Defaults to None.
3639
3740
Returns:
3841
Response: Microsoft Graph Response.
3942
"""
43+
if isinstance(expiration_datetime, datetime):
44+
expiration_datetime = format_time(expiration_datetime, is_webhook=True)
45+
4046
data = {
4147
"changeType": change_type,
4248
"notificationUrl": notification_url,
@@ -47,18 +53,21 @@ def create_subscription(
4753
return self._client._post(self._client.base_url + "subscriptions", json=data)
4854

4955
@token_required
50-
def renew_subscription(self, subscription_id: str, expiration_datetime: str) -> Response:
56+
def renew_subscription(self, subscription_id: str, expiration_datetime: datetime) -> Response:
5157
"""Renews a subscription to keep receiving notifications for a resource.
5258
5359
https://docs.microsoft.com/en-us/graph/webhooks#renewing-a-subscription
5460
5561
Args:
5662
subscription_id (str): Subscription ID.
57-
expiration_datetime (str): Expiration date.
63+
expiration_datetime (datetime): Expiration date.
5864
5965
Returns:
6066
Response: Microsoft Graph Response.
6167
"""
68+
if isinstance(expiration_datetime, datetime):
69+
expiration_datetime = expiration_datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")
70+
6271
data = {"expirationDateTime": expiration_datetime}
6372
return self._client._patch(self._client.base_url + "subscriptions/{}".format(subscription_id), json=data)
6473

0 commit comments

Comments
 (0)