graph-rs-sdk
Rust SDK Client for Microsoft Graph and the Microsoft Graph Api
Available on crates.io - v1.1.4 - Latest Stable Version
= "1.1.4"
= { = "1.25.0", = ["full"] }
For using types that implement serde Serialize
as request bodies or passing serde's json macro:
= { = "1", = ["derive"] }
= "1"
To use stream features add the futures crate:
= "0.3"
And import futures::StreamExt
.
use StreamExt;
use *;
Pre Release Version (May Be Unstable)
- Complete rewrite of SDK Client for the Microsoft Identity Platform
- In Memory Token Cache
- Automatic Token Refresh
- Interactive Auth Using WebView
- X509 Certificate Support
See https://github.com/sreeise/graph-rs-sdk/tree/v2.0.0-beta.0 for examples and docs.
On Pre-Release Only:
- Identity Platform Auth Examples
- Url Builders For Flows Using Sign In To Get Authorization Code - Building Sign In Url
- Interactive Auth Examples (feature =
interactive-auth
) - Certificate Auth (feature =
openssl
)
graph-rs-sdk = "2.0.0-beta.0"
Contributing and Wiki:
Feature requests or Bug reports.
For bug reports please file an issue on GitHub and a response or fix will be given as soon as possible.
The Discussions tab on GitHub is enabled so feel free to stop by there with any questions or feature requests as well. For bugs, please file an issue first. Features can be requested through issues or discussions. Either way works. Other than that feel free to ask questions, provide tips to others, and talk about the project in general.
Table Of Contents
What APIs are available
The APIs available are generated from OpenApi configs that are stored in Microsoft's msgraph-metadata repository for the Graph Api. There may be some requests and/or APIs not yet included in this project that are in the OpenApi config but in general most of them are implemented.
Usage
For extensive examples see the examples directory on GitHub
Authentication and Authorization (In Active Development)
The crate is undergoing major development in order to support all or most scenarios in the Microsoft Identity Platform where its possible to do so. Another goal is to make the authentication/authorization impl much easier to use by providing easy to use clients that follow similar designs to other sdks for the Identity Platform.
This includes token caches, automatic refresh of tokens, interactive web view authentication, and much more. The development is well underway - as of right now no merge has gone into master but changes will be there soon and they will almost certainly be unstable in some respects while we continue to work on this. However, the crate on crates.io is currently only updated on stable version releases.
Async and Blocking Client
The crate can do both an async and blocking requests.
Async Client (default)
graph-rs-sdk = "1.1.4"
tokio = { version = "1.25.0", features = ["full"] }
Example
use *;
async
Blocking Client
To use the blocking client use the into_blocking()
method. You should not
use tokio
when using the blocking client.
graph-rs-sdk = "1.1.4"
Example
use graph_rs_sdk::*;
Cargo Feature Flags
native-tls
: Use thenative-tls
TLS backend (OpenSSL on *nix, SChannel on Windows, Secure Transport on macOS).rustls-tls
: Use therustls-tls
TLS backend (cross-platform backend, only supports TLS 1.2 and 1.3).brotli
: Enables reqwest feature brotli. For more info see the reqwest crate.deflate
: Enables reqwest feature deflate. For more info see the reqwest crate.trust-dns
: Enables reqwest feature trust-dns. For more info see the reqwest crate.test-util
: Enables testing features. Currently only enables setting https-only to false for use in mocking frameworks.
Default features: default=["native-tls"]
The send method
The send() method is the main method for sending a request and returns a Result<rewest::Response, GraphFailure>
. See the
reqwest crate for information on the Response type.
use *;
pub async
Response Errors/Error Types
While the crate does have its own error type and result you will probably want to use crates like anyhow due to the amount of possible errors that could occur.
If the Microsoft Graph API returned an error this is almost always in the body of the response.
The ResponseExt
for async requests and BlockingResponseExt
for blocking requests have convenience
methods that can be used to get the error message from the body of the response.
use ;
use Error;
async
Custom Types
You can pass your own types to API requests that require a request body by implementing serde::Serialize
.
You can implement your own types by utilizing methods from reqwest::Response. These types must implement serde::Deserialize
.
See the reqwest crate for more info.
extern crate serde;
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
static ITEM_ID: &str = "ITEM_ID";
pub async
Paging
Paging handles scenarios where the response body is a truncated version of the data and a URL is provided to continue calling and getting the rest of the data. Paging can consist of multiple links in the call chain.
The sdk provides convenience methods for getting all data in a paging scenario such as using next links or using delta links to track changes to Graph data.
If you just want a quick and easy way to get all next link responses or the JSON bodies you can use the paging().json()
method which will exhaust all
next link calls and return all the responses in a VecDeque<Response<Result<T>>>
. Keep in mind that the larger the volume of next link calls that need to be
made the longer the return delay will be when calling this method.
All paging methods have their response body read in order to get the @odata.nextLink
URL for calling next link requests. Because of this
the original reqwest::Response
is lost. However, the paging responses are re-wrapped in a Response object (http::Response
) that is
similar to reqwest::Response
.
See the Detailed Guide On Paging to learn more about using Paging calls in the SDK.
There are different levels of support for paging Microsoft Graph APIs. See the documentation, Paging Microsoft Graph data in your app, for more info on supported APIs and availability.
extern crate serde;
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
async
The paging example shows a simple way to list users and call all next links. You can also stream the next link responses or use a channel receiver to get the responses.
Streaming
Streaming is only available using the async client.
use StreamExt;
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
pub async
pub async
Channels
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
async
API Usage
The following shows a few examples of how to use the client and a few of the APIs.
OneDrive
Make requests to drive using a drive id or through specific drives for me, sites, users, and groups.
use *;
async
Me API
async
Users API
async
Sites API
async
Create a folder.
use *;
use HashMap;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
static FOLDER_NAME: &str = "NEW_FOLDER_NAME";
static PARENT_ID: &str = "PARENT_ID";
// For more info on creating a folder see:
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_post_children?view=odsp-graph-online
pub async
Path based addressing for drive.
// Pass the path location of the item staring from the OneDrive root folder.
// Start the path with :/ and end with :
async
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
async
Create message
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
static MAIL_FOLDER_ID: &str = "MAIL_FOLDER_ID";
async
Send mail
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
async
Mail folders
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
static MAIL_FOLDER_ID: &str = "MAIL_FOLDER_ID";
async
Get Inbox Messages
use *;
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
static USER_ID: &str = "USER_ID";
async
Use your own struct. Anything that implements serde::Serialize can be used for things like creating messages for mail or creating a folder for OneDrive.
extern crate serde;
use *;
async
OData Queries
use *;
async
Batch Requests
Batch requests use a mpsc::channel and return the receiver for responses.
use *;
static USER_ID: &str = "USER_ID";
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
async
Id vs Non-Id methods (such as user("user-id")
vs users()
)
Many of the available APIs have methods that do not require an id for a resource as well as many of the APIs have methods that do require an id. For most all of these resources the methods are implemented in this sdk by using two different naming schemes and letting the user go directly to the methods they want.
As en example, the users API can list users without an id, and you can find list_users()
by calling the users()
method whereas getting a specific user requires a users id
and you can find get_users()
by calling user<ID: AsRef<str>>(id: ID)
method.
Using the users()
method:
use *;
// For more info on users see: https://docs.microsoft.com/en-us/graph/api/resources/user?view=graph-rest-1.0
// For more examples see the examples directory on GitHub.
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
async
Using the user id user<ID: AsRef<str>>(id: ID)
method:
use *;
// For more info on users see: https://docs.microsoft.com/en-us/graph/api/resources/user?view=graph-rest-1.0
// For more examples see the examples directory on GitHub
static ACCESS_TOKEN: &str = "ACCESS_TOKEN";
static USER_ID: &str = "USER_ID";
async