Merge pull request #13253 from backstage/emmaindal/stack-overflow-collator-paginated-request

This commit is contained in:
Emma Indal
2022-08-23 21:10:33 +02:00
committed by GitHub
3 changed files with 35 additions and 10 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-stack-overflow-backend': patch
---
Now requests all questions available using pagination. Default max page is set to 100, with a configurable `maxPage` option on the collator.
@@ -41,6 +41,7 @@ export class StackOverflowQuestionsCollatorFactory
// @public
export type StackOverflowQuestionsCollatorFactoryOptions = {
baseUrl?: string;
maxPage?: number;
apiKey?: string;
requestParams: StackOverflowQuestionsRequestParams;
logger: Logger;
@@ -50,6 +50,7 @@ export type StackOverflowQuestionsRequestParams = {
*/
export type StackOverflowQuestionsCollatorFactoryOptions = {
baseUrl?: string;
maxPage?: number;
apiKey?: string;
requestParams: StackOverflowQuestionsRequestParams;
logger: Logger;
@@ -66,12 +67,14 @@ export class StackOverflowQuestionsCollatorFactory
protected requestParams: StackOverflowQuestionsRequestParams;
private readonly baseUrl: string | undefined;
private readonly apiKey: string | undefined;
private readonly maxPage: number | undefined;
private readonly logger: Logger;
public readonly type: string = 'stack-overflow';
private constructor(options: StackOverflowQuestionsCollatorFactoryOptions) {
this.baseUrl = options.baseUrl;
this.apiKey = options.apiKey;
this.maxPage = options.maxPage;
this.requestParams = options.requestParams;
this.logger = options.logger;
}
@@ -84,9 +87,11 @@ export class StackOverflowQuestionsCollatorFactory
const baseUrl =
config.getOptionalString('stackoverflow.baseUrl') ||
'https://api.stackexchange.com/2.2';
const maxPage = options.maxPage || 100;
return new StackOverflowQuestionsCollatorFactory({
...options,
baseUrl,
maxPage,
apiKey,
});
}
@@ -122,17 +127,31 @@ export class StackOverflowQuestionsCollatorFactory
? `${params ? '&' : '?'}key=${this.apiKey}`
: '';
const res = await fetch(`${this.baseUrl}/questions${params}${apiKeyParam}`);
const data = await res.json();
let hasMorePages = true;
let page = 1;
while (hasMorePages) {
if (page === this.maxPage) {
this.logger.warn(
`Over ${this.maxPage} requests to the Stack Overflow API have been made, which may not have been intended. Either specify requestParams that limit the questions returned, or configure a higher maxPage if necessary.`,
);
break;
}
const res = await fetch(
`${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`,
);
for (const question of data.items) {
yield {
title: question.title,
location: question.link,
text: question.owner.display_name,
tags: question.tags,
answers: question.answer_count,
};
const data = await res.json();
for (const question of data.items) {
yield {
title: question.title,
location: question.link,
text: question.owner.display_name,
tags: question.tags,
answers: question.answer_count,
};
}
hasMorePages = data.has_more;
page = page + 1;
}
}
}