Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/common-gax/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@google-cloud/common-gax",
"version": "0.0.0",
"author": "Google Inc.",
"description": "Common components for Cloud APIs Node.js Client Libraries that require google-gax",
"contributors": [
{
"name": "Stephen Sawchuk",
"email": "sawchuk@gmail.com"
}
],
"main": "./src/index.js",
"files": [
"src",
"AUTHORS",
"CONTRIBUTORS",
"COPYING"
],
"repository": "googlecloudplatform/google-cloud-node",
"dependencies": {
"@google-cloud/common": "^0.13.1",
"extend": "^3.0.0",
"stream-events": "^1.0.1",
"through2": "^2.0.3"
},
"devDependencies": {
"mocha": "^3.2.0",
"proxyquire": "^1.7.10"
},
"scripts": {
"publish-module": "node ../../scripts/publish.js common-gax",
"test": "mocha test/*.js"
},
"license": "Apache-2.0",
"engines": {
"node": ">=4.0.0"
}
}
26 changes: 26 additions & 0 deletions packages/common-gax/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*!
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*!
* @module common-gax
*/

'use strict';

/**
* @type {module:common-gax/service}
*/
exports.Service = require('./service.js');
168 changes: 168 additions & 0 deletions packages/common-gax/src/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*!
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*!
* @module common-gax/service
*/

'use strict';

var common = require('@google-cloud/common');
var extend = require('extend');
var streamEvents = require('stream-events');
var through = require('through2');
var util = require('util');

/**
* GaxService is a base class, meant to be inherited from by a "service" that
* uses GAX.
*
* @constructor
* @alias module:common-gax/service
*
* @param {object} config - Configuration object.
* @param {*} config.module - The generated module.
* @param {object} options - [Configuration object](#/docs/?method=gcloud).
*/
function GaxService(config, options) {
config = extend({
scopes: config.module.ALL_SCOPES
}, config);

this.api = {};
this.module = config.module;
this.options = options;

common.Service.call(this, config, options);
}

util.inherits(GaxService, common.Service);

/**
* Funnel all API requests through this method, to be sure we have a project ID.
*
* @param {object} config - Configuration object.
* @param {string} config.client - The gax client name.
* @param {object} config.gaxOpts - GAX options.
* @param {function} config.method - The gax method to call.
* @param {object} config.reqOpts - Request options.
* @param {function=} callback - The callback function.
*/
GaxService.prototype.request = function(config, callback) {
if (global.GCLOUD_SANDBOX_ENV) {
return;
}

this.prepareRequest_(config, function(err, requestFn) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

if (err) {
callback(err);
return;
}

requestFn(callback);
});
};

/**
* Make a request as a stream.
*
* @param {object} config - Configuration object.
* @param {string} config.client - The gax client name.
* @param {object} config.gaxOpts - GAX options.
* @param {function} config.method - The gax method to call.
* @param {object} config.reqOpts - Request options.
* @return {stream}
*/
GaxService.prototype.requestStream = function(config) {
if (global.GCLOUD_SANDBOX_ENV) {
return through.obj();
}

var self = this;

var gaxStream;
var stream = streamEvents(through.obj());

stream.abort = function() {
if (gaxStream && gaxStream.cancel) {
gaxStream.cancel();
}
};

stream.once('reading', function() {
self.prepareRequest_(config, function(err, requestFn) {
if (err) {
stream.destroy(err);
return;
}

gaxStream = requestFn();

gaxStream
.on('error', function(err) {
stream.destroy(err);
})
.pipe(stream);
});
});

return stream;
};

/**
* Prepare a request by instantiating and caching the GAX client. Project ID
* placeholder tokens will be replaced in the request options.
*
* @private
*
* @param {object} config - Configuration object.
* @param {string} config.client - The gax client name.
* @param {object} config.gaxOpts - GAX options.
* @param {function} config.method - The gax method to call.
* @param {object} config.reqOpts - Request options.
* @param {function=} callback - The callback function.
*/
GaxService.prototype.prepareRequest_ = function(config, callback) {
var self = this;

this.getProjectId(function(err, projectId) {
if (err) {
callback(err);
return;
}

var gaxClient = self.api[config.client];

if (!gaxClient) {
// Lazily instantiate client.
gaxClient = self.module(self.options)[config.client](self.options);
self.api[config.client] = gaxClient;
}

var reqOpts = extend(true, {}, config.reqOpts);
reqOpts = common.util.replaceProjectIdToken(reqOpts, projectId);

var requestFn = gaxClient[config.method].bind(
gaxClient,
reqOpts,
config.gaxOpts
);

callback(null, requestFn);
});
};

module.exports = GaxService;
38 changes: 38 additions & 0 deletions packages/common-gax/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var assert = require('assert');
var proxyquire = require('proxyquire');

var fakeService = {};

describe('common-gax', function() {
var commonGax;

before(function() {
commonGax = proxyquire('../src/index.js', {
'./service.js': fakeService
});
});

it('should correctly export the commonGax modules', function() {
assert.deepEqual(commonGax, {
Service: fakeService
});
});
});
Loading