Skip to content

Commit 5be4355

Browse files
set metadata per call
1 parent 975f506 commit 5be4355

1 file changed

Lines changed: 16 additions & 35 deletions

File tree

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

@@ -642,24 +646,15 @@ GrpcService.structToObj_ = function(struct) {
642646
* @param {?error} callback.err - An error getting an auth client.
643647
*/
644648
GrpcService.prototype.getGrpcCredentials_ = function(callback) {
645-
var self = this;
646-
647649
this.authClient.getAuthClient(function(err, authClient) {
648650
if (err) {
649651
callback(err);
650652
return;
651653
}
652654

653-
var callCredentialObjects = [
655+
var grpcCredentials = grpc.credentials.combineChannelCredentials(
654656
grpc.credentials.createSsl(),
655657
grpc.credentials.createFromGoogleCredential(authClient)
656-
];
657-
658-
callCredentialObjects = callCredentialObjects.concat(self.callCredentials);
659-
660-
var grpcCredentials = grpc.credentials.combineChannelCredentials.apply(
661-
null,
662-
callCredentialObjects
663658
);
664659

665660
callback(null, grpcCredentials);
@@ -733,19 +728,5 @@ GrpcService.prototype.getService_ = function(protoOpts) {
733728
return service;
734729
};
735730

736-
GrpcService.prototype.setGrpcMetadata_ = function(metadata) {
737-
var cc = grpc.credentials.createFromMetadataGenerator(function(_, cb) {
738-
var grpcMetadata = new grpc.Metadata();
739-
740-
for (var prop in metadata) {
741-
grpcMetadata.add(prop, metadata[prop]);
742-
}
743-
744-
cb(null, grpcMetadata);
745-
});
746-
747-
this.callCredentials.push(cc);
748-
};
749-
750731
module.exports = GrpcService;
751732
module.exports.GRPC_ERROR_CODE_TO_HTTP = GRPC_ERROR_CODE_TO_HTTP;

0 commit comments

Comments
 (0)