javascript - Node中并行/异步的多个分页GET API调用

标签 javascript node.js typescript async.js node-request

我正在调用 bitbucket API 以获取存储库中的所有文件。我已经达到了可以获取存储库中所有文件夹的列表的地步,并对存储库中的所有根文件夹进行第一次 API 调用,并获取所有文件夹的前 1000 个文件的列表。

但问题是 bitbucket api 一次只能给我每个文件夹 1000 个文件。

我需要附加一个查询参数 &start =nextPageStart 并再次调用,直到它为空并且每个 API 的 isLastPage 为真。我怎样才能用下面的代码来实现呢??

我从第一次调用 api 得到 nextPageStart。请参阅下面的 API 响应。

下面是我目前的代码。

感谢任何帮助或指导。

Response from individual API thats called per folder.

{
    "values": [
        "/src/js/abc.js",
        "/src/js/efg.js",
        "/src/js/ffg.js",
        ...
    ],
    "size": 1000,
    "isLastPage": false,
    "start": 0,
    "limit": 1000,
    "nextPageStart": 1000
}

function where i made asynchronous calls to get the list of files

export function getFilesList() {
  const foldersURL: any[] = [];
  getFoldersFromRepo().then((response) => {
    const values = response.values;
    values.forEach((value: any) => {
    //creating API URL for each folder in the repo
      const URL = 'https://bitbucket.abc.com/stash/rest/api/latest/projects/'
                   + value.project.key + '/repos/' + value.slug + '/files?limit=1000';
      foldersURL.push(URL);
        });
    return foldersURL;
      }).then((res) => {
    // console.log('Calling all the URLS in parallel');
    async.map(res, (link, callback) => {
       const options = {
         url: link,
         auth: {
           password: 'password',
           username: 'username',
         },
       };
       request(options, (error, response, body) => {

      // TODO: How do I make the get call again so that i can paginate and append the response to the body till the last page.

         callback(error, body);
       });
     }, (err, results) => {
       console.log('In err, results function');
       if (err) {
         return console.log(err);
       }
       //Consolidated results after all API calls.
       console.log('results', results);
     });
  })
   .catch((error) => error);
}

最佳答案

我能够通过创建带有回调的函数来使其正常工作。

export function getFilesList() {
  const foldersURL: any[] = [];
  getFoldersFromRepo().then((response) => {
    const values = response.values;
    values.forEach((value: any) => {
    //creating API URL for each folder in the repo
      const URL = 'https://bitbucket.abc.com/stash/rest/api/latest/projects/'
                   + value.project.key + '/repos/' + value.slug + '/files?limit=1000';
      foldersURL.push(URL);
        });
    return foldersURL;
      }).then((res) => {
    // console.log('Calling all the URLS in parallel');
    async.map(res, (link, callback) => {
       const options = {
         url: link,
         auth: {
           password: 'password',
           username: 'username',
         },
       };
      const myarray = [];
// This function will consolidate response till the last Page per API.
      consolidatePaginatedResponse(options, link, myarray, callback);
     }, (err, results) => {
       console.log('In err, results function');
       if (err) {
         return console.log(err);
       }
       //Consolidated results after all API calls.
       console.log('results', results);
     });
  })
   .catch((error) => error);
}

function consolidatePaginatedResponse(options, link, myarray, callback) {
  request(options, (error, response, body) => {
    const content = JSON.parse(body);
    content.link = options.url;
    myarray.push(content);
    if (content.isLastPage === false) {
      options.url = link + '&start=' + content.nextPageStart;
      consolidatePaginatedResponse(options, link, myarray, callback);
    } else {
// Final response after consolidation per API
      callback(error, JSON.stringify(myarray));
    }
  });
}

关于javascript - Node中并行/异步的多个分页GET API调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52842394/

相关文章:

javascript 表单 cookie - 仅选择前 10 个索引的打开 cookie

javascript - d3.js 中 x 轴的缩写月份格式

angular - 如何在不重新加载页面的情况下刷新 Angular 8 中的元素?

Angular - 具有嵌套 FormGroup 的组件中没有用于表单控制的值访问器

sql-server - 从 sql 查询制作 json

javascript - 在node.js中编写嵌套非阻塞循环

javascript - 了解在 Canvas 弧中为端点完成的数学运算

node.js - 恶梦循环法提取数据

javascript - 编译 Jade 模板时出现类型错误

node.js - 如何在控制台中禁用 Node mongodb-native 驱动程序连接日志记录