@@ -25,6 +25,12 @@ const getCircularReplacer = () => {
25
25
* APIs in the same uniform manner.
26
26
*/
27
27
export default class GSheetService {
28
+ constructor ( ) {
29
+ this . getSheetAPI = this . getSheetAPI . bind ( this ) ;
30
+ this . addToSheetAPI = this . getSheetAPI . bind ( this ) ;
31
+ this . addToSheet = this . getSheetAPI . bind ( this ) ;
32
+ }
33
+
28
34
/**
29
35
* getSheet
30
36
* @param {Object } req the request
@@ -45,7 +51,13 @@ export default class GSheetService {
45
51
rows : JSON . parse ( rowsJson ) ,
46
52
} ) ;
47
53
} catch ( e ) {
48
- res . status ( ( e . response && e . response . status ) || 500 ) ;
54
+ const status = ( e . response && e . response . status ) || 500 ;
55
+ if ( status === 429 ) {
56
+ // rate limit issue - wait 15sec and retry
57
+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
58
+ return this . getSheetAPI ( req , res ) ;
59
+ }
60
+ res . status ( status ) ;
49
61
return res . send ( ( e . response && e . response . data ) || { ...e , message : e . message } ) ;
50
62
}
51
63
}
@@ -65,7 +77,7 @@ export default class GSheetService {
65
77
// set credentials for working
66
78
await doc . useServiceAccountAuth ( {
67
79
client_email : config . GOOGLE_SERVICE_ACCOUNT_EMAIL ,
68
- private_key : config . GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY . replace ( new RegExp ( '\\\\n' , 'g' ) , '\n' ) ,
80
+ private_key : config . GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY . replace ( / \\ m / g , '\n' ) ,
69
81
} ) ;
70
82
// load doc infos
71
83
await doc . loadInfo ( ) ;
@@ -77,7 +89,13 @@ export default class GSheetService {
77
89
rows : JSON . parse ( rowsJson ) ,
78
90
} ) ;
79
91
} catch ( e ) {
80
- res . status ( ( e . response && e . response . status ) || 500 ) ;
92
+ const status = ( e . response && e . response . status ) || 500 ;
93
+ if ( status === 429 ) {
94
+ // rate limit issue - wait 15sec and retry
95
+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
96
+ return this . addToSheetAPI ( req , res ) ;
97
+ }
98
+ res . status ( status ) ;
81
99
return res . send ( ( e . response && e . response . data ) || { ...e , message : e . message } ) ;
82
100
}
83
101
}
@@ -105,6 +123,12 @@ export default class GSheetService {
105
123
const rowsJson = JSON . stringify ( moreRows , getCircularReplacer ( ) ) ;
106
124
return rowsJson ;
107
125
} catch ( e ) {
126
+ const status = ( e . response && e . response . status ) || 500 ;
127
+ if ( status === 429 ) {
128
+ // rate limit issue - wait 15sec and retry
129
+ await new Promise ( resolve => setTimeout ( resolve , 15000 ) ) ;
130
+ return this . addToSheet ( id , payload , index ) ;
131
+ }
108
132
return e ;
109
133
}
110
134
}
0 commit comments