@@ -22,7 +22,7 @@ const getDeepLink = (method, server, ...params) => {
2222
2323
2424const login = ( username , password ) => {
25- const response = http . post ( `${ data . server } /api/v1/login` , {
25+ const response = postWithRetry ( `${ data . server } /api/v1/login` , {
2626 headers : {
2727 'Content-Type' : 'application/json'
2828 } ,
@@ -44,7 +44,7 @@ const createUser = (customProps) => {
4444
4545 login ( output . account . adminUser , output . account . adminPassword ) ;
4646
47- http . post ( `${ data . server } /api/v1/users.create` , {
47+ postWithRetry ( `${ data . server } /api/v1/users.create` , {
4848 headers : {
4949 'Content-Type' : 'application/json' ,
5050 ...headers
@@ -74,19 +74,20 @@ const deleteCreatedUser = async ({ username: usernameToDelete }) => {
7474 try {
7575 login ( output . account . adminUser , output . account . adminPassword ) ;
7676
77- const result = http . get ( `${ data . server } /api/v1/users.info?username=${ usernameToDelete } ` , {
77+ const result = getWithRetry ( `${ data . server } /api/v1/users.info?username=${ usernameToDelete } ` , {
7878 headers : {
7979 'Content-Type' : 'application/json' ,
8080 ...headers
8181 }
8282 } ) ;
8383
8484 const userId = json ( result . body ) ?. data ?. user ?. _id ;
85- http . post ( `${ data . server } /api/v1/users.delete` , { userId , confirmRelinquish : true } , {
85+ postWithRetry ( `${ data . server } /api/v1/users.delete` , {
8686 headers : {
8787 'Content-Type' : 'application/json' ,
8888 ...headers
89- }
89+ } ,
90+ body : JSON . stringify ( { userId, confirmRelinquish : true } )
9091 } ) ;
9192 } catch ( error ) {
9293 console . log ( JSON . stringify ( error ) ) ;
@@ -98,7 +99,7 @@ const createRandomTeam = (username, password) => {
9899
99100 const teamName = output . randomTeamName ( ) ;
100101
101- http . post ( `${ data . server } /api/v1/teams.create` , {
102+ postWithRetry ( `${ data . server } /api/v1/teams.create` , {
102103 headers : {
103104 'Content-Type' : 'application/json' ,
104105 ...headers
@@ -113,7 +114,7 @@ const createRandomRoom = (username, password, type = 'c') => {
113114 login ( username , password ) ;
114115 const room = `room${ output . random ( ) } ` ;
115116
116- const response = http . post ( `${ data . server } /api/v1/${ type === 'c' ? 'channels.create' : 'groups.create' } ` , {
117+ const response = postWithRetry ( `${ data . server } /api/v1/${ type === 'c' ? 'channels.create' : 'groups.create' } ` , {
117118 headers : {
118119 'Content-Type' : 'application/json' ,
119120 ...headers
@@ -133,7 +134,7 @@ const sendMessage = (username, password, channel, msg, tmid) => {
133134 login ( username , password ) ;
134135 const channelParam = tmid ? { roomId : channel } : { channel } ;
135136
136- const response = http . post ( `${ data . server } /api/v1/chat.postMessage` , {
137+ const response = postWithRetry ( `${ data . server } /api/v1/chat.postMessage` , {
137138 headers : {
138139 'Content-Type' : 'application/json' ,
139140 ...headers
@@ -153,7 +154,7 @@ const sendMessage = (username, password, channel, msg, tmid) => {
153154const getProfileInfo = ( userId ) => {
154155 login ( output . account . adminUser , output . account . adminPassword ) ;
155156
156- const result = http . get ( `${ data . server } /api/v1/users.info?userId=${ userId } ` , {
157+ const result = getWithRetry ( `${ data . server } /api/v1/users.info?userId=${ userId } ` , {
157158 headers : {
158159 'Content-Type' : 'application/json' ,
159160 ...headers
@@ -168,7 +169,7 @@ const getProfileInfo = (userId) => {
168169const post = ( endpoint , username , password , body ) => {
169170 login ( username , password ) ;
170171
171- const response = http . post ( `${ data . server } /api/v1/${ endpoint } ` , {
172+ const response = postWithRetry ( `${ data . server } /api/v1/${ endpoint } ` , {
172173 headers : {
173174 'Content-Type' : 'application/json' ,
174175 ...headers
@@ -182,7 +183,7 @@ const post = (endpoint, username, password, body) => {
182183const createDM = ( username , password , otherUsername ) => {
183184 login ( username , password ) ;
184185
185- const result = http . post ( `${ data . server } /api/v1/im.create` , {
186+ const result = postWithRetry ( `${ data . server } /api/v1/im.create` , {
186187 headers : {
187188 'Content-Type' : 'application/json' ,
188189 ...headers
@@ -208,6 +209,48 @@ function logAccounts() {
208209 console . log ( JSON . stringify ( data . accounts ) ) ;
209210}
210211
212+ const sleep = ( ms ) => {
213+ const start = Date . now ( ) ;
214+ while ( Date . now ( ) - start < ms ) { }
215+ }
216+
217+ const retryRequest = ( fn , {
218+ retries = 3 ,
219+ delay = 1000 ,
220+ factor = 2
221+ } = { } ) => {
222+ let lastError ;
223+ for ( let attempt = 1 ; attempt <= retries ; attempt ++ ) {
224+ try {
225+ const response = fn ( ) ;
226+
227+ if ( response && response . status >= 200 && response . status < 300 ) {
228+ return response ;
229+ }
230+
231+ if ( response && response . status >= 400 && response . status < 500 ) {
232+ throw new Error ( `Non-retryable error ${ response . status } ` ) ;
233+ }
234+
235+ lastError = new Error ( `HTTP ${ response ? response . status : 'unknown' } ` ) ;
236+ } catch ( err ) {
237+ lastError = err ;
238+ }
239+
240+ if ( attempt < retries ) {
241+ const wait = delay * Math . pow ( factor , attempt - 1 ) ;
242+ console . log ( `Retry ${ attempt } /${ retries } after ${ wait } ms` ) ;
243+ sleep ( wait ) ;
244+ }
245+ }
246+
247+ throw lastError ;
248+ } ;
249+
250+ const postWithRetry = ( url , options ) => retryRequest ( ( ) => http . post ( url , options ) ) ;
251+
252+ const getWithRetry = ( url , options ) => retryRequest ( ( ) => http . get ( url , options ) ) ;
253+
211254output . utils = {
212255 createUser,
213256 createUserWithPasswordChange,
0 commit comments