Microsoft Graph Python SDK – Public Preview

Posted by

This post has been republished via RSS; it originally appeared at: Microsoft Tech Community - Latest Blogs - .

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.

 

LeeStott_4-1669717995183.png

 


Get started with Microsoft Graph Python SDK

  • Start with the Microsoft Graph SDKs documentation page.
  • The README and code examples will teach you how to use the Python SDK. And the Upgrade guide contains instructions to upgrade from the Python Core library. You can also find the SDK to access the beta endpoint here.
  • Open an issue for errors you encounter that we didn’t include in our guide or simply to share feedback with us.
  • Sign up for the Microsoft 365 Developer Program to get a free Microsoft 365 developer subscription and start building with Microsoft Graph.


The Microsoft Graph Python SDK

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.

 

Fluent Experience in Python SDK

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.

Standardize API Client Output with Kiota

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.

 

Authentication provider

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)

Fluent request builder pattern for Python

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.

 

Built-in retry handler

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.