Skip to content

Commit

Permalink
feat: support GITHUB_TOKEN to increae github api request rate (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jan 13, 2024
1 parent 05445b4 commit 0f7aa4a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/common/adapter/binary/AbstractBinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ export abstract class AbstractBinary {
return xml;
}

protected async requestJSON(url: string) {
protected async requestJSON(url: string, requestHeaders?: Record<string, string>) {
const { status, data, headers } = await this.httpclient.request(url, {
timeout: 30000,
dataType: 'json',
followRedirect: true,
gzip: true,
headers: requestHeaders,
});
if (status !== 200) {
this.logger.warn('[AbstractBinary.requestJSON:non-200-status] url: %s, status: %s, headers: %j', url, status, headers);
Expand Down
6 changes: 5 additions & 1 deletion app/common/adapter/binary/GithubBinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export class GithubBinary extends AbstractBinary {
const maxPage = binaryConfig.options?.maxPage || 1;
for (let i = 0; i < maxPage; i++) {
const url = `https://api.github.com/repos/${binaryConfig.repo}/releases?per_page=100&page=${i + 1}`;
const data = await this.requestJSON(url);
const requestHeaders: Record<string, string> = {};
if (process.env.GITHUB_TOKEN) {
requestHeaders.Authorization = `token ${process.env.GITHUB_TOKEN}`;
}
const data = await this.requestJSON(url, requestHeaders);
if (!Array.isArray(data)) {
// {"message":"API rate limit exceeded for 47.57.239.54. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}
if (typeof data?.message === 'string' && data.message.includes('rate limit')) {
Expand Down

0 comments on commit 0f7aa4a

Please sign in to comment.