gbatchrequests

1.0.0 • Public • Published

node-gbatchrequests

MIT License

Overview

This is a Node.js module to run the batch requests of Google APIs.

Description

In Google APIs, there are APIs where batch requests can be run. The batch requests can run multiple API calls by one API call with the asynchronous process. By this, both the process cost and the quota cost can be reduced. Ref In Node.js, the wonderful module of googleapis for Node.js is existing. But, in the current stage, unfortunately, it seems that the googleapis for Node.js cannot run the batch requests. Ref So, I created this module. This module can achieve batch requests with Node.js. In order to run batch requests, the access token retrieved from googleapis for Node.js can be used.

Install

$ npm install --save-dev gbatchrequests

or

$ npm install --global gbatchrequests

You can also see this module at https://www.npmjs.com/package/gbatchrequests.

Method

Method Explanation
RunBatch(obj) Run batch requests of Google APIs.
GetBatchPath(obj) Get batch path.

This library uses HTTPS for requesting Google APIs.

Usage

About the authorization, please check the section of Authorization.

Scopes

This library requests your inputted requests as batch requests. So, the requirement scopes depend on the Google APIs you want to use. So, please check the official document of the API you want to use.

1. RunBatch(obj)

This is the main method of this library. This method runs the batch requests of Google APIs.

Sample script

This is a sample script for changing the filename of a file on Google Drive using batch requests.

const { RunBatch } = require("gbatchrequests");

const fileId = "###"; // Please set the file ID.
const obj = {
  accessToken: "###", // Please set your access token.
  api: { name: "drive", version: "v3" },
  requests: [
    {
      method: "PATCH",
      endpoint: `https://www.googleapis.com/drive/v3/files/${fileId}`,
      requestBody: { name: "sample" },
    },
  ],
};
RunBatch(obj)
  .then((res) => console.log(res))
  .catch((err) => console.error(err));
  • accessToken: (Required) Your access token. In this case, please include the scopes you want to use.
  • api: { name: "###", version: "###" }: (Required) Please set the API name and the API version you want to use. For example, when Drive API is used, it's api: { name: "drive", version: "v3" }. When version is not used, the latest version is automatically used.
  • requests: (Required) Please set method, endpoint, and requestBody of Google API you want to use. For example, when you want to change the filename of the file on Google Drive, the above sample script can be used.
  • skipError: (Option) When this is true, even when an error occurs in the request, the error is skipped. The default is false. So, when an error occurs, the script is stopped.
  • returnRawData: (Option) In the case of batch requests, the returned values are text data as the default. When this option is true, the returned value is not parsed. Namely, you can retrieve the raw text data. The default is false. So, as the default, the returned values are parsed.

2. GetBatchPath(obj)

Get batch path. The batch path is required to be used from August 12, 2020. Ref For example, when Drive API is used with the batch requests, the batch path is batch/drive/v3.

Sample script

In this case, batch/drive/v3 is obtained as the response value.

const { GetBatchPath } = require("gbatchrequests");

const obj = { api: { name: "drive", version: "v3" } };
GetBatchPath(obj)
  .then((res) => console.log(res))
  .catch((err) => console.error(err));
  • api: { name: "###", version: "###" }: (Required) Please set the API name and the API version you want to use. For example, when Drive API is used, it's api: { name: "drive", version: "v3" }. When version is not used, the latest version is automatically used.

Limitations for batch request

There are some limitations to the batch request.

About how to retrieve access token

Retrieving access token from OAuth2 with Quickstart

When Quickstart of Node.js for Drive API is seen, you can see const drive = google.drive({version: 'v3', auth: authClient});. Ref In this sample, the access token is retrieved from authClient.

const accessToken = authClient.credentials.access_token;

Retrieving access token from Service account

When the access token is retrieved from the service account, you can retrieve the access token using the following script.

const accessToken = await new google.auth.GoogleAuth({
  keyFile: "### file path of service account credential file ###",
  scopes: ["### Scopes you want to use ###"],
}).getAccessToken();

Licence

MIT

Author

Tanaike

If you have any questions and commissions for me, feel free to tell me.

Update History

  • v1.0.0 (October ##, 2022)

    1. Initial release.

TOP


Package Sidebar

Install

npm i gbatchrequests

Weekly Downloads

13

Version

1.0.0

License

MIT

Unpacked Size

15.9 kB

Total Files

5

Last publish

Collaborators

  • tanaike