microsoftgraph / microsoftgraph/msgraph-sdk-java

Improve paging functionality

未關閉
#2,095 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type:feature
主要語言
Java
星號
444
分支
154
平均合併
18 小時 28 分鐘
30 天內合併 PR
4

描述

Is your feature request related to a problem? Please describe the problem.

In my opinion, the code required for getting all paged results of a collection request is not ideal. In v5 I had a generic method that handled it for all types, but I couldn't achieve the same using v6. Mainly because the classes for the original request use a RequestConfiguration object and the classes for the paginated requests use a RequestInformation object and they seem to be wholly incompatible.

This is the best I could come up with:

public List<User> getAllUsers(List<String> selectAttributes, String filter)
{
    UserCollectionResponse userCollectionResponse = graphClient.users().get(
            requestConfig ->
            {
                if (!selectAttributes.isEmpty())
                {
                    requestConfig.queryParameters.select = selectAttributes.toArray(new String[0]);
                    if (selectAttributes.contains("manager"))
                        requestConfig.queryParameters.expand = new String[]{"manager"};
                }

                if (filter != null)
                    requestConfig.queryParameters.filter = filter;
            });

    UnaryOperator<RequestInformation> requestInformation =
            requestInfo ->
            {
                if (!selectAttributes.isEmpty())
                {
                    requestInfo.addQueryParameter("%24select", selectAttributes.toArray(new String[0]));
                    if (selectAttributes.contains("manager"))
                        requestInfo.addQueryParameter("%24expand", new String[]{"manager"});
                }

                if (filter != null)
                    requestInfo.addQueryParameter("%24filter", filter);

                return requestInfo;
            };

    return loadPagedEntities(userCollectionResponse, UserCollectionResponse::createFromDiscriminatorValue, requestInformation);
}

private <T extends Parsable> List<T> loadPagedEntities(
        BaseCollectionPaginationCountResponse baseCollectionPaginationCountResponse,
        Function<ParseNode, BaseCollectionPaginationCountResponse> collectionPageFactoryFunction,
        UnaryOperator<RequestInformation> requestInformation)
{
    try
    {
        List<T> entities = new ArrayList<>();

        PageIterator<T, BaseCollectionPaginationCountResponse> pageIterator =
                new PageIterator.Builder<T, BaseCollectionPaginationCountResponse>()
                        .client(graphClient)
                        .collectionPage(Objects.requireNonNull(baseCollectionPaginationCountResponse))
                        .collectionPageFactory(collectionPageFactoryFunction::apply)
                        .requestConfigurator(requestInformation)
                        .processPageItemCallback(entities::add)
                        .build();

        pageIterator.iterate();
        return entities;
    }
    catch (ReflectiveOperationException e)
    {
        throw new RuntimeException(e);
    }
}

What irks me most about this, is that I have to specify the query parameters twice and using different syntax! Especially the "%24..." is pretty ugly.

Describe the solution you'd like.

The best solution, of course, would be if the SDK handled this transparently:

List<User> allUsers = graphClient.users().getAll(requestConfig -> ...);

The next best solution would be some kind of compatibility between RequestConfiguration and RequestInformation, so the query parameters would only have to specified once.

What do you think?

Additional context?

No response

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先檢查報告中提到的 Java SDK APIs:RequestConfiguration、RequestInformation 和 PageIterator。重現範例分頁流程,並比較首次請求與後續請求的查詢參數設定方式。當呼叫端能夠設定分頁查詢參數,而不必重複這些參數或使用 "%24select" 和 "%24filter" 這類編碼名稱時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
java
領域
api
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。