-
Notifications
You must be signed in to change notification settings - Fork 653
introduce common-gax #2234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
stephenplusplus
wants to merge
4
commits into
googleapis:master
from
stephenplusplus:spp--common-gax
Closed
introduce common-gax #2234
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.