Microsoft Graph Python SDK, now available for public preview. Get ready to adopt and utilize the Microsoft Python core library.
You can now access the beta and v1 endpoints of Microsoft Graph, with a fluent experience, designed to facilitate discoverability and enhance with best features of the Python language.
The Python SDK improves the best of the Python core library, supplying an authentication provider that automatically refreshes access tokens, along with a built-in retry handler that understands response status codes and automatically waits the recommended time. The SDK also provides async experience, allowing the application to work on multiple requests at the same time and perform other operations while it waits for the response, improving the speed and efficiency of the application. It is also equipped with HTTP2 support and model classes corresponding to Microsoft Graph resources, allowing the developer to work with Python objects, automatically serializing and deserializing JSON format in requests and responses.
Contrary to other fluent experiences in Python, we don’t map every endpoint and method to a unique function. Instead, we present a framework to construct any request using method chaining, providing a clean flow that works seamlessly with Python. The fluent pattern contributes to reducing errors by displaying only the methods corresponding to operations on that resource, aided by the IDE’s autocomplete, allowing the developer to confidently type code that runs. The SDK also makes use of type annotations to provide clarity and control during the development process.
Continuing our efforts to standardize our tools, this SDK is also generated with Kiota, an open-source project for generating an API client to call any OpenAPI described API, assuring quality and consistency across our tools.
The authentication provider handles the fetching, caching, and refreshing of tokens automatically, ensuring your requests are always authenticated. The auth provider verifies the token is always valid by tracking the expiration and automatically refreshing it in the background before it expires.
from azure.identity.aio import ClientSecretCredential
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
credential=ClientSecretCredential(tenant_id: str, client_id: str, client_secret: str)
scopes = ['User.Read', 'Mail.Read'];
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)
The fluent pattern makes the request building experience more intuitive. Instead of passing raw URLs, this new version provides a fluent experience that enhances discoverability and efficiency, also reducing the time spent in reference docs.
request.py
client = GraphServiceClient(request_adapter)
req = client.users_by_id('userPrincipalName').messages('messageId').get()
msg = asyncio.run(req)
print(msg.subject)
This request builder pattern was carefully designed with Python developers in mind, providing enhanced discoverability without mutating the objects.
A configurable built-in retry handler understands 429, 503 and 504 response status codes, reading the information in the `Retry-After` header and making decisions accordingly. For throttling status codes, the handler reads the recommendation in the `Retry-After` header and retries after the recommended time.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.