Skip to content

Commit c6a4ccd

Browse files
Merge pull request #661 from ofrobots/patch-1
cache auth client
2 parents c70ce1e + 29f77ce commit c6a4ccd

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/common/util.js

+13
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ util.shouldRetryRequest = shouldRetryRequest;
468468
* for authenticating API requests.
469469
*
470470
* @param {object} config - Configuration object.
471+
* @param {object=} config.authClient - AuthClient object. If not provided,
472+
* it will be created and cached here.
471473
* @param {object=} config.credentials - Credentials object.
472474
* @param {string=} config.email - Account email address, required for PEM/P12
473475
* usage.
@@ -476,6 +478,12 @@ util.shouldRetryRequest = shouldRetryRequest;
476478
* @param {function} callback - The callback function.
477479
*/
478480
function getAuthClient(config, callback) {
481+
if (config.authClient) {
482+
setImmediate(function() {
483+
callback(null, config.authClient);
484+
});
485+
return;
486+
}
479487
var googleAuth = new GoogleAuth();
480488

481489
if (config.keyFile) {
@@ -500,6 +508,7 @@ function getAuthClient(config, callback) {
500508
authClient = authClient.createScoped(config.scopes);
501509
}
502510

511+
config.authClient = authClient;
503512
callback(null, authClient);
504513
}
505514
}
@@ -510,6 +519,8 @@ util.getAuthClient = getAuthClient;
510519
* Authenticate a request by extending its headers object with an access token.
511520
*
512521
* @param {object} config - Configuration object.
522+
* @param {object=} config.authClient - AuthClient object. If not provided,
523+
* it will be created and cached here.
513524
* @param {object=} config.credentials - Credentials object.
514525
* @param {string=} config.email - Account email address, required for PEM/P12
515526
* usage.
@@ -563,6 +574,8 @@ util.authorizeRequest = authorizeRequest;
563574
* response is related to rate limits or certain intermittent server errors.
564575
* We will exponentially backoff subsequent requests by default. (default:
565576
* true)
577+
* @param {object=} config.authClient - AuthClient object. If not provided,
578+
* it will be created and cached here.
566579
* @param {object=} config.credentials - Credentials object.
567580
* @param {boolean=} config.customEndpoint - If true, just return the provided
568581
* request options. Default: false.

0 commit comments

Comments
 (0)