Skip to content

Commit 68431b0

Browse files
set metadata per call
1 parent 13f26e2 commit 68431b0

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

packages/common/src/grpc-service.js

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ function GrpcService(config, options) {
159159
this.grpcCredentials = grpc.credentials.createInsecure();
160160
}
161161

162+
this.grpcMetadata = config.grpcMetadata;
162163
this.maxRetries = options.maxRetries;
163164

164165
var apiVersion = config.apiVersion;
@@ -186,12 +187,6 @@ function GrpcService(config, options) {
186187
service.baseUrl = protoConfig.baseUrl;
187188
}
188189
});
189-
190-
this.callCredentials = [];
191-
192-
if (config.grpcMetadata) {
193-
this.setGrpcMetadata_(config.grpcMetadata);
194-
}
195190
}
196191

197192
nodeutil.inherits(GrpcService, Service);
@@ -234,8 +229,17 @@ GrpcService.prototype.request = function(protoOpts, reqOpts, callback) {
234229
delete reqOpts.autoPaginateVal;
235230

236231
var service = this.getService_(protoOpts);
237-
var grpcOpts = {};
238232

233+
var metadata = null;
234+
if (this.grpcMetadata) {
235+
metadata = new grpc.Metadata();
236+
237+
for (var prop in this.grpcMetadata) {
238+
metadata.add(prop, this.grpcMetadata[prop]);
239+
}
240+
}
241+
242+
var grpcOpts = {};
239243
if (is.number(protoOpts.timeout)) {
240244
grpcOpts.deadline = GrpcService.createDeadline_(protoOpts.timeout);
241245
}
@@ -255,16 +259,16 @@ GrpcService.prototype.request = function(protoOpts, reqOpts, callback) {
255259
request: function(_, onResponse) {
256260
respError = null;
257261

258-
service[protoOpts.method](reqOpts, grpcOpts, function(err, resp) {
259-
if (err) {
260-
respError = GrpcService.decorateError_(err);
262+
service[protoOpts.method](reqOpts, metadata, grpcOpts, function(e, resp) {
263+
if (e) {
264+
respError = GrpcService.decorateError_(e);
261265

262266
if (respError) {
263267
onResponse(null, respError);
264268
return;
265269
}
266270

267-
onResponse(err, resp);
271+
onResponse(e, resp);
268272
return;
269273
}
270274

@@ -653,24 +657,15 @@ GrpcService.structToObj_ = function(struct) {
653657
* @param {?error} callback.err - An error getting an auth client.
654658
*/
655659
GrpcService.prototype.getGrpcCredentials_ = function(callback) {
656-
var self = this;
657-
658660
this.authClient.getAuthClient(function(err, authClient) {
659661
if (err) {
660662
callback(err);
661663
return;
662664
}
663665

664-
var callCredentialObjects = [
666+
var grpcCredentials = grpc.credentials.combineChannelCredentials(
665667
grpc.credentials.createSsl(),
666668
grpc.credentials.createFromGoogleCredential(authClient)
667-
];
668-
669-
callCredentialObjects = callCredentialObjects.concat(self.callCredentials);
670-
671-
var grpcCredentials = grpc.credentials.combineChannelCredentials.apply(
672-
null,
673-
callCredentialObjects
674669
);
675670

676671
callback(null, grpcCredentials);
@@ -744,19 +739,5 @@ GrpcService.prototype.getService_ = function(protoOpts) {
744739
return service;
745740
};
746741

747-
GrpcService.prototype.setGrpcMetadata_ = function(metadata) {
748-
var cc = grpc.credentials.createFromMetadataGenerator(function(_, cb) {
749-
var grpcMetadata = new grpc.Metadata();
750-
751-
for (var prop in metadata) {
752-
grpcMetadata.add(prop, metadata[prop]);
753-
}
754-
755-
cb(null, grpcMetadata);
756-
});
757-
758-
this.callCredentials.push(cc);
759-
};
760-
761742
module.exports = GrpcService;
762743
module.exports.GRPC_ERROR_CODE_TO_HTTP = GRPC_ERROR_CODE_TO_HTTP;

0 commit comments

Comments
 (0)