New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

batchelor

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batchelor

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

  • 0.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.4K
increased by11.83%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Issues, Features and Bugs

This has been created for an internal project of wapisasa so it might not fit everyone's requirements. It might also be buggy as it's an alpha release. Any issues, feature requests or bugs that need attention please let us know.

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 Couple of Little Gifts
Method Chaining

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

Batchelor.init({
	...
}).add([
	...
]).run(function(data){
	...
});
Data Pass-through

When passing options to the .add() you can include an Object called extend. In the case of providing a callback, this will be passed back as a second parameter. When using the default callback on the .run() call, an array of all data passed through will be added as a second parameter with the requestId as the key:

Batchelor.add({
	...
	'extend':{
		...
	},
	'callback':function(response, extendObj){
		console.log(response, extendObj);
	}
});

This could be required, for example, when making multiple requests with different Auth data and then needing to make further requests with the same Auth data.

Resetting and Re-using

Once Batchelor has been run, there are certain use-cases for running futher batch requests on response. This requires the variables in the module to be reset. This can be done using the .reset() call:

Batchelor.run(function(response){

	//	Reset Batchelor for further use
	Batchelor.reset();
	...

});

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 to write it)

Release History

  • 0.0.7 Reset function for when you are using batchelor more than once in a script (ability for nested requests too)
  • 0.0.6 Bug fixes introduced in the last update and clean up was happening too soon. Moved it.
  • 0.0.5 Added the ability to passthrough specific information from the .add() options to the response object
  • 0.0.4 Authorization can now be set on each request (currently Bearer [...] only)
  • 0.0.3 More bug fixes: New contentId for every request
  • 0.0.2 Bug fixes
  • 0.0.1 Initial release

Acknowledgement

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

Keywords

FAQs

Package last updated on 24 Feb 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc