Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache auth client #661

Merged
merged 1 commit into from
Jun 17, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ util.shouldRetryRequest = shouldRetryRequest;
* for authenticating API requests.
*
* @param {object} config - Configuration object.
* @param {object=} config.authClient - AuthClient object. If not provided,
* it will be created and cached here.
* @param {object=} config.credentials - Credentials object.
* @param {string=} config.email - Account email address, required for PEM/P12
* usage.
Expand All @@ -476,6 +478,12 @@ util.shouldRetryRequest = shouldRetryRequest;
* @param {function} callback - The callback function.
*/
function getAuthClient(config, callback) {
if (config.authClient) {
setImmediate(function() {
callback(null, config.authClient);
});
return;
}
var googleAuth = new GoogleAuth();

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

config.authClient = authClient;
callback(null, authClient);
}
}
Expand All @@ -510,6 +519,8 @@ util.getAuthClient = getAuthClient;
* Authenticate a request by extending its headers object with an access token.
*
* @param {object} config - Configuration object.
* @param {object=} config.authClient - AuthClient object. If not provided,
* it will be created and cached here.
* @param {object=} config.credentials - Credentials object.
* @param {string=} config.email - Account email address, required for PEM/P12
* usage.
Expand Down Expand Up @@ -563,6 +574,8 @@ util.authorizeRequest = authorizeRequest;
* response is related to rate limits or certain intermittent server errors.
* We will exponentially backoff subsequent requests by default. (default:
* true)
* @param {object=} config.authClient - AuthClient object. If not provided,
* it will be created and cached here.
* @param {object=} config.credentials - Credentials object.
* @param {boolean=} config.customEndpoint - If true, just return the provided
* request options. Default: false.
Expand Down