@google-cloud/nodejs-common 2.3.0 → 2.3.1
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 +1 -1
- package/src/apis/auth_client.js +10 -10
- package/src/apis/base/ads_api_common.js +1 -1
- package/src/apis/google_ads_api.js +17 -17
- package/src/apis/spreadsheets.js +27 -6
- package/src/components/firestore/access_base.js +3 -3
- package/src/components/firestore/data_access_object.js +1 -1
- package/src/components/firestore/datastore_mode_access.js +3 -3
- package/src/components/pubsub.js +1 -1
- package/src/components/scheduler.js +1 -1
- package/src/components/storage.js +1 -1
- package/src/components/utils.js +1 -1
package/package.json
CHANGED
package/src/apis/auth_client.js
CHANGED
|
@@ -48,23 +48,23 @@ const DEFAULT_ENV_KEYFILE = 'API_SERVICE_ACCOUNT';
|
|
|
48
48
|
*
|
|
49
49
|
* There are two use cases for this authentication helper class:
|
|
50
50
|
* 1. The user only has OAuth access due to some reasons, so ADC can't be used;
|
|
51
|
-
* 2. User-managed service account is
|
|
51
|
+
* 2. User-managed service account is required for external APIs for some
|
|
52
52
|
* specific considerations, e.g. security. In this case, a file based key file
|
|
53
53
|
* can be used to generate a JWT auth client.
|
|
54
54
|
*
|
|
55
55
|
* To solve these challenges, this class tries to probe the settings from
|
|
56
|
-
*
|
|
56
|
+
* environment variables, starts from the name of secret (Secret Manager), OAuth
|
|
57
57
|
* token file (deprecated), then service account key file (deprecated). It will
|
|
58
58
|
* fallback to ADC if those probing failed.
|
|
59
|
-
* Note, Secret Manager is the
|
|
59
|
+
* Note, Secret Manager is the recommended way to store tokens because it is a
|
|
60
60
|
* secure and convenient central storage system to manage access across Google
|
|
61
61
|
* Cloud.
|
|
62
62
|
*
|
|
63
63
|
* The recommended environment variable is:
|
|
64
64
|
* SECRET_NAME: the name of secret. The secret can be a oauth token file or a
|
|
65
65
|
* service account key file. This env var is used to offer a global auth for a
|
|
66
|
-
* solution. If different
|
|
67
|
-
* set
|
|
66
|
+
* solution. If different authentications are required, the value of passed
|
|
67
|
+
* `env` can be set by the runtime.
|
|
68
68
|
*
|
|
69
69
|
* Alternative environment variable but not recommended for prod environment:
|
|
70
70
|
* OAUTH2_TOKEN_JSON : the oauth token key files, refresh token and proper API
|
|
@@ -88,7 +88,7 @@ class AuthClient {
|
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
90
|
* Prepares the `oauthToken` object and/or `serviceAccountKey` based on the
|
|
91
|
-
* settings in
|
|
91
|
+
* settings in environment object.
|
|
92
92
|
* A secret name is preferred to offer the token of the OAuth or key of a
|
|
93
93
|
* service account.
|
|
94
94
|
* To be compatible, this function also checks the env for oauth token file
|
|
@@ -100,10 +100,10 @@ class AuthClient {
|
|
|
100
100
|
return;
|
|
101
101
|
}
|
|
102
102
|
if (this.env[DEFAULT_ENV_SECRET]) {
|
|
103
|
-
const
|
|
103
|
+
const secretManager = new SecretManager({
|
|
104
104
|
projectId: this.env.GCP_PROJECT,
|
|
105
105
|
});
|
|
106
|
-
const secret = await
|
|
106
|
+
const secret = await secretManager.access(this.env[DEFAULT_ENV_SECRET]);
|
|
107
107
|
if (secret) {
|
|
108
108
|
const secretObj = JSON.parse(secret);
|
|
109
109
|
if (secretObj.token) this.oauthToken = secretObj;
|
|
@@ -127,7 +127,7 @@ class AuthClient {
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
/**
|
|
130
|
-
* Factory method to offer a prepared AuthClient
|
|
130
|
+
* Factory method to offer a prepared AuthClient instance in an async way.
|
|
131
131
|
* @param {string|!Array<string>|!ReadonlyArray<string>} scopes
|
|
132
132
|
* @param {!Object<string,string>=} env The environment object to hold env
|
|
133
133
|
* variables.
|
|
@@ -163,7 +163,7 @@ class AuthClient {
|
|
|
163
163
|
* steps:
|
|
164
164
|
* 1. Checks environment variable GOOGLE_APPLICATION_CREDENTIALS to get
|
|
165
165
|
* service account. Returns a JWT if it exists;
|
|
166
|
-
* 2. Uses default service account of Computer Engine/
|
|
166
|
+
* 2. Uses default service account of Computer Engine/AppEngine/Cloud
|
|
167
167
|
* Functions
|
|
168
168
|
* 3. Otherwise, an error occurs.
|
|
169
169
|
* @see https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually
|
|
@@ -38,7 +38,7 @@ const END_TAG = '"requestId"';
|
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* A stream.Transform that can extract properties and convert naming of the
|
|
41
|
-
*
|
|
41
|
+
* response of Google/Search Ads report from REST interface.
|
|
42
42
|
*/
|
|
43
43
|
class RestSearchStreamTransform extends Transform {
|
|
44
44
|
|
|
@@ -211,7 +211,7 @@ const MAX_IDENTIFIERS = {
|
|
|
211
211
|
};
|
|
212
212
|
|
|
213
213
|
/**
|
|
214
|
-
* Configuration for uploading click conversions, call
|
|
214
|
+
* Configuration for uploading click conversions, call conversions or conversion
|
|
215
215
|
* adjustments for Google Ads, includes:
|
|
216
216
|
* gclid, conversionAction, conversionDateTime, conversionValue,
|
|
217
217
|
* currencyCode, orderId, externalAttributionData,
|
|
@@ -598,7 +598,7 @@ class GoogleAdsApi {
|
|
|
598
598
|
* e.g. wrong conversion Id, wrong gclid, etc.
|
|
599
599
|
* `Status` has a property `message` that has the general information of the
|
|
600
600
|
* error, however, the detailed information (e.g. the failed conversions and
|
|
601
|
-
* their reasons) lies in the property named `
|
|
601
|
+
* their reasons) lies in the property named `details` (an array of
|
|
602
602
|
* `GoogleAdsFailure`). Each `GoogleAdsFailure` is related to a failed
|
|
603
603
|
* conversion. The function `extraFailedLines_` is used to extract the
|
|
604
604
|
* details.
|
|
@@ -612,8 +612,8 @@ class GoogleAdsApi {
|
|
|
612
612
|
* @param {string} customerId
|
|
613
613
|
* @param {string} loginCustomerId Login customer account ID (Mcc Account id).
|
|
614
614
|
* @param {!ConversionConfig} conversionConfig Default conversion parameters.
|
|
615
|
-
* @param {string} functionName The name of sending
|
|
616
|
-
* be `uploadClickConversions`, `uploadCallConversions` or
|
|
615
|
+
* @param {string} functionName The name of sending conversions function,
|
|
616
|
+
* could be `uploadClickConversions`, `uploadCallConversions` or
|
|
617
617
|
* `uploadConversionAdjustments`.
|
|
618
618
|
* @param {string} propertyForDebug The name of property for debug info.
|
|
619
619
|
* @return {!SendSingleBatch} Function which can send a batch of hits to
|
|
@@ -945,8 +945,8 @@ class GoogleAdsApi {
|
|
|
945
945
|
AND user_list.membership_status = OPEN
|
|
946
946
|
AND user_list.crm_based_user_list.upload_key_type = ${uploadKeyType}
|
|
947
947
|
`;
|
|
948
|
-
const
|
|
949
|
-
return
|
|
948
|
+
const userLists = await this.getReport(customerId, loginCustomerId, query);
|
|
949
|
+
return userLists.length === 0 ? undefined : userLists[0].userList.id;
|
|
950
950
|
}
|
|
951
951
|
|
|
952
952
|
/**
|
|
@@ -954,8 +954,8 @@ class GoogleAdsApi {
|
|
|
954
954
|
* Id. The user list would be a CRM_BASED type.
|
|
955
955
|
* Trying to create a list with an used name will fail.
|
|
956
956
|
* The Google Ads service behind this function (UserListService) supports
|
|
957
|
-
* `partial_failure`. Here, we only create one
|
|
958
|
-
* `partial_failure` is disabled to simplified the error
|
|
957
|
+
* `partial_failure`. Here, we only create one user list at one time, so
|
|
958
|
+
* `partial_failure` is disabled to simplified the error handling process.
|
|
959
959
|
* @see getUploadConversionFnBase_ for more details of error handling.
|
|
960
960
|
* @param {!CustomerMatchConfig} customerMatchConfig
|
|
961
961
|
* @return {number} The created user list id. Note this is not the resource
|
|
@@ -973,7 +973,7 @@ class GoogleAdsApi {
|
|
|
973
973
|
customerId: getCleanCid(customerId),
|
|
974
974
|
operations: [{ create: userList }],
|
|
975
975
|
validateOnly: this.debugMode, // when true makes no changes
|
|
976
|
-
partialFailure: false, // Simplify error handling in creating
|
|
976
|
+
partialFailure: false, // Simplify error handling in creating user list
|
|
977
977
|
});
|
|
978
978
|
const client = await this.getUserListServiceClient_();
|
|
979
979
|
const options = this.getCallOptions_(loginCustomerId);
|
|
@@ -984,7 +984,7 @@ class GoogleAdsApi {
|
|
|
984
984
|
*/
|
|
985
985
|
const [response] = await client.mutateUserLists(request, options);
|
|
986
986
|
const { results } = response; // No `partialFailureError` here.
|
|
987
|
-
this.logger.debug(`Created crm
|
|
987
|
+
this.logger.debug(`Created crm user list from`, customerMatchConfig);
|
|
988
988
|
if (!results[0]) {
|
|
989
989
|
if (this.debugMode) {
|
|
990
990
|
throw new Error('No UserList was created in DEBUG mode.');
|
|
@@ -1139,7 +1139,7 @@ class GoogleAdsApi {
|
|
|
1139
1139
|
/**
|
|
1140
1140
|
* Creates a OfflineUserDataJob and returns resource name.
|
|
1141
1141
|
* @param {OfflineUserDataJobConfig} offlineUserDataJobConfig
|
|
1142
|
-
* @return {string} The
|
|
1142
|
+
* @return {string} The resource name of the created job.
|
|
1143
1143
|
*/
|
|
1144
1144
|
async createOfflineUserDataJob(offlineUserDataJobConfig) {
|
|
1145
1145
|
const config = this.getCamelConfig_(offlineUserDataJobConfig);
|
|
@@ -1231,7 +1231,7 @@ class GoogleAdsApi {
|
|
|
1231
1231
|
this.logger.debug(`Add operation to job batch[${batchId}]`, response);
|
|
1232
1232
|
const { partialFailureError: failed } = response;
|
|
1233
1233
|
if (failed) {
|
|
1234
|
-
this.logger.info(`Job[${jobResourceName}]
|
|
1234
|
+
this.logger.info(`Job[${jobResourceName}] fail:`, failed.message);
|
|
1235
1235
|
const failures = failed.details.map(
|
|
1236
1236
|
({ value }) => GoogleAdsFailure.decode(value));
|
|
1237
1237
|
this.extraFailedLines_(batchResult, failures, lines, 0);
|
|
@@ -1331,7 +1331,7 @@ class GoogleAdsApi {
|
|
|
1331
1331
|
|
|
1332
1332
|
/**
|
|
1333
1333
|
* Returns a HTTP header object contains the authentication information for
|
|
1334
|
-
* Google Ads API, include: `developer-token` and `
|
|
1334
|
+
* Google Ads API, include: `developer-token` and `login-customer-id`.
|
|
1335
1335
|
* @param {string} loginCustomerId
|
|
1336
1336
|
* @return {object} The HTTP header object.
|
|
1337
1337
|
* @private
|
|
@@ -1360,7 +1360,7 @@ class GoogleAdsApi {
|
|
|
1360
1360
|
}
|
|
1361
1361
|
|
|
1362
1362
|
/**
|
|
1363
|
-
* Prepares the
|
|
1363
|
+
* Prepares the fetch data service client instance.
|
|
1364
1364
|
* @return {!GoogleAdsServiceClient}
|
|
1365
1365
|
* @private
|
|
1366
1366
|
*/
|
|
@@ -1447,8 +1447,8 @@ class GoogleAdsApi {
|
|
|
1447
1447
|
}
|
|
1448
1448
|
|
|
1449
1449
|
/**
|
|
1450
|
-
* Returns an
|
|
1451
|
-
* @param {Object} record An object contains user
|
|
1450
|
+
* Returns an array of UserIdentifier object based the given JSON object.
|
|
1451
|
+
* @param {Object} record An object contains user identifier information.
|
|
1452
1452
|
* @param {!Array<string>} identifierTypes An list of user identifier types that
|
|
1453
1453
|
* are supported by the target service.
|
|
1454
1454
|
* @param {number} maximumNumOfIdentifiers The maximum number of user
|
|
@@ -1541,7 +1541,7 @@ const buildConversionJsonList = (lines, config, conversionFields,
|
|
|
1541
1541
|
})
|
|
1542
1542
|
}
|
|
1543
1543
|
/**
|
|
1544
|
-
* Returns an
|
|
1544
|
+
* Returns an array of UserData object based on the given arran of JSON strings.
|
|
1545
1545
|
* @param {!Array<string>} lines An array of JSON strings of UserData.
|
|
1546
1546
|
* @param {{
|
|
1547
1547
|
* additionalAttributes: (!UserIdentifierSource|undefined)
|
package/src/apis/spreadsheets.js
CHANGED
|
@@ -67,15 +67,19 @@ let DimensionRange;
|
|
|
67
67
|
class Spreadsheets extends GoogleApiClient {
|
|
68
68
|
/**
|
|
69
69
|
* Init Spreadsheets API client.
|
|
70
|
-
* @param {string}
|
|
70
|
+
* @param {string} spreadsheetIdOrUrl
|
|
71
71
|
* @param {!Object<string,string>=} env The environment object to hold env
|
|
72
72
|
* variables.
|
|
73
73
|
*/
|
|
74
|
-
constructor(
|
|
74
|
+
constructor(spreadsheetIdOrUrl, env = process.env) {
|
|
75
75
|
super(env);
|
|
76
76
|
this.googleApi = 'sheets';
|
|
77
77
|
/** @const {string} */
|
|
78
|
-
|
|
78
|
+
if (spreadsheetIdOrUrl.startsWith('https://')) {
|
|
79
|
+
this.spreadsheetId = /spreadsheets\/d\/([^/]*)/.exec(spreadsheetIdOrUrl)[1];
|
|
80
|
+
} else {
|
|
81
|
+
this.spreadsheetId = spreadsheetIdOrUrl;
|
|
82
|
+
}
|
|
79
83
|
this.logger = getLogger('API.GS');
|
|
80
84
|
}
|
|
81
85
|
|
|
@@ -109,7 +113,7 @@ class Spreadsheets extends GoogleApiClient {
|
|
|
109
113
|
}
|
|
110
114
|
|
|
111
115
|
/**
|
|
112
|
-
* Returns whether the sheet with
|
|
116
|
+
* Returns whether the sheet with specified name exists.
|
|
113
117
|
* @param {string} sheetName
|
|
114
118
|
* @return {boolean}
|
|
115
119
|
*/
|
|
@@ -122,7 +126,7 @@ class Spreadsheets extends GoogleApiClient {
|
|
|
122
126
|
}
|
|
123
127
|
|
|
124
128
|
/**
|
|
125
|
-
* Creates a sheet with the
|
|
129
|
+
* Creates a sheet with the given name.
|
|
126
130
|
*
|
|
127
131
|
* @param {string} sheetName
|
|
128
132
|
*/
|
|
@@ -137,7 +141,7 @@ class Spreadsheets extends GoogleApiClient {
|
|
|
137
141
|
}
|
|
138
142
|
|
|
139
143
|
/**
|
|
140
|
-
* Deletes a sheet with the
|
|
144
|
+
* Deletes a sheet with the given name.
|
|
141
145
|
*
|
|
142
146
|
* @param {string} sheetName
|
|
143
147
|
*/
|
|
@@ -289,6 +293,23 @@ class Spreadsheets extends GoogleApiClient {
|
|
|
289
293
|
}
|
|
290
294
|
return batchResult;
|
|
291
295
|
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Gets values of first row of the specified sheet.
|
|
299
|
+
* @param {string} sheetName
|
|
300
|
+
* @return {!Array<string>}
|
|
301
|
+
*/
|
|
302
|
+
async getHeadline(sheetName) {
|
|
303
|
+
const request = {
|
|
304
|
+
spreadsheetId: this.spreadsheetId,
|
|
305
|
+
range: `'${sheetName}'!1:1`,
|
|
306
|
+
};
|
|
307
|
+
const sheets = await this.getApiClient();
|
|
308
|
+
const response = await sheets.spreadsheets.values.get(request);
|
|
309
|
+
const data = response.data;
|
|
310
|
+
this.logger.debug(`Get: `, data);
|
|
311
|
+
return data.values[0];
|
|
312
|
+
}
|
|
292
313
|
}
|
|
293
314
|
|
|
294
315
|
module.exports = {
|
|
@@ -65,7 +65,7 @@ let Filter;
|
|
|
65
65
|
*/
|
|
66
66
|
class DatastoreDocumentFacade {
|
|
67
67
|
/**
|
|
68
|
-
* Initializes DocumentFacade for
|
|
68
|
+
* Initializes DocumentFacade for Datastore.
|
|
69
69
|
* @param {!DatastoreModeEntity} entity
|
|
70
70
|
*/
|
|
71
71
|
constructor(entity) {
|
|
@@ -87,7 +87,7 @@ class DatastoreDocumentFacade {
|
|
|
87
87
|
*/
|
|
88
88
|
class DatastoreTransactionFacade {
|
|
89
89
|
/**
|
|
90
|
-
* Initializes
|
|
90
|
+
* Initializes Firestore TransactionFacade for Datastore Transaction.
|
|
91
91
|
* @param {!DatastoreModeTransaction} transaction
|
|
92
92
|
* @param {!DatastoreModeEntity} entity
|
|
93
93
|
*/
|
|
@@ -145,7 +145,7 @@ let Database;
|
|
|
145
145
|
* interface offers unified operations on the data objects in both of these two
|
|
146
146
|
* modes.
|
|
147
147
|
*
|
|
148
|
-
* Firestore Native mode ('Firestore') and Firestore Datastore mode ('
|
|
148
|
+
* Firestore Native mode ('Firestore') and Firestore Datastore mode ('Datastore')
|
|
149
149
|
* have different interfaces:
|
|
150
150
|
* 1. 'Firestore' has two kinds objects: 'document' stands for an object (data
|
|
151
151
|
* entity) and 'collection' stands for a group of 'documents'. The
|
|
@@ -34,7 +34,7 @@ const DatastoreModeAccess = require('./datastore_mode_access.js');
|
|
|
34
34
|
/**
|
|
35
35
|
* This is data access object base class on Firestore. It seals the details of
|
|
36
36
|
* different underlying databases, Firestore and Datastore.
|
|
37
|
-
* This class relies on an initial parameter in
|
|
37
|
+
* This class relies on an initial parameter in constructor to indicate the
|
|
38
38
|
* Firestore type.
|
|
39
39
|
*
|
|
40
40
|
* Firestore and Datastore have different transaction APIs. In that case,
|
|
@@ -33,7 +33,7 @@ const {
|
|
|
33
33
|
} = require('./access_base.js');
|
|
34
34
|
const {getLogger, wait} = require('../utils.js');
|
|
35
35
|
|
|
36
|
-
/** @const {number} Max retry times when commit failed in a
|
|
36
|
+
/** @const {number} Max retry times when commit failed in a transaction. */
|
|
37
37
|
const MAX_RETRY_TIMES = 5;
|
|
38
38
|
|
|
39
39
|
/**
|
|
@@ -66,7 +66,7 @@ class DatastoreModeAccess {
|
|
|
66
66
|
* Datastore uses 'Key' to identify entities. A 'Key' composes of Id, entity
|
|
67
67
|
* kind and namespace. The 'id' can be 'undefined' if the next operation is
|
|
68
68
|
* creating a new entity.
|
|
69
|
-
* The default Id of Datastore is
|
|
69
|
+
* The default Id of Datastore is an integer. However, Pub/sub can only send
|
|
70
70
|
* attributes with string values. This will cause the Datastore Ids to be
|
|
71
71
|
* converted to strings. So here will try to change the id back to number if
|
|
72
72
|
* possible.
|
|
@@ -106,7 +106,7 @@ class DatastoreModeAccess {
|
|
|
106
106
|
async waitUntilGetObject(id) {
|
|
107
107
|
const entity = await this.getObject(id);
|
|
108
108
|
if (entity) return id;
|
|
109
|
-
this.logger.debug(`Wait 1 more second until the
|
|
109
|
+
this.logger.debug(`Wait 1 more second until the entity@${id} is ready`);
|
|
110
110
|
return wait(1000, this.waitUntilGetObject(id));
|
|
111
111
|
}
|
|
112
112
|
|
package/src/components/pubsub.js
CHANGED
|
@@ -129,7 +129,7 @@ class EnhancedPubSub {
|
|
|
129
129
|
|
|
130
130
|
/**
|
|
131
131
|
* Using `SubscriberClient` to acknowledge messages.
|
|
132
|
-
* 2022.11.02 The
|
|
132
|
+
* 2022.11.02 The method `ack()` in Message doesn't work properly due to a
|
|
133
133
|
* unknown reason. Use this function to acknowledge a message for now.
|
|
134
134
|
*
|
|
135
135
|
* @param {string} subscription Subscription name.
|
|
@@ -116,7 +116,7 @@ class StorageFile {
|
|
|
116
116
|
*/
|
|
117
117
|
async getLastLineBreaker(start, end, checkPoint = -1) {
|
|
118
118
|
/**
|
|
119
|
-
* How many characters to look back to find a
|
|
119
|
+
* How many characters to look back to find a possible line breaker. If no
|
|
120
120
|
* link break in this range, it will extend to find the last one.
|
|
121
121
|
*/
|
|
122
122
|
const possibleLineBreakRange = 1000;
|
package/src/components/utils.js
CHANGED
|
@@ -29,7 +29,7 @@ const lodash = require('lodash');
|
|
|
29
29
|
* data that will be sent out in one single request.
|
|
30
30
|
* Some APIs allows partial failure: it will take those correct data and
|
|
31
31
|
* response with reasons for those failed ones. 'groupedFailed' uses error
|
|
32
|
-
* message as the key, and
|
|
32
|
+
* message as the key, and the array of related failed lines(records) as value.
|
|
33
33
|
* Some APIs upload whole file. In this case, there will be not 'numberOfLines'
|
|
34
34
|
* or 'failedLines', etc.
|
|
35
35
|
* @typedef {{
|