【发布时间】:2019-03-21 09:30:17
【问题描述】:
我正在调用 bitbucket API 以获取存储库中的所有文件。我已经达到了可以获取存储库中所有文件夹的列表的地步,并对存储库中的所有根文件夹进行第一次 API 调用,并获取所有文件夹的前 1000 个文件的列表。
但问题是 bitbucket api 一次只能给我每个文件夹 1000 个文件。
我需要附加一个查询参数 &start =nextPageStart 并再次调用,直到它为空并且每个 API 的 isLastPage 为真。我怎样才能用下面的代码实现呢??
我从第一次调用 api 得到 nextPageStart。请参阅下面的 API 响应。
下面是我目前的代码。
感谢任何帮助或指导。
来自每个文件夹调用的单个 API 的响应。
{
"values": [
"/src/js/abc.js",
"/src/js/efg.js",
"/src/js/ffg.js",
...
],
"size": 1000,
"isLastPage": false,
"start": 0,
"limit": 1000,
"nextPageStart": 1000
}
我进行异步调用以获取文件列表的函数
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);
}
【问题讨论】:
-
下一页的网址在哪里?或者说
isLastPage = false时会怎样形成。 -
如果 isLastPage 为 false,我需要将查询参数附加到 URL &start =nextPageStart
标签: javascript node.js typescript async.js node-request