Search code examples
azure-active-directorymicrosoft-graph-apimicrosoft-graph-sdks

How to retry requests using MS JAVA SDK for GRAPH API?


I am a beginner using Graph API and currently trying to handle throttling related errors in my code. In the sdk's github page there is a RetryHandler class. But i can't seem to find an implementation of the code for a sample request. Is there any examples which i can inspect and try?

As an example i can give my own code below.

@Override
public JsonObject getUserList(String accessToken){
    LOGGER.trace("Querying directRoutingEnabled users");
    final String selectQuery = "mail,givenName,surname,displayName";
    final List<Option> requestOptions = new LinkedList<>();
    final String filterQuery = createFilterQuery();

    requestOptions.add(new HeaderOption(CONSISTENCY_LEVEL, EVENTUAL));
    requestOptions.add(new QueryOption(SELECT, selectQuery));
    requestOptions.add(new QueryOption(COUNT, true));
    requestOptions.add(new QueryOption(FILTER, filterQuery));

    IGraphServiceClient graphServiceClient = GraphServiceClient.builder().authenticationProvider(new SimpleAuthProvider(accessToken)).buildClient();
    IUserCollectionRequest request = graphServiceClient
            .users()
            .buildRequest(requestOptions);
    IUserCollectionPage users = request.get();
    return users.getRawObject();
}

How can i cover this code with retry-after mechanism (and maybe also Error Handling) using Graph SDK for Java?.


Solution

  • The default middleware from the core library comes with the default RetryHandler implementation. And thanks to a recent fix, it works with throttling errors as well.

    Starting from V3 of the SDK, you can specify custom OkHttpClient with custom interceptors. Customization guidelines can be found here: https://learn.microsoft.com/en-us/graph/sdks/customize-client?tabs=java.

    Default RetryHandler implementation: https://github.com/microsoftgraph/msgraph-sdk-java-core/blob/dev/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java

    Default RetryHandler design: https://microsoftgraph.github.io/msgraph-sdk-design/middleware/RetryHandler.html