-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathapiKeySwitcher.js
84 lines (77 loc) · 3.29 KB
/
apiKeySwitcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class ApiKeySwitcher {
constructor(keys) {
this.keys = keys || [];
this.currentIndex = 0;
this.userToken = localStorage.getItem('userOmdbToken'); // توکن کاربر
}
getCurrentKey() {
if (this.userToken) {
console.log('استفاده از توکن کاربر:', this.userToken);
return this.userToken; // اولویت با توکن کاربر
}
if (this.keys.length === 0) {
throw new Error('هیچ کلید API در دسترس نیست.');
}
return this.keys[this.currentIndex];
}
switchToNextKey() {
if (this.userToken) {
console.log('توکن کاربر استفاده میشود، تعویض کلید غیرفعال است.');
return; // اگر توکن کاربر باشد، تعویض کلید غیرفعال است
}
this.currentIndex = (this.currentIndex + 1) % this.keys.length;
console.log(`تعویض به کلید API جدید: ${this.getCurrentKey()}`);
}
async fetchWithKeySwitch(urlTemplate, maxRetriesPerKey = 3) {
let attempts = 0;
const totalAttemptsLimit = this.userToken ? maxRetriesPerKey : this.keys.length * maxRetriesPerKey;
while (attempts < totalAttemptsLimit) {
const url = urlTemplate(this.getCurrentKey());
try {
const response = await fetch(url);
if (!response.ok) {
if (response.status === 429) {
console.warn('محدودیت نرخ OMDB API - تلاش مجدد...');
await new Promise(resolve => setTimeout(resolve, 1000));
attempts++;
continue;
}
throw new Error(`خطای سرور (OMDB): ${response.status}`);
}
return await response.json();
} catch (error) {
console.warn(`خطا در درخواست با کلید ${this.getCurrentKey()}: ${error.message}`);
attempts++;
if (!this.userToken && attempts % maxRetriesPerKey === 0) {
this.switchToNextKey();
}
await new Promise(resolve => setTimeout(resolve, 1000));
if (attempts >= totalAttemptsLimit) {
throw new Error('تمام تلاشها ناموفق بود.');
}
}
}
}
}
async function loadApiKeys() {
const possiblePaths = [
'/omdbKeys.json',
'/../omdbKeys.json'
];
for (const path of possiblePaths) {
try {
const response = await fetch(path);
if (!response.ok) {
console.warn(`خطا در بارگذاری از ${path}: ${response.status}`);
continue;
}
const keys = await response.json();
console.log(`فایل کلیدها از ${path} با موفقیت بارگذاری شد.`);
return new ApiKeySwitcher(keys);
} catch (error) {
console.warn(`خطا در مسیر ${path}: ${error.message}`);
}
}
console.error('هیچ فایل کلید API پیدا نشد.');
return new ApiKeySwitcher(['38fa39d5']); // کلید پیشفرض
}