Support for 2.3 teamName

Signed-off-by: Chris Hawkins <cmdkeen@users.noreply.github.com>
This commit is contained in:
Chris Hawkins
2023-03-29 16:59:30 +01:00
committed by Chris
parent a2d7f82c94
commit a0ac7496ad
2 changed files with 68 additions and 3 deletions
@@ -139,5 +139,49 @@ describe('StackOverflowQuestionsCollatorFactory', () => {
expect(documents).toHaveLength(mockOverrideQuestion.items.length);
});
it('uses API key when provided', async () => {
worker.use(
rest.get('http://stack.overflow.override/questions', (req, res, ctx) =>
req.url.searchParams.get('key') === 'abcdefg'
? res(ctx.status(200), ctx.json(mockOverrideQuestion))
: res(ctx.status(401), ctx.json({})),
),
);
const factory = StackOverflowQuestionsCollatorFactory.fromConfig(config, {
logger,
baseUrl: 'http://stack.overflow.override',
apiKey: 'abcdefg',
requestParams: defaultOptions.requestParams,
});
const collator = await factory.getCollator();
const pipeline = TestPipeline.fromCollator(collator);
const { documents } = await pipeline.execute();
expect(documents).toHaveLength(mockOverrideQuestion.items.length);
});
it('uses teamName when provided', async () => {
worker.use(
rest.get('http://stack.overflow.override/questions', (req, res, ctx) =>
req.url.searchParams.get('team') === 'abcdefg'
? res(ctx.status(200), ctx.json(mockOverrideQuestion))
: res(ctx.status(401), ctx.json({})),
),
);
const factory = StackOverflowQuestionsCollatorFactory.fromConfig(config, {
logger,
baseUrl: 'http://stack.overflow.override',
teamName: 'abcdefg',
requestParams: defaultOptions.requestParams,
});
const collator = await factory.getCollator();
const pipeline = TestPipeline.fromCollator(collator);
const { documents } = await pipeline.execute();
expect(documents).toHaveLength(mockOverrideQuestion.items.length);
});
});
});
@@ -53,6 +53,7 @@ export type StackOverflowQuestionsCollatorFactoryOptions = {
maxPage?: number;
apiKey?: string;
apiAccessToken?: string;
teamName?: string;
requestParams: StackOverflowQuestionsRequestParams;
logger: Logger;
};
@@ -69,6 +70,7 @@ export class StackOverflowQuestionsCollatorFactory
private readonly baseUrl: string | undefined;
private readonly apiKey: string | undefined;
private readonly apiAccessToken: string | undefined;
private readonly teamName: string | undefined;
private readonly maxPage: number | undefined;
private readonly logger: Logger;
public readonly type: string = 'stack-overflow';
@@ -77,6 +79,7 @@ export class StackOverflowQuestionsCollatorFactory
this.baseUrl = options.baseUrl;
this.apiKey = options.apiKey;
this.apiAccessToken = options.apiAccessToken;
this.teamName = options.teamName;
this.maxPage = options.maxPage;
this.requestParams = options.requestParams;
this.logger = options.logger.child({ documentType: this.type });
@@ -90,15 +93,18 @@ export class StackOverflowQuestionsCollatorFactory
const apiAccessToken = config.getOptionalString(
'stackoverflow.apiAccessToken',
);
const pat = config.getOptionalString('stackoverflow.pat');
const teamName = config.getOptionalString('stackoverflow.teamName');
const baseUrl =
config.getOptionalString('stackoverflow.baseUrl') ||
'https://api.stackexchange.com/2.2';
'https://api.stackexchange.com/2.3';
const maxPage = options.maxPage || 100;
return new StackOverflowQuestionsCollatorFactory({
baseUrl,
maxPage,
apiKey,
apiAccessToken,
teamName,
...options,
});
}
@@ -114,10 +120,16 @@ export class StackOverflowQuestionsCollatorFactory
);
}
if (this.apiKey && this.teamName) {
this.logger.debug(
'Both stackoverflow.apiKey and stackoverflow.teamName configured in your app-config.yaml, apiKey must be removed before teamName will be used',
);
}
try {
if (Object.keys(this.requestParams).indexOf('key') >= 0) {
this.logger.warn(
'The API Key should be passed as a seperate param to bypass encoding',
'The API Key should be passed as a separate param to bypass encoding',
);
delete this.requestParams.key;
}
@@ -134,6 +146,15 @@ export class StackOverflowQuestionsCollatorFactory
? `${params ? '&' : '?'}key=${this.apiKey}`
: '';
const teamParam = this.teamName
? `${params ? '&' : '?'}team=${this.teamName}`
: '';
// PAT change requires team name as a parameter
const requestUrl = this.apiKey
? `${this.baseUrl}/questions${params}${apiKeyParam}`
: `${this.baseUrl}/questions${params}${teamParam}`;
let hasMorePages = true;
let page = 1;
while (hasMorePages) {
@@ -144,7 +165,7 @@ export class StackOverflowQuestionsCollatorFactory
break;
}
const res = await fetch(
`${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`,
`${requestUrl}&page=${page}`,
this.apiAccessToken
? {
headers: {