<commit_before># Batchelor
A lovely little Node.js module to perform batch requests with the Google REST API. Simply does it.

## 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](https://github.com/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](https://github.com/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](https://github.com/wapisasa/batchelor/issues).

## Upgrading to 2.0

`batch.run(...)` now takes a node style callback, with an error as the first parameter and the result as the second.

**INCORRECT** (old, v1.0-style):

``` node
batch.run(function(result){});
```

**CORRECT** (new, v2.0-style):

``` node
batch.run(function(err, result){});
```

## Upgrading to 1.0

The API was changed in 1.0 to move from a singleton instance to a constructor. So before where you used `Batchelor` directly:

``` node
var Batchelor = require('batchelor')
Batchelor.init(...)
Batchelor.add(...)
Batchelor.run(...)
```

You now need to create an instance of Batchelor:

``` node
var Batchelor = require('batchelor')
var batch = new Batchelor(...)
batch.add(...)
batch.run(...)
```

See <https://github.com/wapisasa/batchelor/issues/4> for why this change was made.

## Installation

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

``` sh
$ npm install batchelor --save
```

## How to Use
#### GET Requests
``` node
var Batchelor = require('batchelor');
```
Once the module has been included, we initialise it with all our default options:
``` node
var batch = new Batchelor({
	'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:
``` node
batch.add({
	'method':'GET',
	'path':'/plusDomains/v1/people/me/activities/user'
})
```
Or an Array of objects:
``` node
batch.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()`:
``` node
batch.run(function(err, response){
	if (err){
		console.log("Error: " + err.toString());
	} else {
		res.json(response);
	}
});
```
#### POST Requests
The above examples show `GET` requests. To perform a `POST` requires a few more settings:
``` node
batch.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 `batch.run()` call. Alternatively, a callback can be supplied for each individual calls:
``` node
batch.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 `batch.add()`:
``` node
batch.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` instance. So you can chain calls together.<commit_msg>Removes deprecated Google API endpoint from readme<commit_after># Batchelor
A lovely little Node.js module to perform batch requests with the Google REST API. Simply does it.

## 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](https://github.com/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.

## Upgrading to 2.0

`batch.run(...)` now takes a node style callback, with an error as the first parameter and the result as the second.

**INCORRECT** (old, v1.0-style):

``` node
batch.run(function(result){});
```

**CORRECT** (new, v2.0-style):

``` node
batch.run(function(err, result){});
```

## Upgrading to 1.0

The API was changed in 1.0 to move from a singleton instance to a constructor. So before where you used `Batchelor` directly:

``` node
var Batchelor = require('batchelor')
Batchelor.init(...)
Batchelor.add(...)
Batchelor.run(...)
```

You now need to create an instance of Batchelor:

``` node
var Batchelor = require('batchelor')
var batch = new Batchelor(...)
batch.add(...)
batch.run(...)
```

See <https://github.com/wapisasa/batchelor/issues/4> for why this change was made.

## Installation

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

``` sh
$ npm install batchelor --save
```

## How to Use
#### GET Requests
``` node
var Batchelor = require('batchelor');
```
Once the module has been included, we initialise it with all our default options:
``` node
var batch = new Batchelor({
	'uri':'https://www.googleapis.com/batch/gmail/v1/',
	'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:
``` node
batch.add({
	'method':'GET',
	'path':'/gmail/v1/users/me/messages/123'
})
```
Or an Array of objects:
``` node
batch.add([
	{
		'method':'GET',
		'path':'/gmail/v1/users/me/messages/123'
	},
	{
		'method':'GET',
		'path':'/gmail/v1/users/me/messages/456'
	},
	{
		'method':'GET',
		'path':/gmail/v1/users/me/messages/789'
	}
]);
```
Once you have added all of the requests you need, call `.run()`:
``` node
batch.run(function(err, response){
	if (err){
		console.log("Error: " + err.toString());
	} else {
		res.json(response);
	}
});
```
#### POST Requests
The above examples show `GET` requests. To perform a `POST` requires a few more settings:
``` node
batch.add({
	'method':'POST',
	'path':'/gmail/v1/users/me/messages/',
	'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 `batch.run()` call. Alternatively, a callback can be supplied for each individual calls:
``` node
batch.add({
	'method':'POST',
	'path':'/gmail/v1/users/me/messages/',
	'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 `batch.add()`:
``` node
batch.add({
	'method':'GET',
	'path':'/gmail/v1/users/me/messages/123',
	'requestId':'Batch_UniqueID_1'
})
```
#### A Couple of Little Gifts
###### Method Chaining
All methods return the `Batchelor` instance. So you can chain calls together.
{'fraction_non_alphanumeric': 0.16505202726946538, 'fraction_numerical': 0.00669776342542758, 'mean_word_length': 4.508563899868248, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3446059', 'n_tokens_mistral': 2825, 'n_tokens_neox': 2629, 'n_words': 911}