update Stack Overflow plugin to support API Access Token

Signed-off-by: Connor Younglund <younglund_connor@bah.com>
This commit is contained in:
Connor Younglund
2022-11-29 13:47:04 -05:00
parent 600810c367
commit 73ab069f5e
2 changed files with 11 additions and 0 deletions
@@ -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();