原创

RetryHandler / RetryHandlerOption 与 msgraph-sdk-java v6.x

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I am currently working on a task to upgrade our application from msgraph-sdk-java 5.80.0 to 6.5.1. We have in this application one Graph class to provide all needed functions, like getting applications, service principals, settings owners, etc.

For our use case, we need a RetryHandler if the return code of a request is 404. We achieved this with version 5.x by implementing a RetryHandler and configuring it with the graph client instantiation.

With the new version 6.x, the approach is to configure this per request by passing a RetryHandlerOption to the default RetryHandler Middleware. This leads me to the first question:

Is it indeed not an option anymore, to configure a RetryHandler "globally" at client instantiation? In our Graph class, we have tons of requests - and it seems quite a pain to add the RetryHandlerOption to every one of these. I looked at the default RetryHandler Middleware and thought about taking the same approach as before by implementing our own RetryHandler. But there are package-private classes in use and simply copying from the default one, seems not to be possible.

And now to my second question: When I configure a RetryHandlerOption I get a java.lang.UnsupportedOperationException. I tried to begin with a minimal setup:

The client class (client initialisation & test function):


    private final val clientSecretCredential: ClientSecretCredential? =
        ClientSecretCredentialBuilder()
            .clientId(azureConfig.graphApiClientId)
            .clientSecret(azureConfig.graphApiClientSecret)
            .tenantId(azureConfig.tenantId)
            .build()

    private val authProvider = AzureIdentityAuthenticationProvider(clientSecretCredential, azureConfig.allowedHosts, azureConfig.scopeGraphApi)
    private final val msGraphApiDebugger = MsGraphApiDebugger()
    private final val retryHandler = RetryHandler()

    private final val httpClient = GraphClientFactory.create()
        .addInterceptor(msGraphApiDebugger)
        .addInterceptor(retryHandler)
        .build()

    fun getApplicationByObjectId(objectId: String): Application {
        val retryHandlerOption = RetryHandlerOption()

        return graphServiceClient.applications().byApplicationId(oid).get { requestConfiguration ->
            requestConfiguration.options.add(retryHandlerOption)
        }
    }

The unit test:

    @Test
    fun `check that getting an existing app registration returns the app registration`() {
        val application = graphApiClient.getApplicationByObjectId("ad86b88a-bd06-4299-a587-1da6c3d0abdd")
        assertEquals("Example App Registration Display Name", application.displayName)
    }

The exception:

java.lang.UnsupportedOperationException
    at java.base/java.util.AbstractList.add(AbstractList.java:155)
    at java.base/java.util.AbstractList.add(AbstractList.java:113)
    at xx.xxx.xxx.client.MsGraphApiClient.getApplicationByObjectId$lambda$0(MsGraphApiClient.kt:60)
    at com.microsoft.kiota.RequestInformation.configure(RequestInformation.java:81)
    at com.microsoft.graph.applications.item.ApplicationItemRequestBuilder.toGetRequestInformation(ApplicationItemRequestBuilder.java:335)
    at com.microsoft.graph.applications.item.ApplicationItemRequestBuilder.get(ApplicationItemRequestBuilder.java:267)
    at xx.xxx.xxx.client.MsGraphApiClient.getApplicationByObjectId(MsGraphApiClient.kt:59)
    at xx.xxx.xxx.client.MsGraphApiClientIT.check that getting an existing app registration returns the app registration(MsGraphApiClientIT.kt:16)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Do you have an idea what I am doing wrong?

Regards

正文到此结束
热门推荐
本文目录