Add GitHub token authentication to sync services
- Add GitHub token authentication to GitHubJsonService for API calls - Add GitHub token authentication to GitHubService for API calls - Update fetchFromGitHub methods to use GITHUB_TOKEN from .env - Update downloadJsonFile methods to use GitHub token for raw file downloads - Add proper error handling for rate limit exceeded (403) errors - Add console logging to show when token is/isn't being used - Improve error messages to suggest setting GITHUB_TOKEN for higher rate limits This ensures that when a GitHub token is specified in .env, it will be used for all GitHub API calls during sync operations, providing higher rate limits and better reliability.
This commit is contained in:
@@ -29,14 +29,25 @@ export class GitHubService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async fetchFromGitHub<T>(endpoint: string): Promise<T> {
|
private async fetchFromGitHub<T>(endpoint: string): Promise<T> {
|
||||||
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
const headers: HeadersInit = {
|
||||||
headers: {
|
'Accept': 'application/vnd.github.v3+json',
|
||||||
'Accept': 'application/vnd.github.v3+json',
|
'User-Agent': 'PVEScripts-Local/1.0',
|
||||||
'User-Agent': 'PVEScripts-Local/1.0',
|
};
|
||||||
},
|
|
||||||
});
|
// Add GitHub token authentication if available
|
||||||
|
if (env.GITHUB_TOKEN) {
|
||||||
|
headers.Authorization = `token ${env.GITHUB_TOKEN}`;
|
||||||
|
console.log('Using GitHub token for API authentication');
|
||||||
|
} else {
|
||||||
|
console.log('No GitHub token found, using unauthenticated requests (lower rate limits)');
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`${this.baseUrl}${endpoint}`, { headers });
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (response.status === 403) {
|
||||||
|
throw new Error(`GitHub API rate limit exceeded. Consider setting GITHUB_TOKEN for higher limits. Status: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
|
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,14 +41,26 @@ export class GitHubJsonService {
|
|||||||
|
|
||||||
private async fetchFromGitHub<T>(endpoint: string): Promise<T> {
|
private async fetchFromGitHub<T>(endpoint: string): Promise<T> {
|
||||||
this.initializeConfig();
|
this.initializeConfig();
|
||||||
const response = await fetch(`${this.baseUrl!}${endpoint}`, {
|
|
||||||
headers: {
|
const headers: HeadersInit = {
|
||||||
'Accept': 'application/vnd.github.v3+json',
|
'Accept': 'application/vnd.github.v3+json',
|
||||||
'User-Agent': 'PVEScripts-Local/1.0',
|
'User-Agent': 'PVEScripts-Local/1.0',
|
||||||
},
|
};
|
||||||
});
|
|
||||||
|
// Add GitHub token authentication if available
|
||||||
|
if (env.GITHUB_TOKEN) {
|
||||||
|
headers.Authorization = `token ${env.GITHUB_TOKEN}`;
|
||||||
|
console.log('Using GitHub token for API authentication');
|
||||||
|
} else {
|
||||||
|
console.log('No GitHub token found, using unauthenticated requests (lower rate limits)');
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`${this.baseUrl!}${endpoint}`, { headers });
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (response.status === 403) {
|
||||||
|
throw new Error(`GitHub API rate limit exceeded. Consider setting GITHUB_TOKEN for higher limits. Status: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
|
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,8 +71,20 @@ export class GitHubJsonService {
|
|||||||
this.initializeConfig();
|
this.initializeConfig();
|
||||||
const rawUrl = `https://raw.githubusercontent.com/${this.extractRepoPath()}/${this.branch!}/${filePath}`;
|
const rawUrl = `https://raw.githubusercontent.com/${this.extractRepoPath()}/${this.branch!}/${filePath}`;
|
||||||
|
|
||||||
const response = await fetch(rawUrl);
|
const headers: HeadersInit = {
|
||||||
|
'User-Agent': 'PVEScripts-Local/1.0',
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add GitHub token authentication if available (for raw files, use token in URL or header)
|
||||||
|
if (env.GITHUB_TOKEN) {
|
||||||
|
headers.Authorization = `token ${env.GITHUB_TOKEN}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(rawUrl, { headers });
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (response.status === 403) {
|
||||||
|
throw new Error(`GitHub rate limit exceeded while downloading ${filePath}. Consider setting GITHUB_TOKEN for higher limits. Status: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
throw new Error(`Failed to download ${filePath}: ${response.status} ${response.statusText}`);
|
throw new Error(`Failed to download ${filePath}: ${response.status} ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user