**Not once was `backoff`, `error_id`, or `error_name` returned by the API.**
Not once was backoff
, error_id
, or error_name
returned by the API.
Complete Code to Replicate:
##Complete Code to Replicate:
const sortOrder = "desc"; // "asc" or "desc"
const cYourKey = "&key=U4DMV*8nvpm3EOpvf69Rxw(("; // best to use your own
const cMaxPages = 300;
const tagStats = {};
let PageNm = 1;
let timeDiffSum = 0;
let timeDiffCnt = 0;
let lastTime = null;
getPageOfTags (PageNm);
function sessionStat () {
console.log (`Fetched ${PageNm} pages from the API.`);
console.log (`Found ${Object.keys(tagStats).length} tag names.`);
console.log (`Average API rate was ${ (1000.0 * timeDiffCnt / timeDiffSum).toFixed(2) } calls per second.`);
}
function getPageOfTags (pageNum) {
fetch (
`https://api.stackexchange.com/2.2/tags?page=${pageNum}&pagesize=100&sort=name&order=${sortOrder}&site=stackoverflow${cYourKey}`
)
.then (function (response) {
if (response.ok) return response.json();
throw new Error (`API problem: Error ${response.status}, ${response.statusText}`);
} )
.then (function (rJsn) {
if (rJsn.backoff || rJsn.error_id) {
throw new Error (`From API: Backoff ${rJsn.backoff}, Error: ${rJsn.error_id}, ${rJsn.error_message}, ${rJsn.error_name}`);
}
let timenow = (new Date() ).getTime();
if (lastTime) {
let timediff = timenow - lastTime;
timeDiffCnt++;
timeDiffSum += timediff;
}
lastTime = timenow;
console.log (`Page:${ (''+PageNm).padStart(4)}, Quota remaining: ${rJsn.quota_remaining}; Items found: ${rJsn.items.length}`, timenow);
let allDone = ! rJsn.has_more;
let zTags = rJsn.items;
for (let zTag of zTags) {
tagStats[zTag.name] = zTag.count;
}
if (rJsn.has_more) {
if (PageNm >= cMaxPages)
allDone = true;
else {
PageNm++;
getPageOfTags (PageNm);
}
}
if (allDone) {
//--- Do final processing here...
console.log ("All done. Running sessionStat()...");
sessionStat ();
}
} )
.catch (function (error) {
console.log (`Page:${ ('' + PageNm).padStart(4)}, ERROR:`);
console.error (error);
} );
};
var sortOrder = "desc"; // "asc" or "desc"
var cYourKey = ""; // "&key=Best to Get and Use your own key";
var cMaxPages = 300;
var tagStats = {};
var PageNm = 1;
var timeDiffSum = 0;
var timeDiffCnt = 0;
var lastTime = null;
getPageOfTags (PageNm);
function sessionStat () {
console.log (`Fetched ${PageNm} pages from the API.`);
console.log (`Found ${Object.keys(tagStats).length} tag names.`);
console.log (`Average API rate was ${ (1000.0 * timeDiffCnt / timeDiffSum).toFixed(2) } calls per second.`);
}
function getPageOfTags (pageNum) {
fetch (
`https://api.stackexchange.com/2.2/tags?page=${pageNum}&pagesize=100&sort=name&order=${sortOrder}&site=stackoverflow${cYourKey}`
)
.then (function (response) {
if (response.ok) return response.json();
throw new Error (`API problem: Error ${response.status}, ${response.statusText}`);
} )
.then (function (rJsn) {
if (rJsn.backoff || rJsn.error_id) {
throw new Error (`From API: Backoff ${rJsn.backoff}, Error: ${rJsn.error_id}, ${rJsn.error_message}, ${rJsn.error_name}`);
}
let timenow = (new Date() ).getTime();
if (lastTime) {
let timediff = timenow - lastTime;
timeDiffCnt++;
timeDiffSum += timediff;
}
lastTime = timenow;
console.log (`Page:${ (''+PageNm).padStart(4)}, Quota remaining: ${rJsn.quota_remaining}; Items found: ${rJsn.items.length}`, timenow);
let allDone = ! rJsn.has_more;
let zTags = rJsn.items;
for (let zTag of zTags) {
tagStats[zTag.name] = zTag.count;
}
if (rJsn.has_more) {
if (PageNm >= cMaxPages)
allDone = true;
else {
PageNm++;
getPageOfTags (PageNm);
}
}
if (allDone) {
//--- Do final processing here...
console.log ("All done. Running sessionStat()...");
sessionStat ();
}
} );
};