Skip to content

Commit

Permalink
feat: load protos from JSON, grpc-fallback support (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-automation authored and JustinBeckwith committed Sep 11, 2019
1 parent b4af7b4 commit 7bac0b1
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 42 deletions.
21 changes: 21 additions & 0 deletions packages/google-privacy-dlp/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');
103 changes: 66 additions & 37 deletions packages/google-privacy-dlp/src/v2/dlp_service_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ class DlpServiceClient {
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,58 +92,73 @@ class DlpServiceClient {
// 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/privacy/dlp/v2/dlp.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 = {
dlpJobPathTemplate: new gax.PathTemplate(
dlpJobPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/dlpJobs/{dlp_job}'
),
organizationPathTemplate: new gax.PathTemplate(
organizationPathTemplate: new gaxModule.PathTemplate(
'organizations/{organization}'
),
organizationDeidentifyTemplatePathTemplate: new gax.PathTemplate(
organizationDeidentifyTemplatePathTemplate: new gaxModule.PathTemplate(
'organizations/{organization}/deidentifyTemplates/{deidentify_template}'
),
organizationInspectTemplatePathTemplate: new gax.PathTemplate(
organizationInspectTemplatePathTemplate: new gaxModule.PathTemplate(
'organizations/{organization}/inspectTemplates/{inspect_template}'
),
organizationStoredInfoTypePathTemplate: new gax.PathTemplate(
organizationStoredInfoTypePathTemplate: new gaxModule.PathTemplate(
'organizations/{organization}/storedInfoTypes/{stored_info_type}'
),
projectPathTemplate: new gax.PathTemplate('projects/{project}'),
projectDeidentifyTemplatePathTemplate: new gax.PathTemplate(
projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'),
projectDeidentifyTemplatePathTemplate: new gaxModule.PathTemplate(
'projects/{project}/deidentifyTemplates/{deidentify_template}'
),
projectInspectTemplatePathTemplate: new gax.PathTemplate(
projectInspectTemplatePathTemplate: new gaxModule.PathTemplate(
'projects/{project}/inspectTemplates/{inspect_template}'
),
projectJobTriggerPathTemplate: new gax.PathTemplate(
projectJobTriggerPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/jobTriggers/{job_trigger}'
),
projectStoredInfoTypePathTemplate: new gax.PathTemplate(
projectStoredInfoTypePathTemplate: new gaxModule.PathTemplate(
'projects/{project}/storedInfoTypes/{stored_info_type}'
),
};
Expand All @@ -142,23 +167,27 @@ class DlpServiceClient {
// (e.g. 50 results at a time, with tokens to get subsequent
// pages). Denote the keys used for pagination and results.
this._descriptors.page = {
listInspectTemplates: new gax.PageDescriptor(
listInspectTemplates: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'inspectTemplates'
),
listDeidentifyTemplates: new gax.PageDescriptor(
listDeidentifyTemplates: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'deidentifyTemplates'
),
listDlpJobs: new gax.PageDescriptor('pageToken', 'nextPageToken', 'jobs'),
listJobTriggers: new gax.PageDescriptor(
listDlpJobs: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'jobs'
),
listJobTriggers: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'jobTriggers'
),
listStoredInfoTypes: new gax.PageDescriptor(
listStoredInfoTypes: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'storedInfoTypes'
Expand All @@ -181,7 +210,9 @@ class DlpServiceClient {
// Put together the "service stub" for
// google.privacy.dlp.v2.DlpService.
const dlpServiceStub = gaxGrpc.createStub(
protos.google.privacy.dlp.v2.DlpService,
opts.fallback
? protos.lookupService('google.privacy.dlp.v2.DlpService')
: protos.google.privacy.dlp.v2.DlpService,
opts
);

Expand Down Expand Up @@ -220,18 +251,16 @@ class DlpServiceClient {
'deleteStoredInfoType',
];
for (const methodName of dlpServiceStubMethods) {
this._innerApiCalls[methodName] = gax.createApiCall(
dlpServiceStub.then(
stub =>
function() {
const args = Array.prototype.slice.call(arguments, 0);
return stub[methodName].apply(stub, args);
},
err =>
function() {
throw err;
}
),
const innerCallPromise = dlpServiceStub.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/privacy/dlp/v2/dlp.proto"
]
10 changes: 5 additions & 5 deletions packages/google-privacy-dlp/synth.metadata
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"updateTime": "2019-08-29T21:07:45.619702Z",
"updateTime": "2019-09-04T11:14:33.690208Z",
"sources": [
{
"generator": {
"name": "artman",
"version": "0.35.1",
"dockerImage": "googleapis/artman@sha256:b11c7ea0d0831c54016fb50f4b796d24d1971439b30fbc32a369ba1ac887c384"
"version": "0.36.2",
"dockerImage": "googleapis/artman@sha256:0e6f3a668cd68afc768ecbe08817cf6e56a0e64fcbdb1c58c3b97492d12418a1"
}
},
{
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "e121a35579e73377f998c11bcc09ba0486736404",
"internalRef": "265953539"
"sha": "a2158681f6e30c5fd9446eb1fd7b5021a6d48bfa",
"internalRef": "266999433"
}
},
{
Expand Down
5 changes: 5 additions & 0 deletions packages/google-privacy-dlp/test/gapic-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe('DlpServiceClient', () => {
assert(client);
});

it('should create a client with gRPC fallback', () => {
const client = new dlpModule.v2.DlpServiceClient({fallback: true});
assert(client);
});

describe('inspectContent', () => {
it('invokes inspectContent without error', done => {
const client = new dlpModule.v2.DlpServiceClient({
Expand Down
46 changes: 46 additions & 0 deletions packages/google-privacy-dlp/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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: 'dlp',
filename: './dlp.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',
};

0 comments on commit 7bac0b1

Please sign in to comment.