From 975f506c85046c94c4372d7ef63cf9118965db3f Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Tue, 30 Aug 2016 15:35:27 -0400 Subject: [PATCH] allow setting gRPC metadata --- packages/common/src/grpc-service.js | 34 +++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/common/src/grpc-service.js b/packages/common/src/grpc-service.js index 3cfd995ab540..465013077479 100644 --- a/packages/common/src/grpc-service.js +++ b/packages/common/src/grpc-service.js @@ -138,6 +138,7 @@ var GRPC_ERROR_CODE_TO_HTTP = { * * @param {object} config - Configuration object. * @param {string} config.baseUrl - The base URL to make API requests to. + * @param {object} config.grpcMetadata - Metadata to send with every request. * @param {string[]} config.scopes - The scopes required for the request. * @param {string} config.service - The name of the service. * @param {object=} config.protoServices - Directly provide the required proto @@ -185,6 +186,12 @@ function GrpcService(config, options) { service.baseUrl = protoConfig.baseUrl; } }); + + this.callCredentials = []; + + if (config.grpcMetadata) { + this.setGrpcMetadata_(config.grpcMetadata); + } } nodeutil.inherits(GrpcService, Service); @@ -635,18 +642,27 @@ GrpcService.structToObj_ = function(struct) { * @param {?error} callback.err - An error getting an auth client. */ GrpcService.prototype.getGrpcCredentials_ = function(callback) { + var self = this; + this.authClient.getAuthClient(function(err, authClient) { if (err) { callback(err); return; } - var credentials = grpc.credentials.combineChannelCredentials( + var callCredentialObjects = [ grpc.credentials.createSsl(), grpc.credentials.createFromGoogleCredential(authClient) + ]; + + callCredentialObjects = callCredentialObjects.concat(self.callCredentials); + + var grpcCredentials = grpc.credentials.combineChannelCredentials.apply( + null, + callCredentialObjects ); - callback(null, credentials); + callback(null, grpcCredentials); }); }; @@ -717,5 +733,19 @@ GrpcService.prototype.getService_ = function(protoOpts) { return service; }; +GrpcService.prototype.setGrpcMetadata_ = function(metadata) { + var cc = grpc.credentials.createFromMetadataGenerator(function(_, cb) { + var grpcMetadata = new grpc.Metadata(); + + for (var prop in metadata) { + grpcMetadata.add(prop, metadata[prop]); + } + + cb(null, grpcMetadata); + }); + + this.callCredentials.push(cc); +}; + module.exports = GrpcService; module.exports.GRPC_ERROR_CODE_TO_HTTP = GRPC_ERROR_CODE_TO_HTTP;