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.1.0 to 0.2.0

HISTORY

191

lib/client.js
/**
* @author burcujdogan@gmail.com (Burcu Dogan)
* @author burcud@google.com (Burcu Dogan)
*/
var request = require('request')
, Request = require('./request.js')
, Batch = require('./batch.js');
var Request = require('./request.js')
, BatchRequest = require('./batchrequest.js');

@@ -12,88 +11,58 @@ /**

* @constructor
*
* @param {string} name
* @param {string} version
* @param {string|null} token
*
*
* @param {object} apiMeta
* @return {Client}
*/
function Client(name, version, token){
this.name = name;
this.version = version;
this.token_ = token;
function Client(apiMeta) {
this.apiMeta = apiMeta;
// generate helper methods
this.registerHelpers_();
};
/**
* Base path for discovery API.
* @type {string}
* Registers request builders for existing API methods.
*/
Client.prototype.BASE_DISCOVERY_PATH_ = '/discovery/v1/apis/';
Client.prototype.registerHelpers_ = function() {
for (var i in this.apiMeta.methods) {
var methodMeta = this.apiMeta.methods[i];
this.extend_(this, methodMeta.id, this.generateHelper_(methodMeta));
}
};
/**
* Discovery type.
* @type {string}
* TODO: add documentation, move to utils
*
* @param {?object} root
* @param {string} key
* @param {?object} obj
*/
Client.prototype.DISCOVERY_TYPE_ = '/rest';
Client.prototype.extend_ = function(root, key, obj) {
/**
* Additional discovery parameters.
* @type {string}
*/
Client.prototype.DISCOVERY_PARAMS_ = '';
if(!root) root = {};
var keys = key.split('.');
/**
* Generates API through discovery API.
* @param {Function} callback
*/
Client.prototype.load = function(callback) {
var chain = root;
for (var i in keys) {
var chainKey = keys[i];
var that = this;
// if this is the last key, put obj in it.
if (i == keys.length - 1) chain[chainKey] = obj;
else if (!chain[chainKey]) chain[chainKey] = {};
var discoveryPath = this.generateDiscoveryPath_();
this.request_({ path: discoveryPath }, function(err, apiMetadata){
that.apiMetadata = apiMetadata;
if (err) callback && callback(err, null);
else {
// generate methods and resources
var api = that.generateResource_(apiMetadata);
callback && callback(null, api);
}
});
};
/**
* Traverses metadata and generates resources.
*
* @param {object} metadata
* @return {object} resource
*/
Client.prototype.generateResource_ = function(metadata){
var resource = {};
// generate methods
for(var methodName in metadata.methods) {
resource[methodName] = this.generateMethod_(metadata.methods[methodName]);
// move to the next key
chain = chain[chainKey];
}
// recursively look for resources in this resource
if(metadata.resources) {
for(var resourceName in metadata.resources) {
resource[resourceName] =
this.generateResource_(metadata.resources[resourceName]);
}
}
return resource;
return root;
};
/**
* Traverse metadata and generate methods.
*
* @param {object} methodMetadata
* Generate a request builder helper.
*
* @param {object} methodMeta
* @return {Function} method
*/
Client.prototype.generateMethod_ = function(methodMetadata) {
Client.prototype.generateHelper_ = function(methodMeta) {
var that = this;

@@ -103,83 +72,35 @@

// to the resource on given method
return function(params, callback) {
var pathParams = {}, queryParams = {}, bodyParams = null;
for(var paramName in params){
var meta = methodMetadata.parameters &&
methodMetadata.parameters[paramName];
if(meta) {
if (meta.location === 'query') queryParams[paramName] = params[paramName];
else if(meta.location === 'path') pathParams[paramName] = params[paramName];
} else {
// if there is no parameter metadata, put it on the body
bodyParams = bodyParams || {};
bodyParams[paramName] = params[paramName];
}
}
that.request_({
method: methodMetadata.httpMethod,
path: that.apiMetadata.basePath + methodMetadata.path,
queryParams: queryParams,
pathParams: pathParams,
bodyParams: bodyParams
}, callback);
}
return function(params) {
return that.newRequest(methodMeta.id, params);
};
};
/**
* Generates the discovery path of the client.
*
* @return {string} discoveryPath
* Constructs a request to method with given parameters.
*
* @param {string} methodName
* @param {object=} opt_params
*/
Client.prototype.generateDiscoveryPath_ = function() {
var discoveryPath = this.BASE_DISCOVERY_PATH_;
discoveryPath += encodeURIComponent(this.name) + '/' +
encodeURIComponent(this.version) +
this.DISCOVERY_TYPE_ + this.DISCOVERY_PARAMS_;
return discoveryPath;
Client.prototype.newRequest = function(methodName, opt_params) {
return new Request(this.apiMeta, methodName, opt_params);
};
/**
* Makes a request to given method, method and params.
*
* @param {object} args
* @param {Function} callback
*/
Client.prototype.request_ = function(args, callback) {
args.key = this.token_;
new Request(args).execute(callback);
};
/**
* Creates a new Batch.
* Constructs a new batch request.
*
* @return {Batch}
*/
Client.prototype.newBatch = function() {
return new Batch();
Client.prototype.newBatchRequest = function() {
return new BatchRequest(this.apiMeta);
};
/**
* Sets the given token.
* Sets the given API key.
*
* @param {string} token
* @param {string|null} apiKey
*/
Client.prototype.setToken = function(token) {
this.token_ = token;
Client.prototype.setApiKey = function(apiKey) {
this.apiMeta.apiKey = apiKey;
}
/**
* Export client creating utility.
*
* @param {string} name
* @param {string} version
* @param {Function} callback
*/
exports.load = function(name, version, callback) {
new Client(name, version).load(callback);
};
module.exports = Client;
/**
* @author burcujdogan@gmail.com (Burcu Dogan)
* @author burcud@google.com (Burcu Dogan)
*/
var request = require('request')
, querystring = require('querystring');
var BatchRequest = require('./batchrequest.js');

@@ -11,48 +10,11 @@ /**

* @constructor
*
* @param {object} args
*/
function Request(args) {
var queryParams = args.queryParams || {};
var pathParams = args.pathParams || {};
if(args.key) queryParams.key = args.key;
var req = {
method: args.method || 'GET',
params: args.params || {},
headers: args.headers || {},
json: args.bodyParams
};
req.uri = this.generateUrl(args.path, queryParams, pathParams);
this.requestParams_ = req;
};
/**
* Base url for Google APIs.
* @type {string}
*/
Request.prototype.BASE_GOOGLE_APIS_URL_ = 'https://www.googleapis.com';
/**
* Generates url end-point with given path and params.
*
* @param {string} path
* @param {object} queryParams
* @param {object} pathParams
*
* @return {string} url
* @param {object} apiMeta
* @param {string} methodName
* @param {object} params
*/
Request.prototype.generateUrl = function(path, queryParams, pathParams) {
var url = this.BASE_GOOGLE_APIS_URL_ + path;
if(queryParams) url += '?' + querystring.stringify(queryParams);
// place path params in path
for(var param in pathParams){
url = url.replace('{' + param + '}', pathParams[param]);
}
return url;
function Request(apiMeta, methodName, params) {
this.apiMeta = apiMeta;
this.methodName = methodName;
this.params = params;
};

@@ -63,30 +25,21 @@

*
* @param {object|null} opt_params Query params
* @param {Function} callback
*/
Request.prototype.execute = function(callback) {
request(this.requestParams_, this.makeCallback_(callback));
};
Request.prototype.execute = function(opt_params, callback) {
/**
* Wraps "request" callback.
*
* @param {Function} fn
* @return {Function}
*/
Request.prototype.makeCallback_ = function(fn) {
return function(err, res, body) {
// TODO: handle parsing error
new BatchRequest(this.apiMeta)
.add(this.methodName, this.params)
.execute(opt_params, function(err, results){
if(err) fn && fn(err, null);
else {
try {
body = JSON.parse(body);
err = body.error || err;
// if err is a indivual batch error
if (err[0]) err = err[0];
delete body.error;
} catch(ex) {}
fn && fn(err, body, res.headers);
}
};
// callback should recieve the first result
if(err || !results || !results[0]) {
callback && callback(err, null);
} else {
callback && callback(err, results[0]);
}
});
};

@@ -93,0 +46,0 @@

{
"name": "googleapis",
"version": "0.1.0",
"author": "Burcu Dogan <burcujdogan@gmail.com>",
"version": "0.2.0",
"author": "Burcu Dogan <burcud@google.com>",
"description": "Generates node client libs for Google APIs.",
"contributors": [
"contributors": [
{
"name": "Burcu Dogan",
"email": "burcujdogan@gmail.com"
}
"email": "burcud@google.com"
}
],
"main": "./lib/client",
"main": "./lib/googleapis",
"repository": {

@@ -20,3 +20,3 @@ "type": "git",

"api",
"rest",
"google apis",
"client"

@@ -26,4 +26,4 @@ ],

"request" : "*"
},
},
"license": "MIT"
}

@@ -12,38 +12,50 @@ #node-googleapis

var googleapis = require('googleapis);
googleapis.load('urlshortener', 'v1', function(err, urlshortener, headers) {
urlshortener.url.get({ shortUrl: 'http://goo.gl/Op6FN' }, console.log);
googleapis.load('urlshortener','v1', function(err, client) {
// set api key
client.setApiKey('AIzaSyAdjHPT5Pb7Nu56WJ_nlrMGOAgUAtKjiPM');
// batch requests
client
.newBatchRequest()
.add('urlshortener.url.get', { shortUrl: 'http://goo.gl/DdUKX' })
.add('urlshortener.url.insert', { longUrl: 'http://burcudogan.com' })
.execute(null, function(errs, results, headers){
});
// a single request
client
.newRequest('urlshortener.url.get', { shortUrl1: 'http://goo.gl/DdUKX' })
.execute(null, function(err, result, headers){
});
// request builders
client.urlshortener.url
.get({ shortUrl: 'http://goo.gl/DdUKX' })
.execute();
});
outputs:
null { kind: 'urlshortener#url',
id: 'http://goo.gl/Op6FN',
longUrl: 'http://google.com/',
status: 'OK' }
{ expires: 'Mon, 17 Sep 2012 23:11:39 GMT',
date: 'Mon, 17 Sep 2012 23:06:39 GMT',
...
}
## License
Copyright (c) 2012 Burcu Dogan <burcujdogan@gmail.com>
Copyright (c) 2012 Burcu Dogan
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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