@google-cloud/nodejs-common 2.4.3 → 2.5.0-rc.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/README.md +6 -3
- package/bin/install_functions.sh +1 -2
- package/index.js +0 -1
- package/package.json +17 -18
- package/src/apis/base/auth_restful_api.js +4 -2
- package/src/apis/base/restful_api_base.js +2 -2
- package/src/apis/google_ads_api.js +31 -28
- package/src/apis/index.js +0 -12
- package/src/apis/search_ads.js +2 -6
- package/src/apis/sendgrid.js +10 -3
- package/src/apis/cloud_platform_apis.js +0 -96
- package/src/apis/measurement_protocol.js +0 -156
- package/src/components/automl.js +0 -103
package/README.md
CHANGED
|
@@ -12,7 +12,8 @@ and [Data Tasks Coordinator]. This library includes:
|
|
|
12
12
|
for [GMP and Google Ads Connector]:
|
|
13
13
|
|
|
14
14
|
- Google Analytics data import
|
|
15
|
-
- Google Analytics measurement protocol
|
|
15
|
+
- ~~Google Analytics measurement protocol~~ (This has
|
|
16
|
+
been deprecated since v2.4.4.)
|
|
16
17
|
- Campaign Manager offline conversion upload
|
|
17
18
|
- Search Ads 360 conversions upload
|
|
18
19
|
- Google Ads click conversions upload
|
|
@@ -39,9 +40,11 @@ and [Data Tasks Coordinator]. This library includes:
|
|
|
39
40
|
interface to operate data objects within or not a transaction on either
|
|
40
41
|
Firestore Native mode or Firestore Datastore mode.
|
|
41
42
|
|
|
42
|
-
-
|
|
43
|
+
- ~~**AutoMl Tables API**: Offers a unified entry to use this API based on
|
|
43
44
|
Google Cloud client library combined with REST requests to service
|
|
44
|
-
directly due to some functionalities missed in the client library
|
|
45
|
+
directly due to some functionalities missed in the client library.~~(Google
|
|
46
|
+
Cloud AutoML Tables was retired on April 30, 2024. Please migrate to Vertex
|
|
47
|
+
AI instead.)
|
|
45
48
|
|
|
46
49
|
- **Vertex AI API**: Offers a unified entry to use this API based on Google
|
|
47
50
|
Cloud client library.
|
package/bin/install_functions.sh
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
# limitations under the License.
|
|
16
16
|
|
|
17
17
|
# Cloud Functions Runtime Environment.
|
|
18
|
-
CF_RUNTIME="${CF_RUNTIME:=
|
|
18
|
+
CF_RUNTIME="${CF_RUNTIME:=nodejs20}"
|
|
19
19
|
CF_MEMORY="${CF_MEMORY:=2048MB}"
|
|
20
20
|
|
|
21
21
|
# Counter for steps.
|
|
@@ -1553,7 +1553,6 @@ set_cloud_functions_default_settings() {
|
|
|
1553
1553
|
local -n default_cf_flag=$1
|
|
1554
1554
|
default_cf_flag+=(--region="${REGION}")
|
|
1555
1555
|
default_cf_flag+=(--no-allow-unauthenticated)
|
|
1556
|
-
default_cf_flag+=(--docker-registry=artifact-registry)
|
|
1557
1556
|
default_cf_flag+=(--timeout=540 --memory="${CF_MEMORY}" --runtime="${CF_RUNTIME}")
|
|
1558
1557
|
default_cf_flag+=(--set-env-vars=GCP_PROJECT="${GCP_PROJECT}")
|
|
1559
1558
|
default_cf_flag+=(--set-env-vars=PROJECT_NAMESPACE="${PROJECT_NAMESPACE}")
|
package/index.js
CHANGED
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
|
|
21
21
|
exports.api = require('./src/apis/index.js');
|
|
22
22
|
exports.cloudfunctions = require('./src/components/cloudfunctions_utils.js');
|
|
23
|
-
exports.automl = require('./src/components/automl.js');
|
|
24
23
|
exports.firestore = require('./src/components/firestore/index.js');
|
|
25
24
|
exports.pubsub = require('./src/components/pubsub.js');
|
|
26
25
|
exports.scheduler = require('./src/components/scheduler.js');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@google-cloud/nodejs-common",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0-rc.1",
|
|
4
4
|
"description": "A NodeJs common library for solutions based on Cloud Functions",
|
|
5
5
|
"author": "Google Inc.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -16,26 +16,25 @@
|
|
|
16
16
|
},
|
|
17
17
|
"homepage": "https://github.com/GoogleCloudPlatform/cloud-for-marketing/blob/master/marketing-analytics/activation/common-libs/nodejs-common/README.md",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@google-cloud/aiplatform": "^
|
|
20
|
-
"@google-cloud/
|
|
21
|
-
"@google-cloud/bigquery": "^7.9.1",
|
|
19
|
+
"@google-cloud/aiplatform": "^4.2.0",
|
|
20
|
+
"@google-cloud/bigquery": "^8.1.0",
|
|
22
21
|
"@google-cloud/datastore": "^9.2.1",
|
|
23
|
-
"@google-cloud/firestore": "^7.
|
|
24
|
-
"@google-cloud/logging-winston": "^6.0.
|
|
25
|
-
"@google-cloud/pubsub": "^
|
|
26
|
-
"@google-cloud/
|
|
27
|
-
"@google-cloud/
|
|
28
|
-
"@google-cloud/
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"google-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
22
|
+
"@google-cloud/firestore": "^7.11.1",
|
|
23
|
+
"@google-cloud/logging-winston": "^6.0.1",
|
|
24
|
+
"@google-cloud/pubsub": "^5.1.0",
|
|
25
|
+
"@google-cloud/scheduler": "^5.2.0",
|
|
26
|
+
"@google-cloud/secret-manager": "^6.0.1",
|
|
27
|
+
"@google-cloud/storage": "^7.16.0",
|
|
28
|
+
"@grpc/grpc-js": "^1.13.4",
|
|
29
|
+
"gaxios": "^7.0.0",
|
|
30
|
+
"google-ads-nodejs-client": "^20.0.0",
|
|
31
|
+
"google-auth-library": "^10.0.0",
|
|
32
|
+
"googleapis": "^150.0.1",
|
|
33
|
+
"lodash": "^4.17.21",
|
|
34
|
+
"winston": "^3.17.0"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
|
-
"jasmine": "^5.
|
|
37
|
+
"jasmine": "^5.7.0"
|
|
39
38
|
},
|
|
40
39
|
"scripts": {
|
|
41
40
|
"test": "node node_modules/jasmine/bin/jasmine"
|
|
@@ -62,8 +62,10 @@ class AuthRestfulApi extends RestfulApiBase {
|
|
|
62
62
|
*/
|
|
63
63
|
async getDefaultHeaders() {
|
|
64
64
|
const auth = await this.getAuth();
|
|
65
|
-
const
|
|
66
|
-
|
|
65
|
+
const authHeaders = await auth.getRequestHeaders();
|
|
66
|
+
const mergedHeaders = new Headers(await super.getDefaultHeaders());
|
|
67
|
+
authHeaders.forEach((value, key) => void mergedHeaders.set(key, value));
|
|
68
|
+
return mergedHeaders;
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
}
|
|
@@ -33,6 +33,7 @@ const {
|
|
|
33
33
|
UserListServiceClient,
|
|
34
34
|
protos: { google: { ads: { googleads } } },
|
|
35
35
|
} = require('google-ads-nodejs-client');
|
|
36
|
+
const { AuthRestfulApi } = require('./base/auth_restful_api.js');
|
|
36
37
|
|
|
37
38
|
const API_VERSION = Object.keys(googleads)[0];
|
|
38
39
|
const {
|
|
@@ -335,24 +336,30 @@ let CustomerMatchRecord;
|
|
|
335
336
|
/**
|
|
336
337
|
* Google Ads API class based on Google Ads API library.
|
|
337
338
|
*/
|
|
338
|
-
class GoogleAdsApi {
|
|
339
|
+
class GoogleAdsApi extends AuthRestfulApi {
|
|
339
340
|
/**
|
|
340
341
|
* Note: Rate limits is set by the access level of Developer token.
|
|
341
|
-
* @param {string} developerToken Developer token to access the API.
|
|
342
|
-
* @param {boolean=} debugMode This is used to set ONLY validate conversions
|
|
343
|
-
* but not real uploading.
|
|
344
342
|
* @param {!Object<string,string>=} env The environment object to hold env
|
|
345
343
|
* variables.
|
|
344
|
+
* @param {object} options
|
|
345
|
+
* @param {string} options.developerToken Developer token to access the API.
|
|
346
|
+
* @param {boolean} options.debugMode This is used to set ONLY validate
|
|
347
|
+
* conversions but not real uploading.
|
|
346
348
|
*/
|
|
347
|
-
constructor(
|
|
348
|
-
|
|
349
|
-
this.
|
|
350
|
-
this.
|
|
349
|
+
constructor(env = process.env, options = {}) {
|
|
350
|
+
super(env, options);
|
|
351
|
+
this.developerToken = options.developerToken;
|
|
352
|
+
this.debugMode = options.debugMode || false;
|
|
351
353
|
this.logger = getLogger('API.ADS.N');
|
|
352
354
|
this.logger.info(
|
|
353
355
|
`Init ${this.constructor.name} with Debug Mode?`, this.debugMode);
|
|
354
356
|
}
|
|
355
357
|
|
|
358
|
+
/** @override */
|
|
359
|
+
getScope() {
|
|
360
|
+
return API_SCOPES;
|
|
361
|
+
}
|
|
362
|
+
|
|
356
363
|
/**
|
|
357
364
|
* Gets a Google Ads report based on a Google Ads Query Language(GAQL) query
|
|
358
365
|
* with pagination automatically handled. It returns an array of
|
|
@@ -486,11 +493,9 @@ class GoogleAdsApi {
|
|
|
486
493
|
* @return {!Promise<stream>}
|
|
487
494
|
*/
|
|
488
495
|
async restStreamReport(customerId, loginCustomerId, query) {
|
|
489
|
-
await this.
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
this.getGoogleAdsHeaders_(loginCustomerId)
|
|
493
|
-
);
|
|
496
|
+
const headers = await this.getDefaultHeaders();
|
|
497
|
+
const extra = new Headers(this.getGoogleAdsHeaders_(loginCustomerId));
|
|
498
|
+
extra.forEach((value, key) => void headers.set(key, value));
|
|
494
499
|
const options = {
|
|
495
500
|
baseURL: `${API_ENDPOINT}/${API_VERSION}/`,
|
|
496
501
|
url: `customers/${getCleanCid(customerId)}/googleAds:searchStream`,
|
|
@@ -1332,12 +1337,17 @@ class GoogleAdsApi {
|
|
|
1332
1337
|
/**
|
|
1333
1338
|
* Returns a HTTP header object contains the authentication information for
|
|
1334
1339
|
* Google Ads API, include: `developer-token` and `login-customer-id`.
|
|
1340
|
+
* `grpc` only takes plain objects as valid values of the object `otherArgs`,
|
|
1341
|
+
* so here can not use 'Headers' type yet.
|
|
1335
1342
|
* @param {string} loginCustomerId
|
|
1336
1343
|
* @return {object} The HTTP header object.
|
|
1337
1344
|
* @private
|
|
1338
1345
|
*/
|
|
1339
1346
|
getGoogleAdsHeaders_(loginCustomerId) {
|
|
1340
|
-
const headers = {
|
|
1347
|
+
const headers = {};
|
|
1348
|
+
if (this.developerToken) {
|
|
1349
|
+
headers['developer-token'] = this.developerToken;
|
|
1350
|
+
}
|
|
1341
1351
|
if (loginCustomerId) {
|
|
1342
1352
|
headers['login-customer-id'] = getCleanCid(loginCustomerId);
|
|
1343
1353
|
}
|
|
@@ -1366,9 +1376,8 @@ class GoogleAdsApi {
|
|
|
1366
1376
|
*/
|
|
1367
1377
|
async getGoogleAdsServiceClient_() {
|
|
1368
1378
|
if (this.googleAdsClient) return this.googleAdsClient;
|
|
1369
|
-
await this.authClient.prepareCredentials();
|
|
1370
1379
|
this.googleAdsClient = new GoogleAdsServiceClient({
|
|
1371
|
-
authClient: this.
|
|
1380
|
+
authClient: await this.getAuth(),
|
|
1372
1381
|
});
|
|
1373
1382
|
return this.googleAdsClient;
|
|
1374
1383
|
}
|
|
@@ -1379,9 +1388,8 @@ class GoogleAdsApi {
|
|
|
1379
1388
|
* @private
|
|
1380
1389
|
*/
|
|
1381
1390
|
async getGoogleAdsFieldServiceClient_() {
|
|
1382
|
-
await this.authClient.prepareCredentials();
|
|
1383
1391
|
return new GoogleAdsFieldServiceClient({
|
|
1384
|
-
authClient: this.
|
|
1392
|
+
authClient: await this.getAuth(),
|
|
1385
1393
|
});
|
|
1386
1394
|
}
|
|
1387
1395
|
|
|
@@ -1391,9 +1399,8 @@ class GoogleAdsApi {
|
|
|
1391
1399
|
* @private
|
|
1392
1400
|
*/
|
|
1393
1401
|
async getConversionUploadServiceClient_() {
|
|
1394
|
-
await this.authClient.prepareCredentials();
|
|
1395
1402
|
return new ConversionUploadServiceClient({
|
|
1396
|
-
authClient: this.
|
|
1403
|
+
authClient: await this.getAuth(),
|
|
1397
1404
|
});
|
|
1398
1405
|
}
|
|
1399
1406
|
|
|
@@ -1403,9 +1410,8 @@ class GoogleAdsApi {
|
|
|
1403
1410
|
* @private
|
|
1404
1411
|
*/
|
|
1405
1412
|
async getConversionAdjustmentUploadServiceClient_() {
|
|
1406
|
-
await this.authClient.prepareCredentials();
|
|
1407
1413
|
return new ConversionAdjustmentUploadServiceClient({
|
|
1408
|
-
authClient: this.
|
|
1414
|
+
authClient: await this.getAuth(),
|
|
1409
1415
|
});
|
|
1410
1416
|
}
|
|
1411
1417
|
|
|
@@ -1415,9 +1421,8 @@ class GoogleAdsApi {
|
|
|
1415
1421
|
* @private
|
|
1416
1422
|
*/
|
|
1417
1423
|
async getUserListServiceClient_() {
|
|
1418
|
-
await this.authClient.prepareCredentials();
|
|
1419
1424
|
return new UserListServiceClient({
|
|
1420
|
-
authClient: this.
|
|
1425
|
+
authClient: await this.getAuth(),
|
|
1421
1426
|
});
|
|
1422
1427
|
}
|
|
1423
1428
|
|
|
@@ -1427,9 +1432,8 @@ class GoogleAdsApi {
|
|
|
1427
1432
|
* @private
|
|
1428
1433
|
*/
|
|
1429
1434
|
async getUserDataServiceClient_() {
|
|
1430
|
-
await this.authClient.prepareCredentials();
|
|
1431
1435
|
return new UserDataServiceClient({
|
|
1432
|
-
authClient: this.
|
|
1436
|
+
authClient: await this.getAuth(),
|
|
1433
1437
|
});
|
|
1434
1438
|
}
|
|
1435
1439
|
|
|
@@ -1439,9 +1443,8 @@ class GoogleAdsApi {
|
|
|
1439
1443
|
* @private
|
|
1440
1444
|
*/
|
|
1441
1445
|
async getOfflineUserDataJobServiceClient_() {
|
|
1442
|
-
await this.authClient.prepareCredentials();
|
|
1443
1446
|
return new OfflineUserDataJobServiceClient({
|
|
1444
|
-
authClient: this.
|
|
1447
|
+
authClient: await this.getAuth(),
|
|
1445
1448
|
});
|
|
1446
1449
|
}
|
|
1447
1450
|
}
|
package/src/apis/index.js
CHANGED
|
@@ -47,18 +47,6 @@ exports.displayvideo = require('./display_video.js');
|
|
|
47
47
|
*/
|
|
48
48
|
exports.analytics = require('./analytics.js');
|
|
49
49
|
|
|
50
|
-
/**
|
|
51
|
-
* APIs integration class for Google Analytics Measurement Protocol.
|
|
52
|
-
* @const {{MeasurementProtocol:!MeasurementProtocol}}
|
|
53
|
-
*/
|
|
54
|
-
exports.measurementprotocol = require('./measurement_protocol.js');
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Cloud Resource Manager for checking the permissions.
|
|
58
|
-
* @const {{CloudPlatformApis:!CloudPlatformApis}}
|
|
59
|
-
*/
|
|
60
|
-
exports.cloudplatform = require('./cloud_platform_apis.js');
|
|
61
|
-
|
|
62
50
|
/**
|
|
63
51
|
* APIs integration class for Google Spreadsheets.
|
|
64
52
|
* @const {{
|
package/src/apis/search_ads.js
CHANGED
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
'use strict';
|
|
20
20
|
|
|
21
21
|
const { request: gaxiosRequest } = require('gaxios');
|
|
22
|
-
const { google } = require('googleapis');
|
|
23
22
|
const { GoogleApiClient } = require('./base/google_api_client.js');
|
|
24
23
|
const { changeStringToBigQuerySafe, getLogger }
|
|
25
24
|
= require('../components/utils.js');
|
|
@@ -119,11 +118,8 @@ class SearchAds extends GoogleApiClient {
|
|
|
119
118
|
* @see https://developers.google.com/search-ads/reporting/api/reference/rest/search
|
|
120
119
|
*/
|
|
121
120
|
async restStreamReport(customerId, loginCustomerId, query) {
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
await auth.getRequestHeaders(), {
|
|
125
|
-
'login-customer-id': getCleanCid(loginCustomerId),
|
|
126
|
-
});
|
|
121
|
+
const headers = await this.getDefaultHeaders();
|
|
122
|
+
headers.set('login-customer-id', getCleanCid(loginCustomerId));
|
|
127
123
|
const options = {
|
|
128
124
|
baseURL: `${API_ENDPOINT}/${API_VERSION}/`,
|
|
129
125
|
url: `customers/${getCleanCid(customerId)}/searchAds360:searchStream`,
|
package/src/apis/sendgrid.js
CHANGED
|
@@ -64,11 +64,18 @@ class SendGrid extends RestfulApiBase {
|
|
|
64
64
|
|
|
65
65
|
/** @override */
|
|
66
66
|
async getDefaultHeaders() {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
const headers = await super.getDefaultHeaders();
|
|
68
|
+
headers.set('Authorization', `Bearer ${this.apiKey}`);
|
|
69
|
+
return headers;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* No responseType is required for empty body response.
|
|
74
|
+
* @override
|
|
75
|
+
*/
|
|
76
|
+
getRequesterOptions() {
|
|
77
|
+
return {};
|
|
78
|
+
}
|
|
72
79
|
/**
|
|
73
80
|
* Generates an email object for Sendgrid.
|
|
74
81
|
* @param {!EmailOptions} options
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
// Copyright 2019 Google Inc.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @fileoverview API handler for Google Cloud Platform APIs.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
'use strict';
|
|
20
|
-
|
|
21
|
-
const {google} = require('googleapis');
|
|
22
|
-
const AuthClient = require('./auth_client.js');
|
|
23
|
-
|
|
24
|
-
const API_SCOPES = Object.freeze([
|
|
25
|
-
'https://www.googleapis.com/auth/cloud-platform',
|
|
26
|
-
]);
|
|
27
|
-
const API_VERSION = 'v1';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Google Cloud Platform API stubs. This class offers convenient request
|
|
31
|
-
* wrappers for some of Google Cloud APIs that are not supported by
|
|
32
|
-
* Google cloud client libraries.
|
|
33
|
-
*/
|
|
34
|
-
class CloudPlatformApis {
|
|
35
|
-
constructor(env = process.env) {
|
|
36
|
-
/** @const {!AuthClient} */
|
|
37
|
-
this.authClient = new AuthClient(API_SCOPES, env);
|
|
38
|
-
this.projectId = env['GCP_PROJECT'];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Gets the auth object.
|
|
43
|
-
* @return {!Promise<{!OAuth2Client|!JWT|!Compute}>}
|
|
44
|
-
*/
|
|
45
|
-
async getAuth_() {
|
|
46
|
-
if (this.auth) return this.auth;
|
|
47
|
-
await this.authClient.prepareCredentials();
|
|
48
|
-
this.auth = this.authClient.getDefaultAuth();
|
|
49
|
-
return this.auth;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Gets the GCP project Id. In Cloud Functions, it *should* be passed in
|
|
54
|
-
* through environment variable during the deployment. But if it doesn't exist
|
|
55
|
-
* (for example, in local unit tests), this function will fallback to ADC
|
|
56
|
-
* (Application Default Credential) auth's asynchronous function to get the
|
|
57
|
-
* project Id.
|
|
58
|
-
* @return {string}
|
|
59
|
-
* @private
|
|
60
|
-
*/
|
|
61
|
-
async getProjectId_() {
|
|
62
|
-
if (!this.projectId) this.projectId = await this.auth.getProjectId();
|
|
63
|
-
return this.projectId;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Returns the available permissions for the given project and permissions
|
|
68
|
-
* list. See:
|
|
69
|
-
* https://cloud.google.com/resource-manager/reference/rest/v1/projects/testIamPermissions.
|
|
70
|
-
* For more information of Permission, see:
|
|
71
|
-
* https://cloud.google.com/iam/docs/overview#permissions.
|
|
72
|
-
* @param {!Array<string>} permissions Permissions array.
|
|
73
|
-
* @return {!Promise<!Array<string>>} The available permissions that current
|
|
74
|
-
* operator (defined by ADC authentication information) has for the given
|
|
75
|
-
* permission list.
|
|
76
|
-
*/
|
|
77
|
-
async testIamPermissions(permissions) {
|
|
78
|
-
const resourceManager = google.cloudresourcemanager({
|
|
79
|
-
version: API_VERSION,
|
|
80
|
-
auth: await this.getAuth_(),
|
|
81
|
-
});
|
|
82
|
-
const projectId = await this.getProjectId_();
|
|
83
|
-
const request = {
|
|
84
|
-
resource_: projectId,
|
|
85
|
-
resource: {permissions: permissions},
|
|
86
|
-
};
|
|
87
|
-
return resourceManager.projects.testIamPermissions(request)
|
|
88
|
-
.then((response) => response.data.permissions);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
module.exports = {
|
|
93
|
-
CloudPlatformApis,
|
|
94
|
-
API_VERSION,
|
|
95
|
-
API_SCOPES,
|
|
96
|
-
};
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
// Copyright 2019 Google Inc.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @fileoverview This is Google Analytics Measurement Protocol based on nodejs
|
|
17
|
-
* 'request' library.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
'use strict';
|
|
21
|
-
|
|
22
|
-
const {request} = require('gaxios');
|
|
23
|
-
const {
|
|
24
|
-
getLogger,
|
|
25
|
-
SendSingleBatch,
|
|
26
|
-
BatchResult,
|
|
27
|
-
} = require('../components/utils.js');
|
|
28
|
-
/** Base URL for Google Analytics service. */
|
|
29
|
-
const BASE_URL = 'https://www.google-analytics.com';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Measurement Protocol hits are HTTP request. Reference:
|
|
33
|
-
* 1. Measurement Protocol Parameter Reference
|
|
34
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
|
|
35
|
-
* 2. Google Analytics Collection Limits and Quotas
|
|
36
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/limits-quotas
|
|
37
|
-
* 3. Validating Hits - Measurement Protocol
|
|
38
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits
|
|
39
|
-
* 4. Batching multiple hits in a single request
|
|
40
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#batch
|
|
41
|
-
*/
|
|
42
|
-
class MeasurementProtocol {
|
|
43
|
-
/**
|
|
44
|
-
* Measurement Protocol has a debug endpoint which will return the results of
|
|
45
|
-
* each hits. By given this initial parameter with 'true' value, this class
|
|
46
|
-
* can send hits to the debug endpoint.
|
|
47
|
-
* @param {boolean=} debugMode
|
|
48
|
-
*/
|
|
49
|
-
constructor(debugMode = false) {
|
|
50
|
-
this.debugMode = debugMode;
|
|
51
|
-
this.logger = getLogger('API.MP');
|
|
52
|
-
this.logger.debug(`Init ${this.constructor.name} with Debug Mode.`);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Returns the function to send out a Measurement Protocol request with
|
|
57
|
-
* multiple hits.
|
|
58
|
-
* @param {!Object<string,string>} config Measurement Protocol common
|
|
59
|
-
* configuration, e.g. web property ID.
|
|
60
|
-
* @return {!SendSingleBatch} Function which can send a batch of hits to
|
|
61
|
-
* Measurement Protocol.
|
|
62
|
-
*/
|
|
63
|
-
getSinglePingFn(config) {
|
|
64
|
-
/**
|
|
65
|
-
* Sends a batch of hits to Measurement Protocol.
|
|
66
|
-
* @param {!Array<string>} lines Data for single request. It should be
|
|
67
|
-
* guaranteed that it doesn't exceed quota limitation.
|
|
68
|
-
* @param {string} batchId The tag for log.
|
|
69
|
-
* @return {!Promise<BatchResult>}
|
|
70
|
-
*/
|
|
71
|
-
return async (lines, batchId) => {
|
|
72
|
-
const payload = lines.map((line) => {
|
|
73
|
-
const record = JSON.parse(line);
|
|
74
|
-
const hit = Object.assign({}, config, record);
|
|
75
|
-
return Object.keys(hit).map(
|
|
76
|
-
(key) => `${key}=${encodeURIComponent(hit[key])}`)
|
|
77
|
-
.join('&');
|
|
78
|
-
})
|
|
79
|
-
.join('\n');
|
|
80
|
-
// In debug mode, the path is fixed to '/debug/collect'.
|
|
81
|
-
const path = (this.debugMode) ? '/debug/collect' : '/batch';
|
|
82
|
-
const requestOptions = {
|
|
83
|
-
method: 'POST',
|
|
84
|
-
responseType: 'json',
|
|
85
|
-
url: `${BASE_URL}${path}`,
|
|
86
|
-
body: payload,
|
|
87
|
-
headers: {'User-Agent': 'Tentacles/MeasurementProtocol-v1'}
|
|
88
|
-
};
|
|
89
|
-
const response = await request(requestOptions);
|
|
90
|
-
/** @type {BatchResult} */ const batchResult = {
|
|
91
|
-
numberOfLines: lines.length,
|
|
92
|
-
};
|
|
93
|
-
if (response.status < 200 || response.status >= 300) {
|
|
94
|
-
const errorMessages = [
|
|
95
|
-
`Measurement Protocol [${batchId}] didn't succeed.`,
|
|
96
|
-
`Get response code: ${response.status}`,
|
|
97
|
-
`response: ${response.data}`,
|
|
98
|
-
];
|
|
99
|
-
this.logger.error(errorMessages.join('\n'));
|
|
100
|
-
batchResult.errors = errorMessages;
|
|
101
|
-
batchResult.result = false;
|
|
102
|
-
return batchResult;
|
|
103
|
-
}
|
|
104
|
-
this.logger.debug(`Configuration:`, config);
|
|
105
|
-
this.logger.debug(`Input Data: `, lines);
|
|
106
|
-
this.logger.debug(`Batch[${batchId}] status: ${response.status}`);
|
|
107
|
-
this.logger.debug(response.data);
|
|
108
|
-
// There is not enough information from the non-debug mode.
|
|
109
|
-
if (!this.debugMode) {
|
|
110
|
-
batchResult.result = true;
|
|
111
|
-
} else {
|
|
112
|
-
this.extraFailedLines_(batchResult, response.data.hitParsingResult,
|
|
113
|
-
lines);
|
|
114
|
-
}
|
|
115
|
-
return batchResult;
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Extras failed lines based on the hitParsingResult, see:
|
|
121
|
-
* https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits
|
|
122
|
-
*
|
|
123
|
-
* Note, only in 'debug' mode, Google Analytics will return this part of data.
|
|
124
|
-
*
|
|
125
|
-
* @param {!BatchResult} batchResult
|
|
126
|
-
* @param {!Array<!Object>} hitParsingResults
|
|
127
|
-
* @param {!Array<string>} lines The original input data.
|
|
128
|
-
* @private
|
|
129
|
-
*/
|
|
130
|
-
extraFailedLines_(batchResult, hitParsingResults, lines) {
|
|
131
|
-
batchResult.failedLines = [];
|
|
132
|
-
batchResult.groupedFailed = {};
|
|
133
|
-
const errors = new Set();
|
|
134
|
-
hitParsingResults.forEach((result, index) => {
|
|
135
|
-
if (!result.valid) {
|
|
136
|
-
const failedLine = lines[index];
|
|
137
|
-
batchResult.failedLines.push(failedLine);
|
|
138
|
-
result.parserMessage.forEach(({description: error, messageType}) => {
|
|
139
|
-
this.logger.info(`[${messageType}]: ${error} for ${failedLine}`);
|
|
140
|
-
if (messageType === 'ERROR') {
|
|
141
|
-
errors.add(error);
|
|
142
|
-
const groupedFailed = batchResult.groupedFailed[error] || [];
|
|
143
|
-
groupedFailed.push(failedLine);
|
|
144
|
-
if (groupedFailed.length === 1) {
|
|
145
|
-
batchResult.groupedFailed[error] = groupedFailed;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
batchResult.result = batchResult.failedLines.length === 0;
|
|
152
|
-
batchResult.errors = Array.from(errors);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
module.exports = {MeasurementProtocol};
|
package/src/components/automl.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
// Copyright 2019 Google Inc.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @fileoverview Google Cloud AutoML helper.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
'use strict';
|
|
20
|
-
const { ClientOptions } = require('google-gax');
|
|
21
|
-
const automl = require('@google-cloud/automl');
|
|
22
|
-
const google = automl.protos.google;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* For version `v1beta1`, BigQuery can be the source and destination.
|
|
26
|
-
* https://googleapis.dev/nodejs/automl/latest/google.cloud.automl.v1beta1.IBatchPredictInputConfig.html
|
|
27
|
-
* https://googleapis.dev/nodejs/automl/latest/google.cloud.automl.v1beta1.IBatchPredictOutputConfig.html
|
|
28
|
-
*
|
|
29
|
-
* For version `v1`, currently only Cloud Storage are allowed.
|
|
30
|
-
* https://googleapis.dev/nodejs/automl/latest/google.cloud.automl.v1.IBatchPredictInputConfig.html
|
|
31
|
-
* https://googleapis.dev/nodejs/automl/latest/google.cloud.automl.v1.IBatchPredictOutputConfig.html
|
|
32
|
-
*
|
|
33
|
-
* NOTE: currently use `v1beta1`
|
|
34
|
-
* @type {string}
|
|
35
|
-
*/
|
|
36
|
-
const API_VERSION = 'v1beta1';
|
|
37
|
-
const { PredictionServiceClient } = automl[API_VERSION];
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* AutoML Tables API class for:
|
|
41
|
-
* 1. start a batch predict job:
|
|
42
|
-
* @see https://cloud.google.com/nodejs/docs/reference/automl/latest/automl/v1beta1.predictionserviceclient#_google_cloud_automl_v1beta1_PredictionServiceClient_batchPredict_member_3_
|
|
43
|
-
* 2. get the status of an operation:
|
|
44
|
-
* @see https://cloud.google.com/nodejs/docs/reference/automl/latest/automl/v1beta1.predictionserviceclient#_google_cloud_automl_v1beta1_PredictionServiceClient_checkBatchPredictProgress_member_1_
|
|
45
|
-
*/
|
|
46
|
-
class AutoMl {
|
|
47
|
-
/**
|
|
48
|
-
* Initialize an instance.
|
|
49
|
-
* @param {ClientOptions=} options
|
|
50
|
-
*/
|
|
51
|
-
constructor(options = {}) {
|
|
52
|
-
this.client = new PredictionServiceClient(options);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Batch predicts based on Google Cloud Client Library.
|
|
57
|
-
* @param {string} projectId
|
|
58
|
-
* @param {string} computeRegion
|
|
59
|
-
* @param {string} modelId
|
|
60
|
-
* @param {google.cloud.automl.v1beta1.IBatchPredictInputConfig} inputConfig
|
|
61
|
-
* @param {google.cloud.automl.v1beta1.IBatchPredictOutputConfig} outputConfig
|
|
62
|
-
* @return {Promise<string>} Predict operation name.
|
|
63
|
-
*/
|
|
64
|
-
async batchPredict(
|
|
65
|
-
projectId,
|
|
66
|
-
computeRegion,
|
|
67
|
-
modelId,
|
|
68
|
-
inputConfig,
|
|
69
|
-
outputConfig
|
|
70
|
-
) {
|
|
71
|
-
const modelFullId = this.client.modelPath(
|
|
72
|
-
projectId,
|
|
73
|
-
computeRegion,
|
|
74
|
-
modelId
|
|
75
|
-
);
|
|
76
|
-
const responses = await this.client.batchPredict({
|
|
77
|
-
name: modelFullId,
|
|
78
|
-
inputConfig,
|
|
79
|
-
outputConfig,
|
|
80
|
-
});
|
|
81
|
-
const operation = responses[1];
|
|
82
|
-
console.log(`Operation name: ${operation.name}`);
|
|
83
|
-
return operation.name;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Gets status of an operation.
|
|
88
|
-
* @param {string} operationName
|
|
89
|
-
* @return {Promise<{{
|
|
90
|
-
* done: boolean,
|
|
91
|
-
* error: Error|undefined,
|
|
92
|
-
* metadata: OperationMetadata,
|
|
93
|
-
* name: string,
|
|
94
|
-
* }}>}
|
|
95
|
-
*/
|
|
96
|
-
async getOperation(operationName) {
|
|
97
|
-
const response = await this.client.checkBatchPredictProgress(operationName);
|
|
98
|
-
const { done, error, metadata, name } = response;
|
|
99
|
-
return { done, error, metadata, name };
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
exports.AutoMl = AutoMl;
|