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