@@ -337,62 +337,42 @@ Api.prototype = {
337
337
} ;
338
338
339
339
function setRateLimits ( self , headers ) {
340
- if ( self . rateLimits === undefined || self . rateLimits . length === 0 ) {
341
- self . rateLimits = [
342
- {
343
- minutesInterval : 15 ,
344
- callsMade : 0 ,
345
- callsRemaining : 0 ,
346
- resetTimeMillis : 0
347
- } ,
348
- {
349
- minutesInterval : 30 ,
350
- callsMade : 0 ,
351
- callsRemaining : 0 ,
352
- resetTimeMillis : 0
353
- } ,
354
- {
355
- minutesInterval : 60 ,
356
- callsMade : 0 ,
357
- callsRemaining : 0 ,
358
- resetTimeMillis : 0
359
- } ,
360
- {
361
- minutesInterval : 24 * 60 ,
362
- callsMade : 0 ,
363
- callsRemaining : 0 ,
364
- resetTimeMillis : 0
365
- }
366
- ] ;
367
- }
368
-
340
+ self . rateLimits = [ ] ;
369
341
if ( headers !== undefined ) {
370
- try {
371
- if ( headers [ 'x-ratelimit' ] !== undefined && headers [ 'x-ratelimit' ] . split ( ', ' ) . length > 0 ) {
372
- const rateLimit = headers [ 'x-ratelimit' ] . split ( ', ' ) . map ( Number ) ;
373
- self . rateLimits [ 0 ] . callsMade = rateLimit [ 0 ] ;
374
- self . rateLimits [ 1 ] . callsMade = rateLimit [ 1 ] ;
375
- self . rateLimits [ 2 ] . callsMade = rateLimit [ 2 ] ;
376
- self . rateLimits [ 3 ] . callsMade = rateLimit [ 3 ] ;
377
- }
378
-
379
- if ( headers [ 'x-ratelimit-remaining' ] !== undefined && headers [ 'x-ratelimit-remaining' ] . split ( ', ' ) . length > 0 ) {
380
- const remaining = headers [ 'x-ratelimit-remaining' ] . split ( ', ' ) . map ( Number ) ;
381
- self . rateLimits [ 0 ] . callsRemaining = remaining [ 0 ] ;
382
- self . rateLimits [ 1 ] . callsRemaining = remaining [ 1 ] ;
383
- self . rateLimits [ 2 ] . callsRemaining = remaining [ 2 ] ;
384
- self . rateLimits [ 3 ] . callsRemaining = remaining [ 3 ] ;
385
- }
342
+ let rateLimitReset = headers [ 'x-ratelimit-reset' ] ;
343
+ let rateLimitRemaining = headers [ 'x-ratelimit-remaining' ] ;
344
+ let rateLimitMade = headers [ 'x-ratelimit' ] ;
345
+
346
+ if ( rateLimitReset !== undefined && rateLimitRemaining !== undefined && rateLimitMade !== undefined ) {
347
+ rateLimitReset = rateLimitReset . split ( "," ) ;
348
+ rateLimitRemaining = rateLimitRemaining . split ( "," ) ;
349
+ rateLimitMade = rateLimitMade . split ( "," ) ;
350
+
351
+ if ( rateLimitReset . length === rateLimitRemaining . length && rateLimitReset . length === rateLimitMade . length ) {
352
+ const currentTime = Math . floor ( Date . now ( ) / 1000 ) ;
353
+ for ( let i = 0 ; i < rateLimitReset . length ; i ++ ) {
354
+ const numberOfMinutes = ( parseInt ( rateLimitReset [ i ] ) - currentTime ) / 60 ;
355
+ const rateLimit = {
356
+ resetTimeMillis : parseInt ( rateLimitReset [ i ] ) ,
357
+ callsRemaining : parseInt ( rateLimitRemaining [ i ] ) ,
358
+ callsMade : parseInt ( rateLimitMade [ i ] )
359
+ } ;
360
+
361
+ if ( numberOfMinutes <= 15 ) {
362
+ rateLimit . minutesInterval = 15 ;
363
+ } else if ( numberOfMinutes <= 30 ) {
364
+ rateLimit . minutesInterval = 30 ;
365
+ } else if ( numberOfMinutes <= 60 ) {
366
+ rateLimit . minutesInterval = 60 ;
367
+ } else if ( numberOfMinutes <= 60 * 24 ) {
368
+ rateLimit . minutesInterval = 60 * 24 ;
369
+ }
386
370
387
- if ( headers [ 'x-ratelimit-reset' ] !== undefined && headers [ 'x-ratelimit-reset' ] . split ( ', ' ) . length > 0 ) {
388
- const limitReset = headers [ 'x-ratelimit-reset' ] . split ( ', ' ) . map ( Number ) ;
389
- self . rateLimits [ 0 ] . resetTimeMillis = limitReset [ 0 ] ;
390
- self . rateLimits [ 1 ] . resetTimeMillis = limitReset [ 1 ] ;
391
- self . rateLimits [ 2 ] . resetTimeMillis = limitReset [ 2 ] ;
392
- self . rateLimits [ 3 ] . resetTimeMillis = limitReset [ 3 ] ;
371
+ self . rateLimits . push ( rateLimit ) ;
372
+ }
373
+ } else {
374
+ console . log ( "Could not set rate limits: headers length should be the same" ) ;
393
375
}
394
- } catch ( exception ) {
395
- console . log ( "Something went wrong while parsing rate limits." ) ;
396
376
}
397
377
}
398
378
}
0 commit comments