-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspotifyManager.ts
47 lines (33 loc) · 1.54 KB
/
spotifyManager.ts
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
import { RestManager } from "./RestManager";
import { SpotifyOptions } from "./Spotify";
const errorMessage:string = "Poru Spotify] your all spotify clientID are ratelimited try to add more clientID due to you have high usage"
export class SpotifyManager {
private readonly mode: 'single' | 'multiple' = 'single';
private manager : RestManager[];
constructor(data:SpotifyOptions){
this.manager =[]
if(data.clients.length){
for (const client of data.clients) this.manager?.push(new RestManager(client));
this.mode = 'multiple';
} else {
this.manager.push(new RestManager({ clientID: data.clientID, clientSecret: data.clientSecret }));
}
}
send<T>(endpoint: string):Promise<T>{
if(this.mode ==="single") return this.manager[0].request(endpoint);
const manager = this.getLeastUsedRequest() as RestManager | undefined;
if(!manager) throw new Error(errorMessage)
return manager.request(endpoint);
}
getData<T>(endpoint: string):Promise<T>{
if(this.mode ==="single") return this.manager[0].request(endpoint);
const manager = this.getLeastUsedRequest() as RestManager | undefined;
if(!manager) throw new Error(errorMessage)
return manager.getData(endpoint);
}
protected getLeastUsedRequest(): RestManager | undefined {
const manager = this.manager.filter((request) => !request.stats.isRateLimited);
if (!manager.length) return undefined;
return manager.sort((a, b) => a.stats.requests - b.stats.requests)[0];
}
}