JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 399
  • Score
    100M100P100Q109879F

A lovely little Node.js module to perform batch requests with the Google REST API

Package Exports

  • batchelor

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (batchelor) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Batchelor

A lovely little Node.js module to perform batch requests with the Google REST API. Simply does it.

Batchelor

Google API Batch Requests

This is a project to solve a need for a missing feature in the wonderfully epic and useful google/google-api-nodejs-client. Currently, this cannot be used to post media and has not been tested with posting anything but JSON.

In theory this library could be used with other APIs, but has only been tested with Google's APIs as that's what we need it for.

Feel free to get involved in development.

Installation

This library has also been distributed on npm. Install it with the following command:

$ npm install batchelor --save

How to Use

GET Requests

var Batchelor = require('batchelor');

Once the module has been included, we initialise it with all our default options:

Batchelor.init({
    'uri':'https://www.googleapis.com/batch',
    'method':'POST',
    'auth': {
        'bearer': [... Google API Token ...]
    },
    'headers': {
        'Content-Type': 'multipart/mixed;'
    }
});

We can then start adding requests to our batch. This can be done 2 ways:

As a one-off object:

Batchelor.add({
    'method':'GET',
    'path':'/plusDomains/v1/people/me/activities/user'
})

Or an Array of objects:

Batchelor.add([
    {
        'method':'GET',
        'path':'/plusDomains/v1/people/me/activities/user'
    },
    {
        'method':'GET',
        'path':'/plusDomains/v1/people/me/circles'
    },
    {
        'method':'GET',
        'path':'/plusDomains/v1/people/me/people/circled'
    }
]);

Once you have added all of the requests you need, call .run():

Batchelor.run(function(response){
    res.json(response);
});

POST Requests

The above examples show GET requests. To perform a POST requires a few more settings:

Batchelor.add({
    'method':'POST',
    'path':'/plusDomains/v1/people/me/activities',
    'parameters':{
        'Content-Type':'application/json;',
        'body':{'object':{'originalContent': 'A wonderful batch post!'},'access': {'items': [{'type': 'domain'}],'domainRestricted': true}}
    }
});

Callbacks

By default, all responses are returned through the callback function in the Batchelor.run() call. Alternatively, a callback can be supplied for each individual calls:

Batchelor.add({
    'method':'POST',
    'path':'/plusDomains/v1/people/me/activities',
    'parameters':{
        'Content-Type':'application/json;',
        'body':{'object':{'originalContent': 'Another wonderful batch post with callback!'},'access': {'items': [{'type': 'domain'}],'domainRestricted': true}}
    },
    'callback':function(response){
        console.log(response);
    }
});

Request and Response IDs

The module will assign a request a randomly generated unique Content-ID by default, but this can be supplied as part of the options to supply Batchelor.add():

Batchelor.add({
    'method':'GET',
    'path':'/plusDomains/v1/people/me/activities/user',
    'requestId':'Batch_UniqueID_1'
})

A Little Gift

All methods return the Batchelor object. So you can chain calls together.

Batchelor.init({
    ...
}).add([
    ...
]).run(function(data){
    ...
});

To Do List-ish

These might get done if we end up needing them/have time:

  • Limit requests per batch request
  • Handle Media in API calls (no need for it here, feel free)

Release History

  • 0.0.1 Initial release

Acknowledgement

Built on the clock by @sparkyfied as an internal project of wapisasa C.I.C.