处理一个带下一个请求直到完成的httpclient

时间:2019-06-10 20:09:49

标签: angular sharepoint httpclient

使用Sharepoint API调用时,一个巨大的列表超过100行或您放入$ top = 100的行。您将获得列表的字段和__next变量。这个__next变量包含一个字符串,该字符串指向您要查询列表中后续元素的下一页地址。

我的问题在于我无法同步执行http.get请求,因此无法知道下一个是否结束。

我尝试了递归方法,构建了自己的自定义RxJS运算符,并构建了一个可调用Subscriber的Observable。接下来,我总是对后续请求感到迷惑。

getExpedienteFromUrl(httpUrl?: string): Observable<ExpedienteListAndNextQuery> {
    const urlAddress = (httpUrl ? httpUrl : this.urlSharepointAPIExpedientes );
    return this.http.get<D>(urlAddress, {
      headers: {
        accept: 'application/json;odata=verbose'
      }
    }).pipe(
      map(x => this.mapExpedientesOfObserver(x) ),
      this.getExpedientesPipe()
    );
}

getExpedientesPipe(): Observable <IExpediente[]> {
    return ($source: Observable<ExpedienteListAndNextQuery>) => {
      return new Observable<IExpediente[]>(subscriber => {
        $source.subscribe(
          data => {
            const expedientes: IExpediente[] = data.Expedientes;
            const next: string = data.Next;
            subscriber.next(expedientes);
            if (next) {
              // call to this.getExpedienteFromUrl(next) [Async]
              // so if this where in a do while the following next will always 
              // be undefined
            }
          },
          err => subscriber.error(err),
          () => subscriber.complete()
        );
      });
    };
  }

1 个答案:

答案 0 :(得分:0)

谢谢@ user2457680。

我将发布代码最终是如何解决我的问题的。我希望其他人会发现它有用。

getExpediente(): Observable<IExpediente> {
    return this.http.get<D>(this.urlSharepointAPIExpedientes, {
      headers: {
        accept: 'application/json;odata=verbose'
      }
    }).pipe(
      map(x => this.mapExpedientesOfObserver(x) ),
      expand(x => (x.Next ? this.getExpedienteFromUrl(x.Next) : EMPTY)),
      concatMap(x => x.Expedientes)
    );
  }

  private getExpedienteFromUrl(httpUrl: string): Observable<ExpedienteListAndNextQuery> {
    return this.http.get<D>(httpUrl, {
      headers: {
        accept: 'application/json;odata=verbose'
      }
    }).pipe(
      map(x => this.mapExpedientesOfObserver(x))
    );
  }

export interface D {
  d: Results;
}

export interface Results {
  results: SharepointExpediente[];
  __next: string;
}

在所有SharePoint都喜欢使用word_x020_word_x020_word来维护自己或他人的噩梦之后,地图功能只会将SharePoint变量转换为类人变量。