microsoftgraph / microsoftgraph/msgraph-sdk-java

Boiler plate when iterating through collections

未关闭
#1,965 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

type:enhancement
主要语言
Java
星标
444
派生
154
平均合并
18 小时 28 分钟
30 天内合并 PR
4

描述

Hello, i have a small question.

https://github.com/microsoftgraph/msgraph-sdk-java/blob/dev/docs/upgrade-to-v6.md#pageiterator

  • With the new page iterator there is a boilerplate associated with each request and iterating through it. Since a new page iterator would need to be created for each request.
  • I was wondering if there is a generic way to create an iterator to run on collection requests and delta collection requests.

So I created this code, from looking at the generated code in the msft graph sdk:

  public static <T extends Parsable> void genericIterator(
      GraphServiceClient graphClient,
      BaseDeltaFunctionResponse res,
      Consumer<T> processEntity, 
      Consumer<String> processDeltaLink) {
    PageIterator<T, BaseDeltaFunctionResponse> pageIterator;
    try {
      pageIterator = new PageIterator.Builder<T, BaseDeltaFunctionResponse>()
              .client(graphClient)
              .collectionPage(res)
              .collectionPageFactory(BaseDeltaFunctionResponse::createFromDiscriminatorValue)
              .processPageItemCallback(entity -> {
                processEntity.accept(entity);
                return true; 
              }).build();
      pageIterator.iterate();
      processDeltaLink.accept(pageIterator.getDeltaLink());
    } catch (ApiException | ReflectiveOperationException e) {
      log.error("Error ", e);
    }
  }

  public static <T extends Parsable> void genericIterator(
      GraphServiceClient graphClient,
      BaseCollectionPaginationCountResponse res,
      Consumer<T> processGroupOwner) {
    PageIterator<T, BaseCollectionPaginationCountResponse> pageIterator;
    try {
      pageIterator = new PageIterator.Builder<T, BaseCollectionPaginationCountResponse>()
              .client(graphClient)
              .collectionPage(res)
              .collectionPageFactory(BaseCollectionPaginationCountResponse::createFromDiscriminatorValue)
              .processPageItemCallback(entity -> {
                processGroupOwner.accept(entity);
                return true; 
              }).build();
      pageIterator.iterate();
    } catch (ApiException | ReflectiveOperationException e) {
      log.error("Error ", e);
    }
  }

This second method works with general collectors, but not the first one for delta collections.

I was wondering if this is even recommending considering that the sdk is based on code generations and/or if there is a way to iterate over collections in a more concise manner than the current page iterator.

Error received from the generic delta collection iterator:

DeltaGetResponse res2 = graphServiceClient.groups().delta().get();
genericIterator(graphServiceClient, res2, (Group group) -> {
  log.info("Group: {} {}", group.getId(), group.getDisplayName());
}, (String deltaLink) -> {
  log.info("DeltaLink: {}", deltaLink);
});
java.lang.IllegalAccessException: NO_COLLECTION_PROPERTY_ERROR
        at com.microsoft.graph.core.tasks.PageIterator.extractEntityListFromParsable(PageIterator.java:300)
        at com.microsoft.graph.core.tasks.PageIterator.interpageIterate(PageIterator.java:250)
        at com.microsoft.graph.core.tasks.PageIterator.iterate(PageIterator.java:272)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 com.microsoft.graph.core.tasks.PageIterator.java 中的 PageIterator 实现开始,重点查看 extractEntityListFromParsable 和页间迭代路径,然后查看 docs/upgrade-to-v6.md 中的 PageIterator 部分。复现 issue 中的通用 delta 示例,并确定 collection 请求和 delta collection 请求是否可以共用一个简洁的迭代器;完成的标准是明确受支持的行为和所需的 API 更改。

由索引模型根据 Issue 内容生成。

评估

技术栈
java
领域
api
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。