@google-cloud/nodejs-common 2.0.3-alpha → 2.0.4-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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@google-cloud/nodejs-common",
3
- "version": "2.0.3-alpha",
3
+ "version": "2.0.4-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",
@@ -41,12 +41,16 @@ const API_VERSION = 'v4';
41
41
  * profileId, idType, conversion, customVariables, encryptionInfo.
42
42
  * The 'idType' can be one of the values: 'encryptedUserId', 'gclid' or
43
43
  * 'mobileDeviceId'.
44
+ * The 'operation' can be 'insert' or 'update'. The default value is 'insert'.
45
+ * 'insert' stands for https://developers.google.com/doubleclick-advertisers/rest/v4/conversions/batchinsert
46
+ * 'update' stands for https://developers.google.com/doubleclick-advertisers/rest/v4/conversions/batchupdate
44
47
  * For other properties, see
45
48
  * https://developers.google.com/doubleclick-advertisers/guides/conversions_update
46
49
  *
47
50
  * @typedef {{
48
51
  * profileId:string,
49
52
  * idType:string,
53
+ * operation:'insert'|'update'|undefined,
50
54
  * conversion:{
51
55
  * floodlightConfigurationId:string,
52
56
  * floodlightActivityId:string,
@@ -60,7 +64,7 @@ const API_VERSION = 'v4';
60
64
  * }|undefined),
61
65
  * }}
62
66
  */
63
- let InsertConversionsConfig;
67
+ let ConversionsConfig;
64
68
 
65
69
  /**
66
70
  * List of properties that will be take from the data file as elements of a
@@ -155,7 +159,7 @@ class DfaReporting {
155
159
  /**
156
160
  * Returns the function to sends out a request to CM with a batch of
157
161
  * conversions.
158
- * @param {!InsertConversionsConfig} config Campaign Manager configuration.
162
+ * @param {!ConversionsConfig} config Campaign Manager configuration.
159
163
  * @return {!SendSingleBatch} Function which can send a batch of hits to
160
164
  * Campaign Manager.
161
165
  */
@@ -171,6 +175,15 @@ class DfaReporting {
171
175
  /** @type {function} Gets the conversion elements from the data object. */
172
176
  const filterObject = getFilterFunction(PICKED_PROPERTIES);
173
177
  const time = new Date().getTime();
178
+ const operation =
179
+ config.operation === 'update' ? 'batchupdate' : 'batchinsert';
180
+ // customVariables is not supported by batchupdate.
181
+ // https://developers.google.com/doubleclick-advertisers/rest/v4/Conversion#CustomFloodlightVariable
182
+ if (operation === 'batchupdate' &&
183
+ typeof config.customVariables !== 'undefined') {
184
+ this.logger.warn('customVariables is not supported by batchupdate');
185
+ delete config.customVariables;
186
+ }
174
187
  const conversions = lines.map((line) => {
175
188
  const record = JSON.parse(line);
176
189
  const conversion = Object.assign(
@@ -184,7 +197,7 @@ class DfaReporting {
184
197
  // Custom Variables
185
198
  if (typeof config.customVariables !== 'undefined') {
186
199
  conversion.customVariables = config.customVariables.map(
187
- (variable) => ({'type': variable, 'value': record[variable],}));
200
+ (variable) => ({ 'type': variable, 'value': record[variable], }));
188
201
  }
189
202
  // User Identifiers
190
203
  if (record.userIdentifiers) {
@@ -225,7 +238,7 @@ class DfaReporting {
225
238
  };
226
239
  try {
227
240
  const dfareporting = await this.getApiClient_();
228
- const response = await dfareporting.conversions.batchinsert({
241
+ const response = await dfareporting.conversions[operation]({
229
242
  profileId: config.profileId,
230
243
  requestBody: requestBody,
231
244
  });
@@ -411,7 +424,7 @@ class DfaReporting {
411
424
 
412
425
  module.exports = {
413
426
  DfaReporting,
414
- InsertConversionsConfig,
427
+ ConversionsConfig,
415
428
  API_VERSION,
416
429
  API_SCOPES,
417
430
  };
package/src/apis/index.js CHANGED
@@ -25,7 +25,7 @@ exports.AuthClient = require('./auth_client.js');
25
25
  * APIs integration class for DFA Reporting API.
26
26
  * @const {{
27
27
  * DfaReporting:!DfaReporting,
28
- * InsertConversionsConfig:!InsertConversionsConfig,
28
+ * ConversionsConfig:!ConversionsConfig,
29
29
  * }}
30
30
  */
31
31
  exports.dfareporting = require('./dfa_reporting.js');
@@ -229,4 +229,3 @@ module.exports = {
229
229
  getIdTokenForFunction,
230
230
  convertEnvPathToAbsolute,
231
231
  };
232
-
@@ -255,4 +255,3 @@ module.exports = {
255
255
  LINE_BREAKER,
256
256
  DEFAULT_SPLIT_SIZE,
257
257
  };
258
-
@@ -21,6 +21,8 @@
21
21
  const winston = require('winston');
22
22
  const {inspect} = require('util');
23
23
  const { LoggingWinston } = require('@google-cloud/logging-winston');
24
+ const { request } = require('gaxios');
25
+ const lodash = require('lodash');
24
26
 
25
27
  /**
26
28
  * The result of a batch of data sent to target API. The batch here means the
@@ -537,6 +539,34 @@ const changeNamingFromSnakeToLowerCamel = (name) => {
537
539
  (initial) => initial.substring(1).toUpperCase());
538
540
  };
539
541
 
542
+ /**
543
+ * Returns the response data for a HTTP request. It will retry the specific
544
+ * times if there was errors happened.
545
+ * @param {object} options Options for the request.
546
+ * @param {object=} logger Default is `console`.
547
+ * @param {number=} retryTimes Default value 3.
548
+ * @return {object}
549
+ */
550
+ const requestWithRetry = async (options, logger = console, retryTimes = 3) => {
551
+ let processedTimes = 0;
552
+ do {
553
+ // Wait sometime (2s, 4s, 8s, ...) before each retry.
554
+ if (processedTimes > 0) await wait(2 ** processedTimes * 1000);
555
+ try {
556
+ const requestOption = lodash.merge({
557
+ responseType: 'json',
558
+ method: 'POST',
559
+ }, options);
560
+ const response = await request(requestOption);
561
+ return response.data;
562
+ } catch (error) {
563
+ processedTimes++;
564
+ if (processedTimes > retryTimes) throw error;
565
+ logger.error(`Request ${JSON.stringify(options)}`, error);
566
+ }
567
+ } while (processedTimes <= retryTimes)
568
+ }
569
+
540
570
  // noinspection JSUnusedAssignment
541
571
  module.exports = {
542
572
  getLogger,
@@ -554,4 +584,5 @@ module.exports = {
554
584
  getObjectByPath,
555
585
  changeNamingFromSnakeToUpperCamel,
556
586
  changeNamingFromSnakeToLowerCamel,
587
+ requestWithRetry,
557
588
  };