可观察的继续调用API并根据条件更改参数 [英] Observable Continue calling API and changing parameters based on condition

查看:74
本文介绍了可观察的继续调用API并根据条件更改参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 Rx.js重复文档,以期找出答案如何根据从API收到的响应继续调用api.我正在呼叫一次只能返回回2k记录的API.该API将发回一个值供我发送,以便我可以继续接收记录,直到它们返回完成的值.

I've read the Rx.js repeat Documentation in an effort to find out how I can continue calling an api based upon the response that I receive from the API. I'm calling an API that can only send back 2k records at a time. The API will send back a value for me to send it so that I can continue receiving the records until they return a done value.

因此流程如下:

  1. 发出GET请求查询参数reqMode='':
  2. 检索响应,最后一个数组包含reqModevaluedone.
  3. 如果我收到一个value,则我需要发出相同的请求,但发送带有值的reqMode参数.
  4. 如果我收到done,则我将停下来并返回自首次呼叫以来的所有记录.
  1. Make a GET request a query parameter reqMode='':
  2. retrieve response with the last array containing reqMode with a value or done.
  3. If I receive a value then I need to make the same request but send the reqMode parameter with the value.
  4. If i receive done then I'll stop and return all of the records since the first call.

我在subscribing normally时获得了第一组值,但这是我在阅读文档后的尝试,但这没有意义:

I get the first set of values when subscribing normally, but this would be my attempt after reading the docs, but it doesn't make sense:

getRecords(){
    let url = this.url + 'reqMode=';
    return this.http.get(url)
            .doWhile() //What would I do here
}

当尝试使用类型为Observable<response>的Observable进行.doWhile时.我正在寻找使用Observables的其他替代方法.

When trying to do .doWhile with a Observable that is type Observable<response>. I'm looking for any alternative using Observables for what I need to do.

推荐答案

我认为repeat()并不是一个很好的运算符.如果我对您的理解正确,那么您想根据前一个请求的响应来重复HTTP请求.如果您想多次重复同一请求,操作员repeat()是很好的选择.

I don't think repeat() is a good operator for this. If I understand you correctly you want to repeat the HTTP request based on the response of the previous request. Operator repeat() is good if you wanted to repeat the same request multiple times.

我将使用 concatMap() 并递归调用自身,直到reqMode等同于"done":

观看现场演示: http://plnkr.co/edit/w0DdepslTaKrLSB3aIkA

import {Observable, Subject} from 'rxjs';

const result = new Subject();
const closeBuffer = new Subject();
const buffer = result.buffer(closeBuffer.asObservable());

function sendHttpRequest(reqMode) {
  return Observable.of('{"reqMode":' + reqMode + '}')
    .map(response => JSON.parse(response))
    .concatMap(data => {
      console.log('HTTP Response:', data);
      // Add data to the buffer of results
      result.next(data);

      if (data.reqMode == 'done') {
        // Return an empty value wrapped as an Observable so concatMap can work
        // with it and emit onNext when it completes (which is immediately
        // thanks to the `.of()` operator).
        return Observable.of(null);
      } else {
        // Simulate that the next call returns 'done'
        return sendHttpRequest('"done"');

        // Uncomment this for real usage
        //return sendHttpRequest(data.reqMode);
      }
    });
}

// Subscribe to the buffer where I'll receive the value.
buffer.subscribe(val => console.log('Next: ', val));

// Simulate HTTP request with reqMode = 42
sendHttpRequest(42).subscribe(() => {
  console.log('done');
  // Emit values from the buffer.
  closeBuffer.next(null);
  closeBuffer.complete();
});

我使用 of() 运算符,以模拟请求并返回包装为Observable的值.我还使用Subject来保存所有使用

I use of() operator to simulate a request and to return a value wrapped as an Observable. I also use Subject to hold all responses that are buffered using buffer() operator. The I subscribe to the buffer to get the final array of responses (If you wrap this code into a function you'll most likely return the buffer where you can subscribe later).

响应如下:

HTTP Response: Object {reqMode: 42}
HTTP Response: Object {reqMode: "done"}
Next:  [Object, Object]

看到类似的问题: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