Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active October 1, 2022 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/a970943f91c0dd0955d1722743e560df to your computer and use it in GitHub Desktop.
Save tanaikech/a970943f91c0dd0955d1722743e560df to your computer and use it in GitHub Desktop.
Retrieving Batch Path for Batch Requests using Google Apps Script

Retrieving Batch Path for Batch Requests using Google Apps Script

This is a sample script for retrieving the batch path for using the batch requests using Google Apps Script.

After August 12, 2020, in order to use batch requests, the batch path is required to be used to the endpoint of the batch requests. And, the batch path is sometimes updated. So, when a constant batch path has been continued to be used, this might lead to the reason for an error. In this sample script, the batch path is retrieved from Discovery API. By this, the latest batch path can be always obtained.

Sample script:

/**
 * @param {object} name Name of Google API and Version of Google API.
 * @return {string} batch path
 */
function getBatchPath_({ name, version = "" }) {
  if (!name) {
    throw new Error("Please set API name you want to search.");
  }
  const url = `https://www.googleapis.com/discovery/v1/apis?preferred=${
    version ? "false" : "true"
  }&name=${encodeURIComponent(name.toLowerCase())}`;
  const res1 = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
  if (res1.getResponseCode() != 200) {
    throw new Error("Batch path cannot be found.");
  }
  const obj1 = JSON.parse(res1.getContentText());
  if (!obj1.items) {
    throw new Error("Batch path cannot be found.");
  }
  const { discoveryRestUrl } =
    (version.toString() == ""
      ? obj1.items[0]
      : obj1.items.find((e) => e.version == version)) || {};
  if (!discoveryRestUrl) {
    throw new Error("Batch path cannot be found.");
  }
  const res2 = UrlFetchApp.fetch(discoveryRestUrl, {
    muteHttpExceptions: true,
  });
  if (res2.getResponseCode() != 200) {
    throw new Error("Batch path cannot be found.");
  }
  const obj2 = JSON.parse(res2);
  const { batchPath } = obj2;
  return batchPath;
}
function sample1() {
  const name = "drive"; // Please set API name. Ex. drive, calendar and so on.
  const batchPath = getBatchPath_({ name });
  console.log(batchPath); // <--- batch/drive/v3
}

function sample2() {
  const name = "drive"; // Please set API name. Ex. drive, calendar and so on.
  const version = "v2"; // If you want to retrieve the batch path of the specific version. Please set this. If this is empty, the batch path of the latest version of the API.
  const batchPath = getBatchPath_({ name, version });
  console.log(batchPath); // <--- batch/drive/v2
}
  • At sample1, the batch path of Drive API v3 is retrieved. When version is not used, the latest version is used.
  • At sample2, the batch path of Drive API v2 is retrieved.

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment