@google-cloud/nodejs-common 2.0.5-alpha → 2.0.7-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/apis/analytics.js +1 -1
- package/src/apis/auth_client.js +33 -23
- package/src/apis/google_ads.js +1 -0
- package/src/apis/google_ads_api.js +1550 -0
- package/src/apis/index.js +12 -0
- package/src/components/utils.js +23 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@google-cloud/nodejs-common",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7-alpha",
|
|
4
4
|
"description": "A NodeJs common library for solutions based on Cloud Functions",
|
|
5
5
|
"author": "Google Inc.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"@google-cloud/scheduler": "^4.0.1",
|
|
28
28
|
"@google-cloud/secret-manager": "^5.0.1",
|
|
29
29
|
"gaxios": "^6.1.1",
|
|
30
|
+
"google-ads-nodejs-client": "16.0.0",
|
|
30
31
|
"google-ads-api": "^14.1.0",
|
|
31
32
|
"google-ads-node": "^12.0.2",
|
|
32
33
|
"google-auth-library": "^9.4.2",
|
package/src/apis/analytics.js
CHANGED
package/src/apis/auth_client.js
CHANGED
|
@@ -21,7 +21,13 @@
|
|
|
21
21
|
|
|
22
22
|
const fs = require('fs');
|
|
23
23
|
const path = require('path');
|
|
24
|
-
const {
|
|
24
|
+
const {
|
|
25
|
+
GoogleAuth,
|
|
26
|
+
OAuth2Client,
|
|
27
|
+
UserRefreshClient,
|
|
28
|
+
JWT,
|
|
29
|
+
Compute,
|
|
30
|
+
} = require('google-auth-library');
|
|
25
31
|
const { SecretManager } = require('../components/secret_manager.js');
|
|
26
32
|
const { getLogger } = require('../components/utils.js');
|
|
27
33
|
|
|
@@ -77,6 +83,7 @@ class AuthClient {
|
|
|
77
83
|
this.logger = getLogger('AUTH');
|
|
78
84
|
this.scopes = scopes;
|
|
79
85
|
this.env = Object.assign({}, process.env, overwrittenEnv);
|
|
86
|
+
this.initialized = false;
|
|
80
87
|
}
|
|
81
88
|
|
|
82
89
|
/**
|
|
@@ -88,6 +95,10 @@ class AuthClient {
|
|
|
88
95
|
* and service account key file if there is no secret name was set in the env.
|
|
89
96
|
*/
|
|
90
97
|
async prepareCredentials() {
|
|
98
|
+
if (this.initialized === true) {
|
|
99
|
+
this.logger.info(`This authClient has been initialized.`);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
91
102
|
if (this.env[DEFAULT_ENV_SECRET]) {
|
|
92
103
|
const secretmanager = new SecretManager({
|
|
93
104
|
projectId: this.env.GCP_PROJECT,
|
|
@@ -95,26 +106,24 @@ class AuthClient {
|
|
|
95
106
|
const secret = await secretmanager.access(this.env[DEFAULT_ENV_SECRET]);
|
|
96
107
|
if (secret) {
|
|
97
108
|
const secretObj = JSON.parse(secret);
|
|
98
|
-
if (secretObj.token)
|
|
99
|
-
|
|
100
|
-
} else {
|
|
101
|
-
this.serviceAccountKey = secretObj;
|
|
102
|
-
}
|
|
109
|
+
if (secretObj.token) this.oauthToken = secretObj;
|
|
110
|
+
else this.serviceAccountKey = secretObj;
|
|
103
111
|
this.logger.info(`Get secret from SM ${this.env[DEFAULT_ENV_SECRET]}.`);
|
|
104
|
-
|
|
112
|
+
} else {
|
|
113
|
+
this.logger.warn(`Cannot find SM ${this.env[DEFAULT_ENV_SECRET]}.`);
|
|
114
|
+
}
|
|
115
|
+
} else {// To be compatible with previous solution.
|
|
116
|
+
const oauthTokenFile = this.getContentFromEnvVar(DEFAULT_ENV_OAUTH);
|
|
117
|
+
if (oauthTokenFile) {
|
|
118
|
+
this.oauthToken = JSON.parse(oauthTokenFile);
|
|
119
|
+
}
|
|
120
|
+
const serviceAccountKeyFile =
|
|
121
|
+
this.getContentFromEnvVar(DEFAULT_ENV_KEYFILE);
|
|
122
|
+
if (serviceAccountKeyFile) {
|
|
123
|
+
this.serviceAccountKey = JSON.parse(serviceAccountKeyFile);
|
|
105
124
|
}
|
|
106
|
-
this.logger.warn(`Cannot find SM ${this.env[DEFAULT_ENV_SECRET]}.`);
|
|
107
|
-
}
|
|
108
|
-
// To be compatible with previous solution.
|
|
109
|
-
const oauthTokenFile = this.getContentFromEnvVar(DEFAULT_ENV_OAUTH);
|
|
110
|
-
if (oauthTokenFile) {
|
|
111
|
-
this.oauthToken = JSON.parse(oauthTokenFile);
|
|
112
|
-
}
|
|
113
|
-
const serviceAccountKeyFile =
|
|
114
|
-
this.getContentFromEnvVar(DEFAULT_ENV_KEYFILE);
|
|
115
|
-
if (serviceAccountKeyFile) {
|
|
116
|
-
this.serviceAccountKey = JSON.parse(serviceAccountKeyFile);
|
|
117
125
|
}
|
|
126
|
+
this.initialized = true;
|
|
118
127
|
}
|
|
119
128
|
|
|
120
129
|
/**
|
|
@@ -137,7 +146,7 @@ class AuthClient {
|
|
|
137
146
|
* 1. OAuth, return an OAuth token if available.
|
|
138
147
|
* 2. JWT, return JWT client if a service account key is available.
|
|
139
148
|
* 3. ADC if none of these files exists.
|
|
140
|
-
* @return {!
|
|
149
|
+
* @return {!UserRefreshClient|!JWT|!Compute}
|
|
141
150
|
*/
|
|
142
151
|
getDefaultAuth() {
|
|
143
152
|
if (typeof this.oauthToken !== 'undefined') {
|
|
@@ -167,13 +176,14 @@ class AuthClient {
|
|
|
167
176
|
|
|
168
177
|
/**
|
|
169
178
|
* Returns an OAuth2 client based on the given key file.
|
|
170
|
-
* @return {!
|
|
179
|
+
* @return {!UserRefreshClient}
|
|
171
180
|
*/
|
|
172
181
|
getOAuth2Client() {
|
|
173
182
|
this.ensureCredentialExists_(this.oauthToken, 'OAuth token');
|
|
174
|
-
const { client_id, client_secret, token }
|
|
175
|
-
|
|
176
|
-
oAuth2Client
|
|
183
|
+
const { client_id, client_secret, token: { refresh_token } }
|
|
184
|
+
= this.oauthToken;
|
|
185
|
+
const oAuth2Client =
|
|
186
|
+
new UserRefreshClient(client_id, client_secret, refresh_token);
|
|
177
187
|
return oAuth2Client;
|
|
178
188
|
}
|
|
179
189
|
|