Skip to content

This is a library for running Batch Requests for Google APIs using Javascript.

License

Notifications You must be signed in to change notification settings

tanaikech/BatchRequest_js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

BatchRequest_js

MIT License

Overview

This is a library for running Batch Requests for Google APIs using Javascript.

Description

When users use Google's APIs, one quota is used for one API call. When the batch request is used, several APIs can be called by one quota, although there are some limitations in the batch request.

google-api-javascript-client can run the batch request. Ref But, I created this for my self study. This library can achieve the batch request using fetch without using google-api-javascript-client.

Install

<script src="batchrequest_js.min.js"></script>

Or, using jsdelivr cdn

<script src="https://cdn.jsdelivr.net/gh/tanaikech/BatchRequest_js@master/batchrequest_js.min.js"></script>

Usage

Method: Do()

When this method is used, the raw values and the parsed values from the batch requests are returned. And also, the number of requests more than 100 can be used. The maximum number of requests is 100. But, the split of the number of requests is processed for the limitation of 100. This sample script renames 2 files using update of Drive API v3.

const object = {
  batchPath: "batch/drive/v3", // batch path. This will be introduced in the near future.
  accessToken: token, // Required
  requests: [
    {
      method: "PATCH",
      endpoint: "https://www.googleapis.com/drive/v3/files/###?fields=name",
      requestBody: { name: "sample1" },
      accessToken: "###", // If this key is used, this access token is used for this request.
    },
    {
      method: "PATCH",
      endpoint: "https://www.googleapis.com/drive/v3/files/###?fields=name",
      requestBody: { name: "sample2" },
    },
  ],
};
Do(object)
  .then((e) => console.log(e))
  .catch((err) => console.log(err));
  • In the current stage, batchPath is required to be used. batchPath can be retrieved by Discovery.

  • If accessToken is used in the object of requests, the accessToken is used for the individual request in the batch request. If accessToken is not used in the requests, this library uses object.accessToken for the whole batch request.

  • In this method, the raw values and the parsed values from the batch requests are returned as follows.

    { "parsed": [, , ,], "raw": "###" }
  • requests
    • batchPath: batchPath can be retrieved by Discovery.
    • method: GET, POST, PUT, PATCH, DELETE and so on. Please set this for the API you want to use.
    • endpoint: Endpoint of the API you want to use.
    • requestBody: Request body of the API you want to use. This library for Google APIs. So in this case, the request body is sent as JSON.

Limitations for batch request

There are some limitations for the batch request.


Licence

MIT

Author

Tanaike

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

Update History

  • v1.0.0 (December 22, 2020)

    1. Initial release.

TOP

About

This is a library for running Batch Requests for Google APIs using Javascript.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published