From 761d540ed2fa51b711c4eacb6c04be109764abf8 Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Fri, 19 Aug 2022 16:37:59 +0200 Subject: [PATCH] set max page to limit requests Signed-off-by: Emma Indal --- .../search/StackOverflowQuestionsCollatorFactory.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts index 5803ef4b6b..a98fc7a2bc 100644 --- a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts +++ b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts @@ -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, }); } @@ -125,6 +130,12 @@ export class StackOverflowQuestionsCollatorFactory let hasMorePages = true; let page = 1; while (hasMorePages) { + if (page === this.maxPage) { + this.logger.warn( + 'You have made over 100 requests to the Stack Overflow API, we recommend you to specify requestParams to limit your search to not reach your API limit or configure your own maxPage.', + ); + break; + } const res = await fetch( `${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`, );