Skip to content

Commit

Permalink
feat: load protos from JSON, grpc-fallback support (#749)
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.

* Delete service_proto_list.json

* fix synth for firestore

* delete webpack.config.js
  • Loading branch information
yoshi-automation authored and alexander-fenster committed Sep 5, 2019
1 parent 20a1ab6 commit 6cb9d68
Show file tree
Hide file tree
Showing 14 changed files with 4,051 additions and 2,146 deletions.
5,886 changes: 3,844 additions & 2,042 deletions dev/protos/protos.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dev/src/service_proto_list.json

This file was deleted.

81 changes: 53 additions & 28 deletions dev/src/v1/firestore_admin_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class FirestoreAdminClient {
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 @@ -75,42 +85,57 @@ class FirestoreAdminClient {
// 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/firestore/admin/v1/firestore_admin.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 = {
databasePathTemplate: new gax.PathTemplate(
databasePathTemplate: new gaxModule.PathTemplate(
'projects/{project}/databases/{database}'
),
fieldPathTemplate: new gax.PathTemplate(
fieldPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/databases/{database}/collectionGroups/{collection_id}/fields/{field_id}'
),
indexPathTemplate: new gax.PathTemplate(
indexPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/databases/{database}/collectionGroups/{collection_id}/indexes/{index_id}'
),
parentPathTemplate: new gax.PathTemplate(
parentPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/databases/{database}/collectionGroups/{collection_id}'
),
};
Expand All @@ -119,12 +144,12 @@ class FirestoreAdminClient {
// (e.g. 50 results at a time, with tokens to get subsequent
// pages). Denote the keys used for pagination and results.
this._descriptors.page = {
listIndexes: new gax.PageDescriptor(
listIndexes: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'indexes'
),
listFields: new gax.PageDescriptor(
listFields: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'fields'
Expand All @@ -147,7 +172,9 @@ class FirestoreAdminClient {
// Put together the "service stub" for
// google.firestore.admin.v1.FirestoreAdmin.
const firestoreAdminStub = gaxGrpc.createStub(
protos.google.firestore.admin.v1.FirestoreAdmin,
opts.fallback
? protos.lookupService('google.firestore.admin.v1.FirestoreAdmin')
: protos.google.firestore.admin.v1.FirestoreAdmin,
opts
);

Expand All @@ -165,18 +192,16 @@ class FirestoreAdminClient {
'updateField',
];
for (const methodName of firestoreAdminStubMethods) {
this._innerApiCalls[methodName] = gax.createApiCall(
firestoreAdminStub.then(
stub =>
function() {
const args = Array.prototype.slice.call(arguments, 0);
return stub[methodName].apply(stub, args);
},
err =>
function() {
throw err;
}
),
const innerCallPromise = firestoreAdminStub.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
5 changes: 3 additions & 2 deletions dev/src/v1/firestore_admin_client_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"retry_codes": {
"idempotent": [
"DEADLINE_EXCEEDED",
"INTERNAL",
"UNAVAILABLE"
],
"non_idempotent": []
Expand All @@ -13,9 +14,9 @@
"initial_retry_delay_millis": 100,
"retry_delay_multiplier": 1.3,
"max_retry_delay_millis": 60000,
"initial_rpc_timeout_millis": 20000,
"initial_rpc_timeout_millis": 60000,
"rpc_timeout_multiplier": 1.0,
"max_rpc_timeout_millis": 20000,
"max_rpc_timeout_millis": 60000,
"total_timeout_millis": 600000
}
},
Expand Down
3 changes: 3 additions & 0 deletions dev/src/v1/firestore_admin_proto_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"../../protos/google/firestore/admin/v1/firestore_admin.proto"
]
89 changes: 57 additions & 32 deletions dev/src/v1/firestore_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ class FirestoreClient {
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 @@ -89,42 +99,57 @@ class FirestoreClient {
// 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/firestore/v1/firestore.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 = {
anyPathPathTemplate: new gax.PathTemplate(
anyPathPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/databases/{database}/documents/{document}/{any_path=**}'
),
databaseRootPathTemplate: new gax.PathTemplate(
databaseRootPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/databases/{database}'
),
documentPathPathTemplate: new gax.PathTemplate(
documentPathPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/databases/{database}/documents/{document_path=**}'
),
documentRootPathTemplate: new gax.PathTemplate(
documentRootPathTemplate: new gaxModule.PathTemplate(
'projects/{project}/databases/{database}/documents'
),
};
Expand All @@ -133,12 +158,12 @@ class FirestoreClient {
// (e.g. 50 results at a time, with tokens to get subsequent
// pages). Denote the keys used for pagination and results.
this._descriptors.page = {
listDocuments: new gax.PageDescriptor(
listDocuments: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'documents'
),
listCollectionIds: new gax.PageDescriptor(
listCollectionIds: new gaxModule.PageDescriptor(
'pageToken',
'nextPageToken',
'collectionIds'
Expand All @@ -148,12 +173,12 @@ class FirestoreClient {
// Some of the methods on this service provide streaming responses.
// Provide descriptors for these.
this._descriptors.stream = {
batchGetDocuments: new gax.StreamDescriptor(
batchGetDocuments: new gaxModule.StreamDescriptor(
gax.StreamType.SERVER_STREAMING
),
runQuery: new gax.StreamDescriptor(gax.StreamType.SERVER_STREAMING),
write: new gax.StreamDescriptor(gax.StreamType.BIDI_STREAMING),
listen: new gax.StreamDescriptor(gax.StreamType.BIDI_STREAMING),
runQuery: new gaxModule.StreamDescriptor(gax.StreamType.SERVER_STREAMING),
write: new gaxModule.StreamDescriptor(gax.StreamType.BIDI_STREAMING),
listen: new gaxModule.StreamDescriptor(gax.StreamType.BIDI_STREAMING),
};

// Put together the default options sent with requests.
Expand All @@ -172,7 +197,9 @@ class FirestoreClient {
// Put together the "service stub" for
// google.firestore.v1.Firestore.
const firestoreStub = gaxGrpc.createStub(
protos.google.firestore.v1.Firestore,
opts.fallback
? protos.lookupService('google.firestore.v1.Firestore')
: protos.google.firestore.v1.Firestore,
opts
);

Expand All @@ -194,18 +221,16 @@ class FirestoreClient {
'listCollectionIds',
];
for (const methodName of firestoreStubMethods) {
this._innerApiCalls[methodName] = gax.createApiCall(
firestoreStub.then(
stub =>
function() {
const args = Array.prototype.slice.call(arguments, 0);
return stub[methodName].apply(stub, args);
},
err =>
function() {
throw err;
}
),
const innerCallPromise = firestoreStub.then(
stub => (...args) => {
return stub[methodName].apply(stub, args);
},
err => () => {
throw err;
}
);
this._innerApiCalls[methodName] = gaxModule.createApiCall(
innerCallPromise,
defaults[methodName],
this._descriptors.page[methodName] ||
this._descriptors.stream[methodName]
Expand Down
8 changes: 4 additions & 4 deletions dev/src/v1/firestore_client_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
"initial_retry_delay_millis": 100,
"retry_delay_multiplier": 1.3,
"max_retry_delay_millis": 60000,
"initial_rpc_timeout_millis": 20000,
"initial_rpc_timeout_millis": 60000,
"rpc_timeout_multiplier": 1.0,
"max_rpc_timeout_millis": 20000,
"max_rpc_timeout_millis": 60000,
"total_timeout_millis": 600000
},
"streaming": {
"initial_retry_delay_millis": 100,
"retry_delay_multiplier": 1.3,
"max_retry_delay_millis": 60000,
"initial_rpc_timeout_millis": 300000,
"initial_rpc_timeout_millis": 60000,
"rpc_timeout_multiplier": 1.0,
"max_rpc_timeout_millis": 300000,
"max_rpc_timeout_millis": 60000,
"total_timeout_millis": 600000
}
},
Expand Down
3 changes: 3 additions & 0 deletions dev/src/v1/firestore_proto_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"../../protos/google/firestore/v1/firestore.proto"
]
Loading

0 comments on commit 6cb9d68

Please sign in to comment.