From 73ab069f5e3e1020f658d5a5d0b9f1558f6b0100 Mon Sep 17 00:00:00 2001 From: Connor Younglund Date: Tue, 29 Nov 2022 13:47:04 -0500 Subject: [PATCH] update Stack Overflow plugin to support API Access Token Signed-off-by: Connor Younglund --- plugins/stack-overflow-backend/README.md | 1 + .../search/StackOverflowQuestionsCollatorFactory.ts | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/plugins/stack-overflow-backend/README.md b/plugins/stack-overflow-backend/README.md index a35a6678cb..65fa50e062 100644 --- a/plugins/stack-overflow-backend/README.md +++ b/plugins/stack-overflow-backend/README.md @@ -23,6 +23,7 @@ If you have a private stack overflow instance you will need to supply an API key stackoverflow: baseUrl: https://api.stackexchange.com/2.2 # alternative: your internal stack overflow instance apiKey: $STACK_OVERFLOW_API_KEY + apiAccessToken: $STACK_OVERFLOW_API_ACCESS_TOKEN ``` ## Areas of Responsibility diff --git a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts index 1aa0e5d300..68aa83cdeb 100644 --- a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts +++ b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts @@ -52,6 +52,7 @@ export type StackOverflowQuestionsCollatorFactoryOptions = { baseUrl?: string; maxPage?: number; apiKey?: string; + apiAccessToken?: string; requestParams: StackOverflowQuestionsRequestParams; logger: Logger; }; @@ -67,6 +68,7 @@ export class StackOverflowQuestionsCollatorFactory protected requestParams: StackOverflowQuestionsRequestParams; private readonly baseUrl: string | undefined; private readonly apiKey: string | undefined; + private readonly apiAccessToken: string | undefined; private readonly maxPage: number | undefined; private readonly logger: Logger; public readonly type: string = 'stack-overflow'; @@ -74,6 +76,7 @@ export class StackOverflowQuestionsCollatorFactory private constructor(options: StackOverflowQuestionsCollatorFactoryOptions) { this.baseUrl = options.baseUrl; this.apiKey = options.apiKey; + this.apiAccessToken = options.apiAccessToken; this.maxPage = options.maxPage; this.requestParams = options.requestParams; this.logger = options.logger.child({ documentType: this.type }); @@ -84,6 +87,7 @@ export class StackOverflowQuestionsCollatorFactory options: StackOverflowQuestionsCollatorFactoryOptions, ) { const apiKey = config.getOptionalString('stackoverflow.apiKey'); + const apiAccessToken = config.getOptionalString('stackoverflow.apiAccessToken'); const baseUrl = config.getOptionalString('stackoverflow.baseUrl') || 'https://api.stackexchange.com/2.2'; @@ -93,6 +97,7 @@ export class StackOverflowQuestionsCollatorFactory baseUrl, maxPage, apiKey, + apiAccessToken, }); } @@ -138,6 +143,11 @@ export class StackOverflowQuestionsCollatorFactory } const res = await fetch( `${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`, + this.apiAccessToken ? { + headers: { + 'X-API-Access-Token': this.apiAccessToken, + }, + } : undefined, ); const data = await res.json();