Socket
Socket
Sign inDemoInstall

googleapis

Package Overview
Dependencies
Maintainers
1
Versions
257
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

googleapis - npm Package Compare versions

Comparing version 0.2.3-alpha to 0.2.4-alpha

2

lib/auth/authclient.js

@@ -27,3 +27,3 @@ /**

AuthClient.prototype.request = function() {
throw Error('Not implemented yet.');
throw new Error('Not implemented yet.');
};

@@ -30,0 +30,0 @@

@@ -160,3 +160,3 @@ /**

if (!credentials.access_token) {
throw Error('No access token is set.');
throw new Error('No access token is set.');
}

@@ -163,0 +163,0 @@

@@ -34,2 +34,18 @@ /**

/**
* Gets the API's name.
* @return {String}
*/
Client.prototype.getName = function() {
return this.apiMeta.name;
};
/**
* Gets the API's version.
* @return {String}
*/
Client.prototype.getVersion = function() {
return this.apiMeta.version;
};
/**
* @private

@@ -60,3 +76,4 @@ * Registers request builders for existing API methods.

var chain = root;
for (var i = 0; i < namespaceKeys.length; i++) {
// avoid the client name
for (var i = 1; i < namespaceKeys.length; i++) {
var chainKey = namespaceKeys[i];

@@ -110,25 +127,4 @@

/**
* Constructs a new batch request.
*
* @return {BatchRequest} New BatchRequest object.
*/
Client.prototype.newBatchRequest = function() {
return new BatchRequest(this.apiMeta);
};
/**
* Sets the given API key.
*
* @param {?string} apiKey API Key.
*
* @return {Client} Returns itself.
*/
Client.prototype.withApiKey = function(apiKey) {
this.apiMeta.apiKey = apiKey;
return this;
};
/**
* Exporting Client.
*/
module.exports = Client;

