IMPORTJSONAPI icon indicating copy to clipboard operation
IMPORTJSONAPI copied to clipboard

Stagger Requests

Open KevinHaeusler opened this issue 3 years ago • 1 comments

Is there a way to stagger requests correctly? For example I have 140 total requests in my google sheet and I want to only do 14 requests/s

Would something like this work?

Utilities.sleep(waitTime);


function do_fetch_(url, params) {
  if (params['contentType'] === "application/json" && typeof params['payload'] === 'object' ) {
     params['payload'] = JSON.stringify(params['payload'])
  }

const waitTime = Math.floor(Math.random() * 10);
Utilities.sleep(waitTime);
  
  var response = UrlFetchApp.fetch(url, params)
  return JSON.parse(response.getContentText());
}

KevinHaeusler avatar Dec 29 '21 19:12 KevinHaeusler

I found this solution to work quite well with the API im querying (from a guy in the api discord)

function do_fetch_(url, params) {
  if (params['contentType'] === "application/json" && typeof params['payload'] === 'object' ) {
     params['payload'] = JSON.stringify(params['payload'])
  }

  while (true) {
    try {
      var response = UrlFetchApp.fetch(url, params)
      return JSON.parse(response.getContentText());
    } catch (ex) {
      Utilities.sleep(100)
    }
  }
}

KevinHaeusler avatar Dec 29 '21 19:12 KevinHaeusler