Skip to content

Commit

Permalink
feat: load protos from JSON, grpc-fallback support
Browse files Browse the repository at this point in the history
* [CHANGE ME] Re-generated  to pick up changes in the API or client library generator.

* fixes

* fix webpack.config.js
  • Loading branch information
yoshi-automation authored and alexander-fenster committed Sep 3, 2019
1 parent 3ac89e2 commit 24aa50f
Show file tree
Hide file tree
Showing 16 changed files with 3,536 additions and 139 deletions.
3,152 changes: 3,152 additions & 0 deletions packages/google-cloud-monitoring/protos/protos.json

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions packages/google-cloud-monitoring/src/browser.js
Original file line number Diff line number Diff line change
@@ -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');

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ class AlertPolicyServiceClient {
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;

Expand All @@ -82,46 +92,61 @@ class AlertPolicyServiceClient {
// 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/monitoring/v3/alert_service.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 = {
alertPolicyPathTemplate: new gax.PathTemplate(
alertPolicyPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/alertPolicies/{alert_policy}'
),
alertPolicyConditionPathTemplate: new gax.PathTemplate(
alertPolicyConditionPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/alertPolicies/{alert_policy}/conditions/{condition}'
),
projectPathTemplate: new gax.PathTemplate('projects/{project}'),
projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'),
};

// Some of the methods on this service return "paged" results,
// (e.g. 50 results at a time, with tokens to get subsequent
// pages). Denote the keys used for pagination and results.
this._descriptors.page = {
listAlertPolicies: new gax.PageDescriptor(
listAlertPolicies: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'alertPolicies'
Expand All @@ -144,7 +169,9 @@ class AlertPolicyServiceClient {
// Put together the "service stub" for
// google.monitoring.v3.AlertPolicyService.
const alertPolicyServiceStub = gaxGrpc.createStub(
protos.google.monitoring.v3.AlertPolicyService,
opts.fallback
? protos.lookupService('google.monitoring.v3.AlertPolicyService')
: protos.google.monitoring.v3.AlertPolicyService,
opts
);

Expand All @@ -158,18 +185,16 @@ class AlertPolicyServiceClient {
'updateAlertPolicy',
];
for (const methodName of alertPolicyServiceStubMethods) {
this._innerApiCalls[methodName] = gax.createApiCall(
alertPolicyServiceStub.then(
stub =>
function() {
const args = Array.prototype.slice.call(arguments, 0);
return stub[methodName].apply(stub, args);
},
err =>
function() {
throw err;
}
),
const innerCallPromise = alertPolicyServiceStub.then(
stub => (...args) => {
return stub[methodName].apply(stub, args);
},
err => () => {
throw err;
}
);
this._innerApiCalls[methodName] = gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"../../protos/google/monitoring/v3/alert_service.proto"
]
81 changes: 55 additions & 26 deletions packages/google-cloud-monitoring/src/v3/group_service_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ class GroupServiceClient {
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;

Expand All @@ -85,44 +95,63 @@ class GroupServiceClient {
// 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/monitoring/v3/group_service.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 = {
groupPathTemplate: new gax.PathTemplate(
groupPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/groups/{group}'
),
projectPathTemplate: new gax.PathTemplate('projects/{project}'),
projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'),
};

// Some of the methods on this service return "paged" results,
// (e.g. 50 results at a time, with tokens to get subsequent
// pages). Denote the keys used for pagination and results.
this._descriptors.page = {
listGroups: new gax.PageDescriptor('pageToken', 'nextPageToken', 'group'),
listGroupMembers: new gax.PageDescriptor(
listGroups: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'group'
),
listGroupMembers: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'members'
Expand All @@ -145,7 +174,9 @@ class GroupServiceClient {
// Put together the "service stub" for
// google.monitoring.v3.GroupService.
const groupServiceStub = gaxGrpc.createStub(
protos.google.monitoring.v3.GroupService,
opts.fallback
? protos.lookupService('google.monitoring.v3.GroupService')
: protos.google.monitoring.v3.GroupService,
opts
);

Expand All @@ -160,18 +191,16 @@ class GroupServiceClient {
'listGroupMembers',
];
for (const methodName of groupServiceStubMethods) {
this._innerApiCalls[methodName] = gax.createApiCall(
groupServiceStub.then(
stub =>
function() {
const args = Array.prototype.slice.call(arguments, 0);
return stub[methodName].apply(stub, args);
},
err =>
function() {
throw err;
}
),
const innerCallPromise = groupServiceStub.then(
stub => (...args) => {
return stub[methodName].apply(stub, args);
},
err => () => {
throw err;
}
);
this._innerApiCalls[methodName] = gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"../../protos/google/monitoring/v3/group_service.proto"
]
Loading

0 comments on commit 24aa50f

Please sign in to comment.