@@ -50,8 +50,7 @@ def authorization_url(self, redirect_uri, scope, state=None):
5050 if state :
5151 params ['state' ] = None
5252
53- url = self .AUTHORITY_URL + self .account_type + self .AUTH_ENDPOINT + urlencode (params )
54- return url
53+ return self .AUTHORITY_URL + self .account_type + self .AUTH_ENDPOINT + urlencode (params )
5554
5655 def exchange_code (self , redirect_uri , code ):
5756 """Exchanges a code for a Token.
@@ -66,14 +65,14 @@ def exchange_code(self, redirect_uri, code):
6665 A dict.
6766
6867 """
69- params = {
68+ data = {
7069 'client_id' : self .client_id ,
7170 'redirect_uri' : redirect_uri ,
7271 'client_secret' : self .client_secret ,
7372 'code' : code ,
7473 'grant_type' : 'authorization_code' ,
7574 }
76- return requests .post (self .AUTHORITY_URL + self .account_type + self .TOKEN_ENDPOINT , data = params ).json ()
75+ return requests .post (self .AUTHORITY_URL + self .account_type + self .TOKEN_ENDPOINT , data = data ).json ()
7776
7877 def refresh_token (self , redirect_uri , refresh_token ):
7978 """
@@ -89,14 +88,14 @@ def refresh_token(self, redirect_uri, refresh_token):
8988 Returns:
9089
9190 """
92- params = {
91+ data = {
9392 'client_id' : self .client_id ,
9493 'redirect_uri' : redirect_uri ,
9594 'client_secret' : self .client_secret ,
9695 'refresh_token' : refresh_token ,
9796 'grant_type' : 'refresh_token' ,
9897 }
99- return requests .post (self .AUTHORITY_URL + self .account_type + self .TOKEN_ENDPOINT , data = params ).json ()
98+ return requests .post (self .AUTHORITY_URL + self .account_type + self .TOKEN_ENDPOINT , data = data ).json ()
10099
101100 def set_token (self , token ):
102101 """Sets the Token for its use in this library.
@@ -121,7 +120,56 @@ def get_me(self, params=None):
121120
122121
123122 """
124- return self ._get ('me' , params = None )
123+ return self ._get (self .base_url + 'me' , params = params )
124+
125+ def create_subscription (self , change_type , notification_url , resource , expiration_datetime , client_state = None ):
126+ """Creating a subscription is the first step to start receiving notifications for a resource.
127+
128+ Args:
129+ change_type: The event type that caused the notification. For example, created on mail receive, or updated on marking a message read.
130+ notification_url:
131+ resource: The URI of the resource relative to https://graph.microsoft.com.
132+ expiration_datetime: The expiration time for the subscription.
133+ client_state: The clientState property specified in the subscription request.
134+
135+ Returns:
136+
137+ """
138+ data = {
139+ 'changeType' : change_type ,
140+ 'notificationUrl' : notification_url ,
141+ 'resource' : resource ,
142+ 'expirationDateTime' : expiration_datetime ,
143+ 'clientState' : client_state
144+ }
145+ return self ._post ('https://graph.microsoft.com/beta/' + 'subscriptions' , json = data )
146+
147+ def renew_subscription (self , subscription_id , expiration_datetime ):
148+ """The client can renew a subscription with a specific expiration date of up to three days from the time
149+ of request. The expirationDateTime property is required.
150+
151+
152+ Args:
153+ subscription_id:
154+
155+ Returns:
156+
157+ """
158+ data = {
159+ 'expirationDateTime' : expiration_datetime
160+ }
161+ return self ._patch ('https://graph.microsoft.com/beta/' + 'subscriptions/{}' .format (subscription_id ), json = data )
162+
163+ def delete_subscription (self , subscription_id ):
164+ """The client can stop receiving notifications by deleting the subscription using its ID.
165+
166+ Args:
167+ subscription_id:
168+
169+ Returns:
170+
171+ """
172+ return self ._delete ('https://graph.microsoft.com/beta/' + 'subscriptions/{}' .format (subscription_id ))
125173
126174 def send_mail (self , subject = None , recipients = None , body = '' , content_type = 'HTML' , attachments = None ):
127175 """Helper to send email from current user.
@@ -163,22 +211,32 @@ def send_mail(self, subject=None, recipients=None, body='', content_type='HTML',
163211 'SaveToSentItems' : 'true' }
164212
165213 # Do a POST to Graph's sendMail API and return the response.
166- return self ._post ('me/microsoft.graph.sendMail' , data = email_msg )
214+ return self ._post (self . base_url + 'me/microsoft.graph.sendMail' , json = email_msg )
167215
168- def _get_headers (self ):
169- return { 'Authorization' : 'Bearer ' + self .token [ 'access_token' ]}
216+ def _get (self , url , ** kwargs ):
217+ return self ._request ( 'GET' , url , ** kwargs )
170218
171- def _get (self , endpoint , params = None ):
172- response = requests .get (self .base_url + endpoint , params = params , headers = self ._get_headers ())
173- return self ._parse (response )
219+ def _post (self , url , ** kwargs ):
220+ return self ._request ('POST' , url , ** kwargs )
174221
175- def _post (self , endpoint , params = None , data = None ):
176- response = requests .post (self .base_url + endpoint , params = params , json = data , headers = self ._get_headers ())
177- return self ._parse (response )
222+ def _put (self , url , ** kwargs ):
223+ return self ._request ('PUT' , url , ** kwargs )
178224
179- def _delete (self , endpoint , params = None ):
180- response = requests .delete (self .base_url + endpoint , params = params )
181- return self ._parse (response )
225+ def _patch (self , url , ** kwargs ):
226+ return self ._request ('PATCH' , url , ** kwargs )
227+
228+ def _delete (self , url , ** kwargs ):
229+ return self ._request ('DELETE' , url , ** kwargs )
230+
231+ def _request (self , method , url , headers = None , ** kwargs ):
232+ _headers = {
233+ 'Authorization' : 'Bearer ' + self .token ['access_token' ],
234+ 'Accept' : 'application/json' ,
235+ 'Content-Type' : 'application/json'
236+ }
237+ if headers :
238+ _headers .update (headers )
239+ return self ._parse (requests .request (method , url , headers = _headers , ** kwargs ))
182240
183241 def _parse (self , response ):
184242 if 'application/json' in response .headers ['Content-Type' ]:
0 commit comments