From 7a4b2a65d7157ee8d8bac099df0c09b6778ad370 Mon Sep 17 00:00:00 2001 From: Yoshi Automation Bot Date: Tue, 3 Sep 2019 14:05:32 -0700 Subject: [PATCH] feat: load protos from JSON, grpc-fallback support * [CHANGE ME] Re-generated to pick up changes in the API or client library generator. * fixes * fix webpack.config.js --- src/browser.js | 21 +++++ src/service_proto_list.json | 1 - ...tcha_enterprise_service_v1_beta1_client.js | 77 +++++++++++++------ ...nterprise_service_v1_beta1_proto_list.json | 3 + synth.metadata | 10 +-- test/gapic-v1beta1.js | 7 ++ webpack.config.js | 46 +++++++++++ 7 files changed, 134 insertions(+), 31 deletions(-) create mode 100644 src/browser.js delete mode 100644 src/service_proto_list.json create mode 100644 src/v1beta1/recaptcha_enterprise_service_v1_beta1_proto_list.json create mode 100644 webpack.config.js diff --git a/src/browser.js b/src/browser.js new file mode 100644 index 0000000..ddbcd7e --- /dev/null +++ b/src/browser.js @@ -0,0 +1,21 @@ +// Copyright 2019 Google LLC +// +// 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 +// +// https://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'; + +// Set a flag that we are running in a browser bundle. +global.isBrowser = true; + +// Re-export all exports from ./index.js. +module.exports = require('./index'); diff --git a/src/service_proto_list.json b/src/service_proto_list.json deleted file mode 100644 index 5e09284..0000000 --- a/src/service_proto_list.json +++ /dev/null @@ -1 +0,0 @@ -["../protos/google/cloud/recaptchaenterprise/v1beta1/recaptchaenterprise.proto"] \ No newline at end of file diff --git a/src/v1beta1/recaptcha_enterprise_service_v1_beta1_client.js b/src/v1beta1/recaptcha_enterprise_service_v1_beta1_client.js index 9094e48..e3ac9d6 100644 --- a/src/v1beta1/recaptcha_enterprise_service_v1_beta1_client.js +++ b/src/v1beta1/recaptcha_enterprise_service_v1_beta1_client.js @@ -58,6 +58,16 @@ class RecaptchaEnterpriseServiceV1Beta1Client { opts = opts || {}; this._descriptors = {}; + if (global.isBrowser) { + // If we're in browser, we use gRPC fallback. + opts.fallback = true; + } + + // If we are in browser, we are already using fallback because of the + // "browser" field in package.json. + // But if we were explicitly requested to use fallback, let's do it now. + const gaxModule = !global.isBrowser && opts.fallback ? gax.fallback : gax; + const servicePath = opts.servicePath || opts.apiEndpoint || this.constructor.servicePath; @@ -74,36 +84,51 @@ class RecaptchaEnterpriseServiceV1Beta1Client { // Create a `gaxGrpc` object, with any grpc-specific options // sent to the client. opts.scopes = this.constructor.scopes; - const gaxGrpc = new gax.GrpcClient(opts); + const gaxGrpc = new gaxModule.GrpcClient(opts); // Save the auth object to the client, for use by other methods. this.auth = gaxGrpc.auth; // Determine the client header string. - const clientHeader = [ - `gl-node/${process.versions.node}`, - `grpc/${gaxGrpc.grpcVersion}`, - `gax/${gax.version}`, - `gapic/${VERSION}`, - ]; + const clientHeader = []; + + if (typeof process !== 'undefined' && 'versions' in process) { + clientHeader.push(`gl-node/${process.versions.node}`); + } + clientHeader.push(`gax/${gaxModule.version}`); + if (opts.fallback) { + clientHeader.push(`gl-web/${gaxModule.version}`); + } else { + clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`); + } + clientHeader.push(`gapic/${VERSION}`); if (opts.libName && opts.libVersion) { clientHeader.push(`${opts.libName}/${opts.libVersion}`); } // Load the applicable protos. + // For Node.js, pass the path to JSON proto file. + // For browsers, pass the JSON content. + + const nodejsProtoPath = path.join( + __dirname, + '..', + '..', + 'protos', + 'protos.json' + ); const protos = gaxGrpc.loadProto( - path.join(__dirname, '..', '..', 'protos'), - ['google/cloud/recaptchaenterprise/v1beta1/recaptchaenterprise.proto'] + opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath ); // This API contains "path templates"; forward-slash-separated // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this._pathTemplates = { - assessmentPathTemplate: new gax.PathTemplate( + assessmentPathTemplate: new gaxModule.PathTemplate( 'projects/{project}/assessments/{assessment}' ), - projectPathTemplate: new gax.PathTemplate('projects/{project}'), + projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'), }; // Put together the default options sent with requests. @@ -122,8 +147,12 @@ class RecaptchaEnterpriseServiceV1Beta1Client { // Put together the "service stub" for // google.cloud.recaptchaenterprise.v1beta1.RecaptchaEnterpriseServiceV1Beta1. const recaptchaEnterpriseServiceV1Beta1Stub = gaxGrpc.createStub( - protos.google.cloud.recaptchaenterprise.v1beta1 - .RecaptchaEnterpriseServiceV1Beta1, + opts.fallback + ? protos.lookupService( + 'google.cloud.recaptchaenterprise.v1beta1.RecaptchaEnterpriseServiceV1Beta1' + ) + : protos.google.cloud.recaptchaenterprise.v1beta1 + .RecaptchaEnterpriseServiceV1Beta1, opts ); @@ -134,18 +163,16 @@ class RecaptchaEnterpriseServiceV1Beta1Client { 'annotateAssessment', ]; for (const methodName of recaptchaEnterpriseServiceV1Beta1StubMethods) { - this._innerApiCalls[methodName] = gax.createApiCall( - recaptchaEnterpriseServiceV1Beta1Stub.then( - stub => - function() { - const args = Array.prototype.slice.call(arguments, 0); - return stub[methodName].apply(stub, args); - }, - err => - function() { - throw err; - } - ), + const innerCallPromise = recaptchaEnterpriseServiceV1Beta1Stub.then( + stub => (...args) => { + return stub[methodName].apply(stub, args); + }, + err => () => { + throw err; + } + ); + this._innerApiCalls[methodName] = gaxModule.createApiCall( + innerCallPromise, defaults[methodName], null ); diff --git a/src/v1beta1/recaptcha_enterprise_service_v1_beta1_proto_list.json b/src/v1beta1/recaptcha_enterprise_service_v1_beta1_proto_list.json new file mode 100644 index 0000000..35d2e75 --- /dev/null +++ b/src/v1beta1/recaptcha_enterprise_service_v1_beta1_proto_list.json @@ -0,0 +1,3 @@ +[ + "../../protos/google/cloud/recaptchaenterprise/v1beta1/recaptchaenterprise.proto" +] diff --git a/synth.metadata b/synth.metadata index 55b4ae6..7d8d1bc 100644 --- a/synth.metadata +++ b/synth.metadata @@ -1,19 +1,19 @@ { - "updateTime": "2019-08-28T11:19:03.821244Z", + "updateTime": "2019-08-31T11:15:56.932984Z", "sources": [ { "generator": { "name": "artman", - "version": "0.35.1", - "dockerImage": "googleapis/artman@sha256:b11c7ea0d0831c54016fb50f4b796d24d1971439b30fbc32a369ba1ac887c384" + "version": "0.36.1", + "dockerImage": "googleapis/artman@sha256:7c20f006c7a62d9d782e2665647d52290c37a952ef3cd134624d5dd62b3f71bd" } }, { "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "dbd38035c35083507e2f0b839985cf17e212cb1c", - "internalRef": "265796259" + "sha": "82809578652607c8ee29d9e199c21f28f81a03e0", + "internalRef": "266247326" } }, { diff --git a/test/gapic-v1beta1.js b/test/gapic-v1beta1.js index 7985dd7..9ff1a38 100644 --- a/test/gapic-v1beta1.js +++ b/test/gapic-v1beta1.js @@ -50,6 +50,13 @@ describe('RecaptchaEnterpriseServiceV1Beta1Client', () => { assert(client); }); + it('should create a client with gRPC fallback', () => { + const client = new recaptchaenterpriseModule.v1beta1.RecaptchaEnterpriseServiceV1Beta1Client( + {fallback: true} + ); + assert(client); + }); + describe('createAssessment', () => { it('invokes createAssessment without error', done => { const client = new recaptchaenterpriseModule.v1beta1.RecaptchaEnterpriseServiceV1Beta1Client( diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..3eb4643 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,46 @@ +// Copyright 2019 Google LLC +// +// 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 +// +// https://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.exports = { + entry: './src/browser.js', + output: { + library: 'recaptchaenterprise', + filename: './recaptchaenterprise.js', + }, + node: { + child_process: 'empty', + fs: 'empty', + crypto: 'empty', + }, + resolve: { + extensions: ['.js', '.json'], + }, + module: { + rules: [ + { + test: /node_modules[\\/]retry-request[\\/]/, + use: 'null-loader', + }, + { + test: /node_modules[\\/]https-proxy-agent[\\/]/, + use: 'null-loader', + }, + { + test: /node_modules[\\/]gtoken[\\/]/, + use: 'null-loader', + }, + ], + }, + mode: 'production', +};