@@ -17,20 +17,40 @@ /**

var Client = require('./client.js');
var DefaultTransporter = require('./transporters.js');
var qs = require('querystring');
var Client = require('./client.js'),
qs = require('querystring'),
requests = require('./requests.js'),
DefaultTransporter = require('./transporters.js'),
async = require('async');
/**
* @constructor
* GoogleApisClient constructor.
*/
function GoogleApisClient() {
this.clients = [];
}
/**
* Add a new individual client to the instance.
* @param {String} name
* @param {Client} client
*/
GoogleApisClient.prototype.add = function(name, client) {
this[name] = client;
};
/**
* Creates a new batch request.
* @return {BatchRequest} New batch request.
*/
GoogleApisClient.prototype.newBatchRequest = function() {
return new requests.BatchRequest();
};
/**
* @constructor
* GoogleApis constructor.
*
* @param {string} name Name of the API.
* @param {string} version Version of the API.
* @param {object=} opt_config Optional configuration.
*/
function GoogleApis(name, version, opt_config) {
this.name = name;
this.version = version;
this.config = opt_config || {};
this.transporter = exports.Transporter ||
new DefaultTransporter();
function GoogleApis() {
this.toBeDiscovered = [];
this.transporter = exports.Transporter || new DefaultTransporter();
}

@@ -65,10 +85,53 @@

/**
* Discover the API with the given name, version and opt options.
* @param {String} name The name of the API.
* @param {String} version The version of the API.
* @param {Object} opt_opts Additional options.
* @return {GoogleApis} Returns itself.
*/
GoogleApis.prototype.discover = function(name, version, opt_opts) {
this.toBeDiscovered.push({
name: name, version: version, opts: opt_opts });
return this;
};
/**
* Executes requests to discover APIs.
* @param {Function=} opt_callback
*/
GoogleApis.prototype.execute = function(opt_callback) {
var that = this,
operations = [],
client = new GoogleApisClient();
this.toBeDiscovered.forEach(function(obj) {
operations.push(function(callback) {
that.load.call(that, obj, callback);
});
});
async.parallel(operations, function(err, results) {
if (err) {
opt_callback && opt_callback(err, null);
} else {
results = results || [];
results.forEach(function(result) {
// extend client object with indivual clients.
client.add(result.getName(), result);
});
opt_callback && opt_callback(null, client);
}
});
};
/**
* Generates a client through discovery API.
*
* @param {String} api An object to represent the api name, version and opts.
* @param {Function=} opt_callback Optional callback function.
*/
GoogleApis.prototype.load = function(opt_callback) {
GoogleApis.prototype.load = function(api, opt_callback) {
var that = this;
// make the request and generate client
var opts = {
uri: this.generateDiscoveryUrl(), json: true
uri: this.generateDiscoveryUrl(api), json: true
};

@@ -78,5 +141,3 @@

var client = null;
if (!err && json) {
client = new Client(json);
}
if (!err && json) { client = new Client(json); }
opt_callback && opt_callback(err, client);

@@ -88,16 +149,16 @@ });

* Generates the discovery url.
*
* @param {String} api An object to represent the api name, version and opts.
* @return {string} discoveryUrl.
*/
GoogleApis.prototype.generateDiscoveryUrl = function() {
var baseDiscoveryUrl = this.config.baseDiscoveryUrl ||
GoogleApis.prototype.generateDiscoveryUrl = function(api) {
api.opts = api.opts || {};
var baseDiscoveryUrl = api.opts.baseDiscoveryUrl ||
GoogleApis.BASE_DISCOVERY_URL_;
var discoveryParams = this.config.discoveryParams ||
var discoveryParams = api.opts.discoveryParams ||
GoogleApis.DISCOVERY_PARAMS_;
var discoveryUrl = baseDiscoveryUrl;
discoveryUrl += encodeURIComponent(this.name) +
'/' + encodeURIComponent(this.version) +
discoveryUrl += encodeURIComponent(api.name) +
'/' + encodeURIComponent(api.version) +
'/' + GoogleApis.DISCOVERY_TYPE_;

@@ -112,18 +173,13 @@

var googleapis = new GoogleApis();
/**
* Export client creating utility.
*
* @param {string} name Name of the API (e.g. urlshortener).
* @param {string} version Version of the API. (e.g. v1).
* @param {Function} callback Callback fn.
* @param {object=} opt_config Optional configuration.
* Shortcut to OAuth2Client.
*/
exports.load = function(name, version, callback, opt_config) {
new GoogleApis(name, version, opt_config).load(callback);
};
googleapis.OAuth2Client = require('./auth/oauth2client.js');
/**
* Exports GoogleApis.
* Exports googleapis.
*/
exports.GoogleApis = GoogleApis;
module.exports = googleapis;

@@ -134,6 +190,1 @@ /**

exports.Transporter = null;
/**
* Exports OAuth2Client.
*/
exports.OAuth2Client = require('./auth/oauth2client.js');

@@ -24,5 +24,5 @@ /**

function BaseRequest(apiMeta) {
this.apiMeta = apiMeta;
this.transporter = new DefaultTransporter();
this.authClient = null;
this.apiKey = null;
}

@@ -32,10 +32,16 @@

* @const
* @type {string}
* @type {String}
*/
BaseRequest.prototype.JSONRPC_VERSION = '2.0';
BaseRequest.JSONRPC_VERSION = '2.0';
/**
* @const
* @type {String}
*/
BaseRequest.JSONRPC_URL = 'https://www.googleapis.com/rpc';
/**
* Sets the auth client.
* @param {AuthClient} client An auth client instance.
* @return {BatchRequest} Returns itself.
* @return {BaseRequest} Returns itself.
*/

@@ -48,2 +54,12 @@ BaseRequest.prototype.withAuthClient = function(client) {

/**
* Sets an API key.
* @param {String} apiKey
* @return {BaseRequest} Returns itself.
*/
BaseRequest.prototype.withApiKey = function(apiKey) {
this.apiKey = apiKey;
return this;
};
/**
* @protected

@@ -56,9 +72,8 @@ * Generates uri end-point with given params.

BaseRequest.prototype.generateUri = function(opt_params) {
var url = this.apiMeta.rpcUrl;
var url = BaseRequest.JSONRPC_URL;
opt_params = opt_params || {};
if (!!this.apiMeta.apiKey) {
opt_params.key = this.apiMeta.apiKey;
if (this.apiKey) {
opt_params.key = this.apiKey;
}
url += '?' + querystring.stringify(opt_params);

@@ -109,15 +124,3 @@ return url;

BaseRequest.prototype.handleResponse = function(opt_fn) {
return function(err, body, res) {
if (err) {
opt_fn && opt_fn(err, body, res);
} else {
var results = [];
var errors = null;
for (var i in body) {
results.push(body[i].result || null);
}
opt_fn && opt_fn(errors, results, res);
}
};
throw new Error('Not implemented...');
};

@@ -135,3 +138,4 @@

function Request(apiMeta, methodName, params, opt_resource) {
Request.super_.call(this, apiMeta);
Request.super_.call(this);
this.apiMeta = apiMeta;
this.methodName = methodName;

@@ -153,3 +157,3 @@ this.params = params;

var request = {
jsonrpc: this.JSONRPC_VERSION,
jsonrpc: BaseRequest.JSONRPC_VERSION,
id: 0,

@@ -191,6 +195,5 @@ method: this.methodName,

* @constructor
* @param {object} apiMeta Schema returned by the discovery API.
*/
function BatchRequest(apiMeta) {
BatchRequest.super_.call(this, apiMeta);
function BatchRequest() {
BatchRequest.super_.call(this);
this.requests_ = [];

@@ -212,2 +215,3 @@ }

this.requests_.push({
apiMeta: request.apiMeta,
method: request.methodName,

@@ -238,7 +242,7 @@ params: request.params,

payload.push({
jsonrpc: this.JSONRPC_VERSION,
jsonrpc: BaseRequest.JSONRPC_VERSION,
id: i,
method: request.method,
params: request.params,
apiVersion: this.apiMeta.version
apiVersion: request.apiMeta.version
});

@@ -249,3 +253,24 @@ }

/**
* @protected
* Wraps request callbacks.
*
* @param {Function=} opt_fn Optional callback function to be wrapped.
* @return {Function} Wrapped callback function.
*/
BatchRequest.prototype.handleResponse = function(opt_fn) {
return function(err, body, res) {
var results = null;
if (body) {
results = [];
for (var i in body) {
results.push(body[i].result || null);
}
}
opt_fn && opt_fn(err, results, res);
};
};
/**
* Exports Request.

@@ -252,0 +277,0 @@ */

@@ -65,2 +65,7 @@ /**

opt_callback && opt_callback(err, null, res);
} else if (body && body.error) {
// handle single request errors
err = body.error;
delete body.error;
opt_callback(err, body, res);
} else {

@@ -67,0 +72,0 @@ // TODO(burcud): Logic not related to the means of transportation

{
"name": "googleapis",
"version": "0.2.3-alpha",
"version": "0.2.4-alpha",
"author": "Google Inc.",

@@ -28,3 +28,4 @@ "description": "Google APIs Client Library for Node.js",

"dependencies" : {
"request": "2.14.0"
"request": "2.14.0",
"async": "0.2.6"
},

@@ -31,0 +32,0 @@ "devDependencies" : {

@@ -22,12 +22,20 @@ # google-api-nodejs-client [alpha]

Dynamically load any Google API and start making requests:
### Discover APIs
Dynamically load Google APIs and start making requests:
var googleapis = require('googleapis');
googleapis.load('urlshortener', 'v1', function(err, client) {
googleapis
.discover('urlshortener', 'v1')
.discover('plus', 'v3')
.execute(function(err, client) {
var params = { shortUrl: 'http://goo.gl/DdUKX' };
var request = client.urlshortener.url.get(params);
request.execute(function (err, response) {
var req1 = client.urlshortener.url.get(params);
req1.execute(function (err, response) {
console.log('Long url is', response.longUrl);
});
var req2 = client.plus.people.get({ userId: '+BurcuDogan' });
req2.execute();
});

@@ -38,3 +46,3 @@

### API Clients
### API Client

@@ -47,7 +55,7 @@ Client libraries are generated during runtime by metadata provided by Google

googleapis.load('urlshortener', 'v1', function(err, client) {
if (!err) {
console.log('Client is loaded successfully');
}
});
googleapis
.discover('urlshortener', 'v1')
.execute(function(err, client) {
// make requests
});

@@ -58,6 +66,8 @@ Alternatively, you may like to configure the client to append an API key to all

googleapis.load('urlshortener', 'v1', function(err, client) {
client.withApiKey('YOUR API KEY HERE');
// make requests
});
googleapis
.discover('urlshortener', 'v1')
.withApiKey('YOUR API KEY HERE')
.execute(function(err, client) {
// make requests
});

@@ -71,14 +81,9 @@ To learn more about API keys, please see the [documentation](https://developers.google.com/console/help/#UsingKeys).

googleapis.load('urlshortener', 'v1', function(err, client) {
// ...
client
.urlshortener
.url
.get({ shortUrl: 'http://goo.gl/DdUKX' })
.execute(function(err, result) {
// result.longUrl contains the long url.
});
});
googleapis.discover('urlshortener', 'v1').execute(function(err, client) {
client.urlshortener.url.get({ shortUrl: 'http://goo.gl/DdUKX' })
.execute(function(err, result) {
// result.longUrl contains the long url.
});
});
### Batch requests

@@ -88,20 +93,17 @@

googleapis.load('urlshortener', 'v1', function(err, client) {
// ...
var request1 =
client.urlshortener.url.get({ shortUrl: 'http://goo.gl/DdUKX' });
var request2 =
client.urlshortener.url.insert(null, { longUrl: 'http://goo.gl/A5492' });
// create from raw action name
var request3 = client.newRequest('urlshortener.url.list');
var request1 =
client.plus.people.get({ userId: '+BurcuDogan' });
var request2 =
client.urlshortener.url.insert(null, { longUrl: 'http://goo.gl/A5492' });
// create from raw action name
var request3 = client.newRequest('urlshortener.url.list');
client
.newBatchRequest()
.add(request1)
.add(request2)
.add(request3)
.execute(function(err, results) {
client
.newBatchRequest()
.add(request1)
.add(request2)
.add(request3)
.execute(function(err, results) {
});
});
});

@@ -108,0 +110,0 @@ ### Authorization and Authentication

Sorry, the diff of this file is not supported yet

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