Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stagger Requests #41

Open
KevinHaeusler opened this issue Dec 29, 2021 · 1 comment
Open

Stagger Requests #41

KevinHaeusler opened this issue Dec 29, 2021 · 1 comment

Comments

@KevinHaeusler
Copy link

KevinHaeusler commented Dec 29, 2021

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
Copy link
Author

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)
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant