Skip to content

Commit

Permalink
allow setting gRPC metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 30, 2016
1 parent 4f8d74a commit 975f506
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions packages/common/src/grpc-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});
};

Expand Down Expand Up @@ -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;

0 comments on commit 975f506

Please sign in to comment.