Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Google Appscript / UrlFetchApp.fetchAll() function run synchronously?

The problem I'm running into is that I have a number of API requests I want to make, all of which take several seconds to return because the size of the data being returned, and UrlFetchApp.fetchAll(..) is simply returning an array of empty JS objects, eg: [{}, {}, {}, ...].

My requests array look something like this (formatted for clarity):

requests = [
  {
    validateHttpsCertificates: false,
    url: 'https://url.com/api/v2/api_key/endpoint/action?params1=false&params2=true'
  },
  {
    validateHttpsCertificates: false,
    url: 'https://url.com/api/v2/api_key/endpoint/action?params3=false&params4=true'
  }
];

The code to make my requests:

responses = UrlFetchApp.fetchAll(requests);

// returns back '[{}, {}]'
console.log(JSON.stringify(responses));

I can confirm through the Database that the API calls are being run, since AWS RDS Performance Metrics show the DB queries run, and I can also confirm that the API itself is responding with 200's via NewRelic, which is why my hunch is that I'm not using GAS/UrlFetchApp.fetchAll() correctly.

So, I am wondering:

  1. Does GAS run synchronously, aka, it'll wait for .fetchAll() to return before running the console.log(...) line?
  2. Am I actually calling fetchAll correctly? Currently at a loss, and the Google Appscript documentation is meager at best.

Thank you in advance for the help.

EDIT:

I migrated to fetchAll after I successfully used fetch, eg:

// synchronously fetching one by one
requests.map(request => UrlFetchAll.fetch(request.url, { validateHttpsCertificates: false });
like image 751
Stu Avatar asked Nov 12 '19 23:11

Stu


1 Answers

How about this answer?

Answer for Question 1:

The fetchAll method works with the asynchronous processing. Ref If you want to use UrlFetchApp with the synchronous processing, please use UrlFetchApp.fetch() in a loop.

Answer for Question 2:

I think that your request for the fetchAll method is correct. In order to retrieve the responses from UrlFetchApp.fetchAll(requests), how about the following modification?

Modified script:

var responses = UrlFetchApp.fetchAll(requests);
var res = responses.map(function(e) {return e.getContentText()});
console.log(JSON.stringify(res)); //  or Logger.log(JSON.stringify(res));
  • In this modification, getContentText() is used for each response.
  • The order of responses is the same with the order of requests.

References:

  • getContentText()
  • fetchAll(requests)
  • Benchmark: fetchAll method in UrlFetch service for Google Apps Script

If I misunderstood your question and this was not the result you want, I apologize.

like image 123
Tanaike Avatar answered Oct 29 '22 11:10

Tanaike