|
| 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 | +from azure.identity import InteractiveBrowserCredential |
| 11 | + |
| 12 | +from msgraph.core import GraphClient, HTTPClientFactory |
| 13 | + |
| 14 | +scopes = ['user.read'] |
| 15 | +# This sample uses InteractiveBrowserCredential only for demonstration. |
| 16 | +# Any azure-identity TokenCredential class will work the same. |
| 17 | +browser_credential = InteractiveBrowserCredential(client_id='YOUR_CLIENT_ID') |
| 18 | + |
| 19 | + |
| 20 | +def sample_http_client_with_custom_retry_defaults(): |
| 21 | + """ |
| 22 | + Initializing a sample client with default middleware using the HTTPClientand passing |
| 23 | + default configs to the retryhandler. These defaults will be used for every subsequent |
| 24 | + request using the client.""" |
| 25 | + |
| 26 | + client = HTTPClientFactory().create_with_default_middleware( |
| 27 | + browser_credential, retry_total=5, retry_backoff_factor=0.1, retry_time_limit=60 |
| 28 | + ) |
| 29 | + result = client.get('/me/messages', scopes=['mail.read']) |
| 30 | + pprint(result.json()) |
| 31 | + |
| 32 | + |
| 33 | +def sample_graph_client_with_custom_retry_defaults(): |
| 34 | + """Initializing a sample graph client and passing default configs to the default retry |
| 35 | + handler. These defaults will be used for every subsequent request using the client unless |
| 36 | + per request options are passed""" |
| 37 | + |
| 38 | + client = GraphClient(credential=browser_credential, retry_total=2, retry_backoff_factor=0.5) |
| 39 | + result = client.get('/me/messages', scopes=['mail.read']) |
| 40 | + pprint(result.json()) |
| 41 | + |
| 42 | + |
| 43 | +def sample_graph_client_with_per_request_retry_options(): |
| 44 | + """Sending a request using the graph client with retry options for that specific request. |
| 45 | + This will override the default config for the retry handler""" |
| 46 | + |
| 47 | + client = GraphClient(credential=browser_credential) |
| 48 | + result = client.get( |
| 49 | + '/me/messages', scopes=['mail.read'], retry_on_status_codes=[429, 502, 503, 504] |
| 50 | + ) |
| 51 | + pprint(result.json()) |
0 commit comments