Git Product home page Git Product logo

batchrequest's Introduction

BatchRequest

MIT License

Overview

This is a library for running Batch Requests using Google Apps Script (GAS).

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. For example, in GAS, Drive API can be used be DriveApp. In this case, the quota is not used for using Drive API. (When Drive of Advanced Google Services is used, the quota is used.) But this is Drive API v2. If users want to use Drive API v3, it is required to directly request each endpoint of Drive API v3. The batch request is much useful for this situation. However, it is a bit difficult for users to use the batch request. Because the batch request is requested by multipart/mixed. I thought that the script may become a bit complicated, because of the request of multipart/mixed using UrlFetchApp. And although I had been looking for the libraries for the batch request, I couldn't find them. So I created this.

Sample scripts

Library's project key

1HLv6tWz0oXFOJHerBTP8HsNmhpRqssijJatC92bv9Ym6HSN69_UuzcDk

How to install

In order to use this library, please install this library.

  1. Install BatchRequest library.
    • Library's project key is 1HLv6tWz0oXFOJHerBTP8HsNmhpRqssijJatC92bv9Ym6HSN69_UuzcDk.
  2. For APIs you want to use, please enable the APIs at API console.
    • Recently, when it enabled APIs, I had an experience that I was made to wait for several minutes for enabling APIs. So when you enabled APIs at API console, if the error related to APIs occurs, please run again after several minutes.

About scopes

About the install of scopes using at this library, users are not required to install scopes. Because this library can automatically install the required scopes to the project which installed this library. The detail information about this can be seen at here.

  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/drive.scripts
  • https://www.googleapis.com/auth/script.external_request
  • https://mail.google.com/
  • https://www.googleapis.com/auth/calendar

IMPORTANT: Above 5 scopes are installed in this library. If you want to use APIs except for Calendar API, Drive API and Gmail API, please install the scopes for the APIs using Manifests to the project installed this library. Also there is a GAS library for managing Manifests.

Methods

Method Description
Do(object) This is a simple method for the batch request. The maximum number of requests is 100. The raw values from the batch request are returned.
EDo(object) This method is the enhanced Do() method. When this method is used, the result values from the batch requests are parsed. And also, the number of requests more than 100 can be used. In this case, the split of the number of requests is processed for the limitation of 100.

Usage

Method: Do()

A sample script is as follows. This sample script renames 2 files using update of Drive API v3.

var requests = {
  batchPath: "batch/drive/v3", // batch path. This will be introduced in the near future.
  requests: [
    // In this method, this the maximum number of requests should be 100.
    {
      method: "PATCH",
      endpoint:
        "https://www.googleapis.com/drive/v3/files/### file ID1 ###?fields=name",
      requestBody: { name: "sample1" },
      accessToken: ScriptApp.getOAuthToken(), // If this key is used, this access token is used.
    },
    {
      method: "PATCH",
      endpoint:
        "https://www.googleapis.com/drive/v3/files/### file ID2 ###?fields=name",
      requestBody: { name: "sample2" },
    },
  ],
};
var result = BatchRequest.Do(requests); // Using this library
Logger.log(result);
  • batchPath will be introduced in the near future. But you have already been able to use this. 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 ScriptApp.getOAuthToken() for the whole batch request.

Method: EDo()

A sample script is as follows. This sample script renames 2 files using update of Drive API v3.

var requests = {
  //   useFetchAll: true, // When "useFetchAll" is true, the request is run with fetchAll method. The default is false.
  batchPath: "batch/drive/v3", // batch path. This will be introduced in the near future.
  requests: [
    // In this method, this the number of requests can be used more than 100.
    {
      method: "PATCH",
      endpoint:
        "https://www.googleapis.com/drive/v3/files/### file ID1 ###?fields=name",
      requestBody: { name: "sample1" },
      accessToken: ScriptApp.getOAuthToken(), // If this key is used, this access token is used.
    },
    {
      method: "PATCH",
      endpoint:
        "https://www.googleapis.com/drive/v3/files/### file ID2 ###?fields=name",
      requestBody: { name: "sample2" },
    },
  ],
};
var result = BatchRequest.EDo(requests); // Using this library
Logger.log(result);
  • In this method, the result values from the batch requests are parsed, and you can retrieve the result values as an array object.
  • batchPath will be introduced in the near future. But you have already been able to use this. 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 ScriptApp.getOAuthToken() for the whole batch request.

  • 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.
    • useFetchAll: When "useFetchAll" is true, the request is run with fetchAll method. The default is false. For example, when 200 batch requests are used, when useFetchAll: true is used, 2 requests which have 100 batch requests are run with the asynchronous process using fetchAll method. Ref When useFetchAll: false is used, 2 requests which have 100 batch requests are run with the synchronous process using fetch method. Ref

Limitations for batch request

There are some limitations for the batch request.

Appendix

  • About the limitation of number of request for one batch request
  • RunAll
    • This is a library for running the concurrent processing using only native Google Apps Script (GAS).

Licence

MIT

Author

Tanaike

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

Update History

  • v1.0.0 (May 2, 2018)

    1. Initial release.
  • v1.1.0 (June 10, 2020)

    1. New method of EDo() was added. This method is the enhanced Do() method. When this method is used, the result values from the batch requests are parsed. And also, the number of requests more than 100 can be used. In this case, the split of the number of requests is processed for the limitation of 100.
  • v1.1.1 (June 12, 2020)

    1. Error handling for the input object was added.
  • v1.1.2 (June 12, 2020)

    1. Removed a bug that when the returned value is empty, an error occurred.

TOP

batchrequest's People

Contributors

tanaikech avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.