Blog Post

Educator Developer Blog
3 MIN READ

Microsoft Graph Python SDK - Public Preview

Lee_Stott's avatar
Lee_Stott
Icon for Microsoft rankMicrosoft
Nov 29, 2022

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.

 

 


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.

Updated Nov 29, 2022
Version 6.0
  • DanielNiccoli's avatar
    DanielNiccoli
    Steel Contributor

    This SDK is a complete waste of everybody's time. It's auto-generated, and no function has any meaning to it. You did not invest any time to create a meaningful documentation, let along any documentation. And as you'd think it could not get any worse, the generated stuff is overly convoluted. You can't even pass a filter down to the request without adding an unhealthy amount of lines of code.

     

    At this point, I'm faster writing my own SDK, than using whatever aberration you created.

     

    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.

     

    You don't fool anybody with that marketing nonsense. Microsoft just doesn't want to invest any time and sells this crap and a new gold standard. That's embarrassing.