
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
google-api-batch-utils
Advanced tools
Utility functions for creating request bodies and parsing batch responses for Google's REST APIs
Utility functions for creating request bodies and parsing batch responses for Google's REST APIs.
List the latest three messages in a user's inbox, and then get a snippet of each message in a batch request to the Gmail API:
var rp = require('request-promise');
var batchUtils = require('google-api-batch-utils');
var createBatchBody = batchUtils.createBatchBody;
var parseBatchResponse = batchUtils.parseBatchResponse;
var BOUNDARY = 'example_boundary';
rp({
uri: 'https://www.googleapis.com/gmail/v1/users/me/messages',
qs: { maxResults: 3 , fields: 'messages(id)'},
headers: { Authorization: 'Bearer {API_KEY}'},
json: true
}).then(function(response) {
var uris = response.messages.map(function(item) {
return {uri: '/gmail/v1/users/me/messages/' + item.id, qs: { fields: 'snippet'}};
});
var batchBody = createBatchBody(uris, BOUNDARY);
return rp({
method: 'POST',
uri: 'https://www.googleapis.com/batch',
headers: {
Authorization: 'Bearer {API_KEY}' ,
'Content-Type': 'multipart/mixed; boundary="' + BOUNDARY + '"'
},
body: batchBody
});
}).then(parseBatchResponse)
.then(console.log.bind(console));
// =>
// [
// { snippet: 'Numberphile has uploaded The iPhone of Slide Rules - Numberphile Thanks Audible: http://www....' },
// { snippet: 'Feel the bern' },
// { snippet: 'See what is new with your LinkedIn connections...' }
// ]
/**
* Takes an array of API call objects and generates a string that can be used
* as the body in a call to Google batch API.
* @param {object[]} apiCalls
* @param {string} apiCalls[].uri - Uri of the API call.
* @param {string} apiCalls[].[method] - Optional HTTP method. Defaults to GET.
* @param {object} apiCalls[].[qs] - Optional object with querystring parameters.
* @param {string} apiCalls[].[body] - Optional request body string.
* @param {string} boundary - String that delimits the calls.
* @return {string}
*/
createBatchBody(apiCalls, boundary);
/**
* Parses a raw string response from the Google batch API into objects.
* @param {string} response
* @return {object[]}
*/
parseBatchResponse(response);
MIT
FAQs
Utility functions for creating request bodies and parsing batch responses for Google's REST APIs
We found that google-api-batch-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.