Skip to content
This repository was archived by the owner on Mar 7, 2020. It is now read-only.

Commit cd526c6

Browse files
author
Doug Mahugh
committed
add get(), post() methods
1 parent 7c74c81 commit cd526c6

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

graphrest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ def api_endpoint(self, url):
9999
f"{self.config['resource']}{self.config['api_version']}/",
100100
url.lstrip('/'))
101101

102+
def get(self, endpoint, headers=None, stream=False):
103+
"""GET from API (authenticated with access token)."""
104+
105+
self.token_validation()
106+
107+
# merge passed headers with default headers
108+
merged_headers = self.headers()
109+
if headers:
110+
merged_headers.update(headers)
111+
112+
return requests.get(self.api_endpoint(endpoint),
113+
headers=merged_headers,
114+
stream=stream)
115+
102116
def headers(self, headers=None):
103117
"""Returns dictionary of default HTTP headers for calls to Microsoft Graph API,
104118
including access token and a unique client-request-id.
@@ -153,6 +167,26 @@ def logout(self, redirect_to=None):
153167
if redirect_to:
154168
bottle.redirect(redirect_to)
155169

170+
def post(self, endpoint, headers=None, data=None, verify=False, params=None):
171+
"""POST to API (authenticated with access token).
172+
173+
headers = custom HTTP headers (merged with defaults, including access token)
174+
175+
verify = the Requests option for verifying SSL certificate; defaults
176+
to False for demo purposes. For more information see:
177+
http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification
178+
"""
179+
180+
self.token_validation()
181+
182+
merged_headers = self.headers()
183+
if headers:
184+
merged_headers.update(headers)
185+
186+
return requests.post(self.api_endpoint(endpoint),
187+
headers=merged_headers, data=data,
188+
verify=verify, params=params)
189+
156190
def redirect_uri_handler(self):
157191
"""Redirect URL handler for AuthCode workflow. Uses the authorization
158192
code received from auth endpoint to call the token endpoint and obtain

sample_graphrest.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import bottle
77
import graphrest
8-
import requests
98

109
MSGRAPH = graphrest.GraphSession()
1110

@@ -32,12 +31,9 @@ def authorized():
3231
@bottle.view('graphcall.html')
3332
def graphcall():
3433
"""Confirm user authentication by calling Graph and displaying some data."""
35-
MSGRAPH.token_validation() # Optional - assures token is valid for >5 seconds.
3634
endpoint = MSGRAPH.api_endpoint('me')
37-
graphdata = requests.get(endpoint, headers=MSGRAPH.headers()).json()
38-
return {'graphdata': graphdata,
39-
'endpoint': endpoint,
40-
'sample': 'graphrest'}
35+
graphdata = MSGRAPH.get(endpoint).json()
36+
return {'graphdata': graphdata, 'endpoint': endpoint, 'sample': 'graphrest'}
4137

4238
@bottle.route('/static/<filepath:path>')
4339
def server_static(filepath):

0 commit comments

Comments
 (0)