Skip to content

Commit 01025bd

Browse files
committed
Update samples
1 parent 0414a3b commit 01025bd

File tree

3 files changed

+218
-49
lines changed

3 files changed

+218
-49
lines changed

samples/client_factory_samples.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
#pylint: disable=undefined-variable
6+
"""
7+
Demonstrates using the HTTPClientFactory to create a client and make HTTP requests
8+
to Microsoft Graph
9+
"""
10+
import json
11+
from pprint import pprint
12+
13+
# This sample uses InteractiveBrowserCredential only for demonstration.
14+
# Any azure-identity TokenCredential class will work the same.
15+
from azure.identity import InteractiveBrowserCredential
16+
from requests import Session
17+
18+
from msgraph.core import APIVersion, HTTPClientFactory, NationalClouds
19+
20+
scopes = ['user.read']
21+
browser_credential = InteractiveBrowserCredential(client_id='YOUR_CLIENT_ID')
22+
23+
# Create client with default middleware
24+
client = HTTPClientFactory().create_with_default_middleware(browser_credential)
25+
26+
27+
def get_sample():
28+
"""Sample HTTP GET request using the client"""
29+
result = client.get(
30+
'/users',
31+
params={
32+
'$select': 'displayName',
33+
'$top': '10'
34+
},
35+
)
36+
pprint(result.json())
37+
38+
39+
def post_sample():
40+
"""Sample HTTP POST request using the client"""
41+
body = {
42+
'message': {
43+
'subject': 'Python SDK Meet for lunch?',
44+
'body': {
45+
'contentType': 'Text',
46+
'content': 'The new cafeteria is open.'
47+
},
48+
'toRecipients': [{
49+
'emailAddress': {
50+
'address': 'ENTER_RECEPIENT_EMAIL_ADDRESS'
51+
}
52+
}]
53+
}
54+
}
55+
56+
result = client \
57+
.post('/me/sendMail',
58+
data=json.dumps(body),
59+
scopes=['mail.send'],
60+
headers={'Content-Type': 'application/json'}
61+
)
62+
pprint(result.status_code)
63+
64+
65+
def client_with_custom_session_sample():
66+
"""Sample client with a custom Session object"""
67+
session = Session()
68+
my_client = HTTPClientFactory(session=session
69+
).create_with_default_middleware(browser_credential)
70+
result = my_client.get('/me')
71+
pprint(result.json())
72+
73+
74+
def client_with_custom_settings_sample():
75+
"""Sample client that makes requests against the beta api on a specified cloud endpoint"""
76+
my_client = HTTPClientFactory(
77+
credential=browser_credential,
78+
api_version=APIVersion.beta,
79+
cloud=NationalClouds.Germany,
80+
).create_with_default_middleware(browser_credential)
81+
result = my_client.get(
82+
'/users',
83+
params={
84+
'$select': 'displayName',
85+
'$top': '10'
86+
},
87+
)
88+
pprint(result.json())
89+
90+
91+
def client_with_custom_middleware():
92+
"""Sample client with a custom middleware chain"""
93+
middleware = [
94+
CustomAuthorizationHandler(),
95+
MyCustomMiddleware(),
96+
]
97+
98+
my_client = HTTPClientFactory().create_with_custom_middleware(middleware)
99+
result = my_client.get(
100+
'https://graph.microsoft.com/v1.0/users',
101+
params={
102+
'$select': 'displayName',
103+
'$top': '10'
104+
},
105+
)
106+
pprint(result.json())
107+
108+
109+
if __name__ == '__main__':
110+
get_sample()
111+
post_sample()
112+
client_with_custom_session_sample()
113+
client_with_custom_settings_sample()
114+
client_with_custom_middleware()

samples/graph_client_samples.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
#pylint: disable=undefined-variable
6+
"""Demonstrates using the GraphClient to make HTTP Requests to Microsoft Graph"""
7+
import json
8+
from pprint import pprint
9+
10+
# This sample uses InteractiveBrowserCredential only for demonstration.
11+
# Any azure-identity TokenCredential class will work the same.
12+
from azure.identity import InteractiveBrowserCredential
13+
from requests import Session
14+
15+
from msgraph.core import APIVersion, GraphClient, NationalClouds
16+
17+
scopes = ['user.read']
18+
browser_credential = InteractiveBrowserCredential(client_id='YOUR_CLIENT_ID')
19+
client = GraphClient(credential=browser_credential)
20+
21+
22+
def get_sample():
23+
"""Sample HTTP GET request using the GraphClient"""
24+
result = client.get('/me/messages', scopes=['mail.read'])
25+
pprint(result.json())
26+
27+
28+
def post_sample():
29+
"""Sample HTTP POST request using the GraphClient"""
30+
body = {
31+
'message': {
32+
'subject': 'Python SDK Meet for lunch?',
33+
'body': {
34+
'contentType': 'Text',
35+
'content': 'The new cafeteria is open.'
36+
},
37+
'toRecipients': [{
38+
'emailAddress': {
39+
'address': 'ENTER_RECEPIENT_EMAIL_ADDRESS'
40+
}
41+
}]
42+
}
43+
}
44+
45+
result = client \
46+
.post('/me/sendMail',
47+
data=json.dumps(body),
48+
scopes=['mail.send'],
49+
headers={'Content-Type': 'application/json'}
50+
)
51+
pprint(result.status_code)
52+
53+
54+
def client_with_custom_session_sample():
55+
"""Sample client with a custom Session object"""
56+
session = Session()
57+
my_client = GraphClient(credential=browser_credential, session=session)
58+
result = my_client.get('/me')
59+
pprint(result.json())
60+
61+
62+
def client_with_custom_settings_sample():
63+
"""
64+
Sample client that makes requests against the beta api on a specified cloud endpoint
65+
"""
66+
my_client = GraphClient(
67+
credential=browser_credential,
68+
api_version=APIVersion.beta,
69+
cloud=NationalClouds.Germany,
70+
)
71+
result = my_client.get(
72+
'/users',
73+
params={
74+
'$select': 'displayName',
75+
'$top': '10'
76+
},
77+
)
78+
pprint(result.json())
79+
80+
81+
def client_with_custom_middleware():
82+
"""Sample client with a custom middleware chain"""
83+
middleware = [
84+
CustomAuthorizationHandler(),
85+
MyCustomMiddleware(),
86+
]
87+
88+
my_client = GraphClient(credential=browser_credential, middleware=middleware)
89+
result = my_client.get(
90+
'https://graph.microsoft.com/v1.0/users',
91+
params={
92+
'$select': 'displayName',
93+
'$top': '10'
94+
},
95+
)
96+
pprint(result.json())
97+
98+
99+
if __name__ == '__main__':
100+
post_sample()
101+
get_sample()
102+
client_with_custom_session_sample()
103+
client_with_custom_settings_sample()
104+
client_with_custom_middleware()

samples/samples.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)