Reputation: 38
We have a search function on our website. I need to index all of our Help Desk articles from Zendesk so that they will appear in search results. I currently have the following code, but it will only return the first 30 results.
const https = require('https');
const { Textify } = require('../common/Textify');
const fs = require('fs');
const path = require('path');
function GetZDeskArticles() {
return new Promise((res, rej) => {
const request = https.request(
'https://example.zendesk.com/api/v2/help_center/en-us/articles.json',
(response) => {
let data = '';
response.on('data', (chunck) => {
data = data + chunck.toString();
});
response.on('end', () => {
const body = JSON.parse(data);
res(body.articles);
});
}
);
request.on('error', (error) => {
rej(error);
});
request.end();
});
}
async function StoreZDeskArticles() {
const zdeskArticles = await GetZDeskArticles();
let arts = [];
zdeskArticles.forEach((art) => {
if (art.draft) return;
art.body = Textify(art.body);
arts.push(art);
});
let storagePath = path.resolve(
__dirname,
'../',
'_data',
'ZenDeskArticles.json'
);
fs.writeFileSync(storagePath, JSON.stringify(arts), 'utf8');
}
StoreZDeskArticles();
If I add ?page%5Bsize%5D=100
to the request url, I obviously will get the first 100 results. But I have 164 articles in total that I need to index. I have read through the Zendesk pagination documentation and am unsure of how apply their solution to my situation.
Upvotes: 1
Views: 1857
Reputation: 41
For pagination: In the returned JSON, there is a section "links" where there is an entry "next", that is the URL to the next page of articles that you need to perform your next request to.
For offset: In the returned JSON is an entry "next_page" that gives you the next page. However, it is recommended to use pagination instead of offset, as it has better performance.
For both solutions: You iterate over requests, using the "next" or "next_page" entry for follow-up requests until the respective entry is null, then you have seen all articles.
https://developer.zendesk.com/api-reference/introduction/pagination/
Upvotes: 1