Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 74e7be175425d5f58a5e5b0fa19d248c7b94ab2e @machadogj committed Jul 20, 2012
Showing with 673 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 index.js
  3. +38 −0 lib/Datasets.js
  4. +25 −0 lib/Jobs.js
  5. +29 −0 lib/Tables.js
  6. +32 −0 lib/del.js
  7. +32 −0 lib/get.js
  8. +12 −0 lib/getProjects.js
  9. +73 −0 lib/getToken.js
  10. +22 −0 lib/index.js
  11. +30 −0 lib/post.js
  12. +27 −0 package.json
  13. +55 −0 readme.md
  14. +295 −0 test/tests.js
@@ -0,0 +1,2 @@
+node_modules
+test/file.pem
@@ -0,0 +1 @@
+module.exports = require('./lib/index');
@@ -0,0 +1,38 @@
+var Datasets = function ( options ) {
+
+ var post = require('./post')(options),
+ del = require('./del')(options),
+ get = require('./get')(options);
+
+
+ return {
+ create: function ( name, projId, cb ) {
+ var url = '/projects/' + projId + '/datasets',
+ data = {
+ datasetReference: {
+ projectId: projId,
+ datasetId: name
+ },
+ friendlyName: name
+ };
+
+ post(url, data, cb);
+ },
+ "delete": function ( id, projId, deleteContents, cb ) {
+
+ var url = '/projects/' + projId + '/datasets/' +id,
+ query = { deleteContents: !!deleteContents };
+ del(url, query, cb);
+ },
+ get: function (id, projId, cb) {
+ var url = '/projects/' + projId + '/datasets/' + id;
+ get(url, cb);
+ },
+ getAll: function (projId, cb) {
+ var url = '/projects/' + projId + '/datasets';
+ get(url, cb);
+ }
+ };
+};
+
+module.exports = Datasets;
@@ -0,0 +1,25 @@
+var Jobs = function ( options ) {
+
+ var post = require('./post')(options),
+ del = require('./del')(options),
+ get = require('./get')(options);
+
+ return {
+ create: function ( job, cb ) {
+ var projId = job.jobReference.projectId,
+ id = job.jobReference.datasetId,
+ url = '/projects/' + projId + '/jobs';
+ post(url, job, cb);
+ },
+ get: function ( id, projId, cb ) {
+ var url = '/projects/' + projId + '/jobs/' + id;
+ get(url, cb);
+ },
+ getAll: function ( projId, cb ) {
+ var url = '/projects/' + projId + '/jobs';
+ get(url, cb);
+ }
+ };
+};
+
+module.exports = Jobs;
@@ -0,0 +1,29 @@
+var Tables = function ( options ) {
+
+ var post = require('./post')(options),
+ del = require('./del')(options),
+ get = require('./get')(options);
+
+ return {
+ create: function ( table, cb ) {
+ var projId = table.tableReference.projectId,
+ id = table.tableReference.datasetId,
+ url = '/projects/' + projId + '/datasets/' + id + '/tables';
+ post(url, table, cb);
+ },
+ "delete": function ( id, datasetId, projId, cb ) {
+ var url = '/projects/' + projId + '/datasets/' + datasetId + '/tables/' + id;
+ del(url, undefined, cb);
+ },
+ get: function ( id, datasetId, projId, cb ) {
+ var url = '/projects/' + projId + '/datasets/' + datasetId + '/tables/' + id;
+ get(url, cb);
+ },
+ getAll: function ( datasetId, projId, cb ) {
+ var url = '/projects/' + projId + '/datasets/' + datasetId + '/tables';
+ get(url, cb);
+ }
+ };
+};
+
+module.exports = Tables;
@@ -0,0 +1,32 @@
+var request = require('request');
+
+var del = function ( options ) {
+
+ var getToken = require('./getToken')(options.iss, options.key);
+
+ return function ( url, query, cb ) {
+
+ getToken(function ( err, token ) {
+
+ if (err) { return cb(err); }
+ console.log('deleting to: ' + url);
+ console.log('using token: ' + token);
+ var qs = query || {};
+ qs.access_token = token;
+ request.del({
+ url: 'https://www.googleapis.com/bigquery/v2' + url,
+ qs: qs
+ }, function ( err, res ) {
+
+ if ( err || res.statusCode !== 204 ) {
+ console.log(err || res);
+ cb('there was a problem deleting your entity');
+ } else {
+ cb(undefined);
+ }
+ });
+ });
+ };
+};
+
+module.exports = del;
@@ -0,0 +1,32 @@
+var request = require('request');
+
+var get = function ( options ) {
+
+ var getToken = require('./getToken')(options.iss, options.key);
+
+ return function ( url, cb ) {
+
+ getToken(function ( err, token ) {
+
+ if (err) { return cb(err); }
+ console.log('requesting to: ' + url);
+
+ request.get({
+ url: 'https://www.googleapis.com/bigquery/v2' + url,
+ qs: {
+ access_token: token
+ }
+ }, function ( err, res, body ) {
+
+ if ( err || res.statusCode !== 200 ) {
+ console.log(err || res);
+ cb('there was a problem executing your query');
+ } else {
+ cb(undefined,JSON.parse(body));
+ }
+ });
+ });
+ };
+};
+
+module.exports = get;
@@ -0,0 +1,12 @@
+var getProjects = function ( options ) {
+
+ var get = require('./get')(options);
+
+ return function (cb) {
+
+ var url = '/projects';
+ get(url, cb);
+ };
+};
+
+module.exports = getProjects;
@@ -0,0 +1,73 @@
+var jwt = require('jwt-sign'),
+ request = require('request');
+
+var tokens = {};
+
+var getToken = function ( iss, key ) {
+
+ if (!tokens[iss]) {
+ tokens[iss] = { key: key, token: {} };
+ }
+
+ get = function ( cb ) {
+
+ //check whether we should get a new token.
+ if (!tokens[iss].token.expiration || tokens[iss].token.expiration < getSecondsToDate()) {
+
+ console.log('requesting new token');
+ //console.log(iss);
+ //console.log(key);
+
+ var payload = getPayload(iss);
+ var token = jwt.sign(payload, key);
+
+ request.post({
+ url:"https://accounts.google.com/o/oauth2/token",
+ method: 'POST',
+ form: {
+ 'grant_type': 'assertion',
+ 'assertion_type': 'http://oauth.net/grant_type/jwt/1.0/bearer',
+ 'assertion': token
+ }
+ }, function (error, res, body) {
+
+ if ( error ) {
+ cb('there was a connection problem trying to request the token.');
+ } else if (res.statusCode !== 200) {
+ console.log(res);
+ cb('there was a problem requesting the access token.');
+ }
+
+ body = JSON.parse(body);
+ tokens[iss].token.access_token = body.access_token;
+ tokens[iss].token.expiration = getSecondsToDate() + body.expires_in - 10; //little correction.
+
+ console.log(tokens[iss].token.access_token);
+ cb(undefined, tokens[iss].token.access_token);
+ });
+
+ } else {
+ console.log('returning old token');
+ cb(undefined, tokens[iss].token.access_token);
+ }
+ };
+
+ return get;
+};
+
+module.exports = getToken;
+
+/* private functions */
+function getSecondsToDate(){
+ return new Date().getTime() / 1000;
+}
+
+function getPayload(iss) {
+ return {
+ "iss": iss,
+ "scope": 'https://www.googleapis.com/auth/bigquery',
+ "aud":"https://accounts.google.com/o/oauth2/token",
+ "exp": ~~(getSecondsToDate() + (30 * 60)), //1 hour
+ "iat": ~~(getSecondsToDate() - 60)
+ };
+}
@@ -0,0 +1,22 @@
+var request = require('request');
+var bigquery = function ( options ) {
+
+ if (options.iss === undefined || options.iss === null) {
+ throw Error("the iss is required.");
+ }
+
+ if (options.key === undefined || options.key === null) {
+ throw Error("the key is required");
+ }
+
+ var service = {};
+
+ service.datasets = require('./Datasets')(options);
+ service.tables = require('./Tables')(options);
+ service.jobs = require('./Jobs')(options);
+ service.getProjects = require('./getProjects')(options);
+
+ return service;
+};
+
+module.exports = bigquery;
@@ -0,0 +1,30 @@
+var request = require('request');
+
+var post = function ( options ) {
+
+ var getToken = require('./getToken')(options.iss, options.key);
+
+ return function ( url, data, cb ) {
+
+ getToken(function ( err, token ) {
+
+ if (err) { return cb(err); }
+
+ request.post({
+ url: 'https://www.googleapis.com/bigquery/v2' + url,
+ qs: {
+ access_token: token
+ },
+ json: data
+ }, function ( err, res, body ) {
+ if ( err || res.statusCode !== 200 ) {
+ cb('there was a problem executing your query');
+ } else {
+ cb(undefined,body);
+ }
+ });
+ });
+ };
+};
+
+module.exports = post;
@@ -0,0 +1,27 @@
+{
+ "name": "google-bigquery",
+ "description": "node.js package for accessing google bigquery through a service account",
+ "version": "0.1.0",
+ "author": {
+ "name": "Gustavo Machado",
+ "email": "machadogj@gmail.com"
+ },
+ "engines": {
+ "node": ">= 0.6.16"
+ },
+ "keywords": [
+ "google",
+ "bigquery"
+ ],
+ "dependencies": {
+ "jwt-sign": "*",
+ "request": "*"
+ },
+ "scripts": {
+ "test": "./node_modules/.bin/mocha test/*.js --timeout 10000 --reporter spec"
+ },
+ "devDependencies": {
+ "mocha": "*"
+ },
+ "main":"./index"
+}
Oops, something went wrong.

0 comments on commit 74e7be1

Please sign in to comment.