|
| 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() |
0 commit comments