11import request from 'request-promise-native' ;
2- import url from 'url' ;
32import async from 'async' ;
43import crypto from 'crypto' ;
54import throttleFactory from 'throttled-queue' ;
6- const throttle = throttleFactory ( 10 , 1000 ) ;
75
86/**
97 * base model
108 */
119class base {
10+ static throttle ;
11+
12+ static init ( config ) {
13+ if ( base . throttle == undefined ) {
14+ base . throttle = throttleFactory ( config . data . throttleMaxRequestsPerInterval , config . data . throttleInterval ) ;
15+ }
16+ }
17+
18+
1219 /**
1320 * construct
1421 * @param config
1522 */
1623 constructor ( config ) {
24+ base . init ( config ) ;
1725 this . config = config ;
1826
1927 this . url = config . get ( 'url' ) . endsWith ( '/' ) ? config . get ( 'url' ) : `${ config . get ( 'url' ) } /` ;
@@ -37,7 +45,7 @@ class base {
3745
3846 data . private_token = this . token ;
3947
40- return new Promise ( ( resolve , reject ) => throttle ( ( ) => {
48+ return new Promise ( ( resolve , reject ) => base . throttle ( ( ) => {
4149 request . post ( `${ this . url } ${ path } ` , {
4250 json : true ,
4351 body : data ,
@@ -68,7 +76,7 @@ class base {
6876 let key = base . createDumpKey ( path , data ) ;
6977 if ( this . config . dump ) return this . getDump ( key ) ;
7078
71- return new Promise ( ( resolve , reject ) => throttle ( ( ) => {
79+ return new Promise ( ( resolve , reject ) => base . throttle ( ( ) => {
7280 request . post ( `${ path } ` , {
7381 json : true ,
7482 body : data ,
@@ -101,7 +109,7 @@ class base {
101109 path += ( path . includes ( '?' ) ? '&' : '?' ) + `private_token=${ this . token } ` ;
102110 path += `&page=${ page } &per_page=${ perPage } ` ;
103111
104- return new Promise ( ( resolve , reject ) => throttle ( ( ) => {
112+ return new Promise ( ( resolve , reject ) => base . throttle ( ( ) => {
105113 request ( `${ this . url } ${ path } ` , {
106114 json : true ,
107115 insecure : this . _insecure ,
0 commit comments