@google-cloud/nodejs-common 2.2.2 → 2.3.0
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 +15 -17
- package/src/apis/base/google_api_client.js +1 -1
- package/src/apis/doubleclick_bidmanager.js +3 -3
- package/src/apis/gmail.js +113 -0
- package/src/apis/index.js +10 -15
- package/src/apis/sendgrid.js +62 -1
- package/src/apis/youtube.js +0 -284
- package/src/components/firestore/data_access_object.js +3 -3
- package/src/apis/google_ads.js +0 -1145
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@google-cloud/nodejs-common",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
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,28 +16,26 @@
|
|
|
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": "^3.
|
|
19
|
+
"@google-cloud/aiplatform": "^3.25.0",
|
|
20
20
|
"@google-cloud/automl": "^4.0.1",
|
|
21
|
-
"@google-cloud/bigquery": "^7.
|
|
22
|
-
"@google-cloud/datastore": "^
|
|
23
|
-
"@google-cloud/firestore": "^7.
|
|
21
|
+
"@google-cloud/bigquery": "^7.8.0",
|
|
22
|
+
"@google-cloud/datastore": "^9.1.0",
|
|
23
|
+
"@google-cloud/firestore": "^7.9.0",
|
|
24
24
|
"@google-cloud/logging-winston": "^6.0.0",
|
|
25
|
-
"@google-cloud/pubsub": "^4.
|
|
26
|
-
"@google-cloud/storage": "^7.
|
|
27
|
-
"@google-cloud/scheduler": "^4.
|
|
28
|
-
"@google-cloud/secret-manager": "^5.
|
|
29
|
-
"gaxios": "^6.
|
|
25
|
+
"@google-cloud/pubsub": "^4.5.0",
|
|
26
|
+
"@google-cloud/storage": "^7.12.0",
|
|
27
|
+
"@google-cloud/scheduler": "^4.3.0",
|
|
28
|
+
"@google-cloud/secret-manager": "^5.6.0",
|
|
29
|
+
"gaxios": "^6.7.0",
|
|
30
30
|
"google-ads-nodejs-client": "16.0.0",
|
|
31
|
-
"google-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"winston": "^3.13.0",
|
|
36
|
-
"@grpc/grpc-js": "^1.10.5",
|
|
31
|
+
"google-auth-library": "^9.11.0",
|
|
32
|
+
"googleapis": "^140.0.1",
|
|
33
|
+
"winston": "^3.13.1",
|
|
34
|
+
"@grpc/grpc-js": "^1.11.1",
|
|
37
35
|
"lodash": "^4.17.21"
|
|
38
36
|
},
|
|
39
37
|
"devDependencies": {
|
|
40
|
-
"jasmine": "^5.
|
|
38
|
+
"jasmine": "^5.2.0"
|
|
41
39
|
},
|
|
42
40
|
"scripts": {
|
|
43
41
|
"test": "node node_modules/jasmine/bin/jasmine"
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* @fileoverview A base class for Google Api client library class.
|
|
17
17
|
*/
|
|
18
18
|
const { google } = require('googleapis');
|
|
19
|
-
const { getLogger
|
|
19
|
+
const { getLogger } = require('../../components/utils.js');
|
|
20
20
|
const { AuthRestfulApi } = require('./auth_restful_api.js');
|
|
21
21
|
|
|
22
22
|
/**
|
|
@@ -94,13 +94,13 @@ class DoubleClickBidManager extends GoogleApiClient {
|
|
|
94
94
|
* Gets a query metadata.
|
|
95
95
|
* See https://developers.google.com/bid-manager/reference/rest/v2/queries/get
|
|
96
96
|
* @param {number} queryId Id of the query.
|
|
97
|
-
* @return {!Promise<!
|
|
98
|
-
* https://developers.google.com/bid-manager/reference/rest/v2/queries#
|
|
97
|
+
* @return {!Promise<!Query>} Query, see
|
|
98
|
+
* https://developers.google.com/bid-manager/reference/rest/v2/queries#Query
|
|
99
99
|
*/
|
|
100
100
|
async getQuery(queryId) {
|
|
101
101
|
const doubleclickbidmanager = await this.getApiClient();
|
|
102
102
|
const response = await doubleclickbidmanager.queries.get({ queryId });
|
|
103
|
-
return response.data
|
|
103
|
+
return response.data;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
/**
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Copyright 2024 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
|
+
/** @fileoverview Gmail API Client Library. */
|
|
16
|
+
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
const { GoogleApiClient } = require('./base/google_api_client.js');
|
|
20
|
+
|
|
21
|
+
const API_SCOPES =
|
|
22
|
+
Object.freeze(['https://www.googleapis.com/auth/gmail.send']);
|
|
23
|
+
const API_VERSION = 'v1';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {{
|
|
27
|
+
* from: string|undefined,
|
|
28
|
+
* to: string|Array<string>,
|
|
29
|
+
* cc: string|Array<string>|undefined,
|
|
30
|
+
* bcc: string|Array<string>|undefined,
|
|
31
|
+
* 'reply-to': string|Array<string>|undefined,
|
|
32
|
+
* subject: string,
|
|
33
|
+
* content: string,
|
|
34
|
+
* type: 'text/plain'|'text/html'|undefined,
|
|
35
|
+
* }}
|
|
36
|
+
*/
|
|
37
|
+
let EmailOptions;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Email sending class based on Gmail API.
|
|
41
|
+
*/
|
|
42
|
+
class Gmail extends GoogleApiClient {
|
|
43
|
+
|
|
44
|
+
constructor(env = process.env) {
|
|
45
|
+
super(env);
|
|
46
|
+
this.googleApi = 'gmail';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @override */
|
|
50
|
+
getScope() {
|
|
51
|
+
return API_SCOPES;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @override */
|
|
55
|
+
getVersion() {
|
|
56
|
+
return API_VERSION;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Generates a string for an email in RFC 2822 format.
|
|
61
|
+
* If 'from' is different from the OAuth token's owner account, it would be
|
|
62
|
+
* ignored.
|
|
63
|
+
* @param {!EmailOptions} options
|
|
64
|
+
* @return {string}
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
getRawEmail_(options = {}) {
|
|
68
|
+
const message = [];
|
|
69
|
+
['from', 'to', 'cc', 'bcc', 'reply-to', 'subject'].forEach((key) => {
|
|
70
|
+
if (options[key]) {
|
|
71
|
+
const value = Array.isArray(options[key])
|
|
72
|
+
? options[key].join(',') : options[key];
|
|
73
|
+
message.push(`${key}: ${value}`);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (message.length === 0)
|
|
77
|
+
throw new Error(`Can not get email from ${options}`);
|
|
78
|
+
message.push('\n');
|
|
79
|
+
message.push(options.content);
|
|
80
|
+
return message.join('\n');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Sends a simple email (no attachment).
|
|
85
|
+
* @see https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send
|
|
86
|
+
* @param {!EmailOptions} options
|
|
87
|
+
* @return {!Message}
|
|
88
|
+
*/
|
|
89
|
+
async sendSimpleEmail(options) {
|
|
90
|
+
const email = this.getRawEmail_(options);
|
|
91
|
+
|
|
92
|
+
const gmail = await this.getApiClient();
|
|
93
|
+
const message = {
|
|
94
|
+
userId: 'me',
|
|
95
|
+
requestBody: { raw: Buffer.from(email).toString('base64') }
|
|
96
|
+
};
|
|
97
|
+
try {
|
|
98
|
+
const response = await gmail.users.messages.send(message);
|
|
99
|
+
return { responseStatus: response.status };
|
|
100
|
+
} catch (error) {
|
|
101
|
+
this.logger.error('Result of sending an email', JSON.stringify(error));
|
|
102
|
+
return { responseStatus: error.status };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports = {
|
|
109
|
+
Gmail,
|
|
110
|
+
EmailOptions,
|
|
111
|
+
API_SCOPES,
|
|
112
|
+
API_VERSION,
|
|
113
|
+
};
|
package/src/apis/index.js
CHANGED
|
@@ -99,18 +99,6 @@ exports.doubleclickbidmanager = require('./doubleclick_bidmanager.js');
|
|
|
99
99
|
*/
|
|
100
100
|
exports.bigquery = require('./bigquery.js');
|
|
101
101
|
|
|
102
|
-
/**
|
|
103
|
-
* APIs integration class for Google Ads.
|
|
104
|
-
* @const {{
|
|
105
|
-
* GoogleAds:!GoogleAds,
|
|
106
|
-
* ConversionConfig:!ConversionConfig,
|
|
107
|
-
* CustomerMatchConfig: !CustomerMatchConfig,
|
|
108
|
-
* CustomerMatchRecord: !CustomerMatchRecord,
|
|
109
|
-
* ReportQueryConfig:!ReportQueryConfig,
|
|
110
|
-
* }}
|
|
111
|
-
*/
|
|
112
|
-
exports.googleads = require('./google_ads.js');
|
|
113
|
-
|
|
114
102
|
/**
|
|
115
103
|
* APIs integration class for Google Ads with Google API library.
|
|
116
104
|
* @const {{
|
|
@@ -146,9 +134,7 @@ exports.measurementprotocolga4 = require('./measurement_protocol_ga4.js');
|
|
|
146
134
|
/**
|
|
147
135
|
* APIs integration class for YouTube.
|
|
148
136
|
* @const {{
|
|
149
|
-
* YouTube:!YouTube
|
|
150
|
-
* ListChannelsConfig: !ListChannelsConfig,
|
|
151
|
-
* ListVideosConfig: !ListVideosConfig,
|
|
137
|
+
* YouTube:!YouTube
|
|
152
138
|
* }}
|
|
153
139
|
*/
|
|
154
140
|
exports.youtube = require('./youtube.js');
|
|
@@ -162,3 +148,12 @@ exports.youtube = require('./youtube.js');
|
|
|
162
148
|
* }}
|
|
163
149
|
*/
|
|
164
150
|
exports.sendgrid = require('./sendgrid.js');
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* APIs integration class for Gmail.
|
|
154
|
+
* @const {{
|
|
155
|
+
* Gmail:!Gmail,
|
|
156
|
+
* API_VERSION: string,
|
|
157
|
+
* }}
|
|
158
|
+
*/
|
|
159
|
+
exports.gmail = require('./gmail.js');
|
package/src/apis/sendgrid.js
CHANGED
|
@@ -18,12 +18,33 @@
|
|
|
18
18
|
|
|
19
19
|
'use strict';
|
|
20
20
|
|
|
21
|
+
const { EmailOptions } = require('./gmail.js');
|
|
21
22
|
const { RestfulApiBase } = require('./base/restful_api_base.js');
|
|
22
23
|
const { getLogger } = require('../components/utils.js');
|
|
23
24
|
|
|
24
25
|
const API_VERSION = 'v3';
|
|
25
26
|
const API_ENDPOINT = 'https://api.sendgrid.com';
|
|
26
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Returns a SendGrid email object based on a given RFC 2822 `mailbox` string.
|
|
30
|
+
* For `mailbox` @see https://www.rfc-editor.org/rfc/rfc2822#page-15
|
|
31
|
+
* @param {string} mailbox
|
|
32
|
+
* @return {{name: string, email: string}} A SendGrid email object.
|
|
33
|
+
*/
|
|
34
|
+
function getSendGridEmail(mailbox) {
|
|
35
|
+
if (typeof mailbox === 'string') {
|
|
36
|
+
if (mailbox.indexOf('<') > -1) {
|
|
37
|
+
const name = mailbox.substring(0, mailbox.indexOf('<')).trim();
|
|
38
|
+
const email =
|
|
39
|
+
mailbox.substring(mailbox.indexOf('<') + 1, mailbox.indexOf('>'));
|
|
40
|
+
return { name, email };
|
|
41
|
+
} else {
|
|
42
|
+
return { email: mailbox.trim() };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return mailbox;
|
|
46
|
+
}
|
|
47
|
+
|
|
27
48
|
/**
|
|
28
49
|
* SendGrid API access class.
|
|
29
50
|
* @see https://docs.sendgrid.com/api-reference/how-to-use-the-sendgrid-v3-api/authentication
|
|
@@ -48,11 +69,51 @@ class SendGrid extends RestfulApiBase {
|
|
|
48
69
|
});
|
|
49
70
|
}
|
|
50
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Generates an email object for Sendgrid.
|
|
74
|
+
* @param {!EmailOptions} options
|
|
75
|
+
* @return {object}
|
|
76
|
+
* @see https://www.twilio.com/docs/sendgrid/api-reference/mail-send/mail-send#reques
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
79
|
+
getEmail_(options = {}) {
|
|
80
|
+
const {
|
|
81
|
+
from,
|
|
82
|
+
'reply-to': reply_to,
|
|
83
|
+
subject,
|
|
84
|
+
content,
|
|
85
|
+
type = 'text/plain',
|
|
86
|
+
} = options;
|
|
87
|
+
const recipientObject = {};
|
|
88
|
+
['to', 'cc', 'bcc'].forEach((key) => {
|
|
89
|
+
if (options[key]) {
|
|
90
|
+
recipientObject[key] = [options[key]].flat().map(getSendGridEmail);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
const email = {
|
|
94
|
+
subject,
|
|
95
|
+
content: [{ type, value: content }],
|
|
96
|
+
personalizations: [recipientObject],
|
|
97
|
+
};
|
|
98
|
+
if (from) email.from = getSendGridEmail(from);
|
|
99
|
+
//@see https://www.twilio.com/docs/sendgrid/api-reference/mail-send/mail-send#reques
|
|
100
|
+
if (reply_to) {
|
|
101
|
+
if (Array.isArray(reply_to)) {
|
|
102
|
+
email.reply_to_list = reply_to.map(getSendGridEmail);
|
|
103
|
+
} else {
|
|
104
|
+
email.reply_to = getSendGridEmail(reply_to);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return email;
|
|
108
|
+
}
|
|
109
|
+
|
|
51
110
|
/**
|
|
52
111
|
* Sends an email.
|
|
112
|
+
* @param {!EmailOptions} options
|
|
53
113
|
* @see https://docs.sendgrid.com/api-reference/mail-send/mail-send
|
|
54
114
|
*/
|
|
55
|
-
async sendMail(
|
|
115
|
+
async sendMail(options) {
|
|
116
|
+
const email = this.getEmail_(options);
|
|
56
117
|
const response = await this.request('mail/send', 'POST', email);
|
|
57
118
|
return response;
|
|
58
119
|
}
|
package/src/apis/youtube.js
CHANGED
|
@@ -34,130 +34,9 @@ const API_SCOPES = Object.freeze([
|
|
|
34
34
|
]);
|
|
35
35
|
const API_VERSION = 'v3';
|
|
36
36
|
|
|
37
|
-
/**
|
|
38
|
-
* Configuration for listing youtube channels.
|
|
39
|
-
* @see https://developers.google.com/youtube/v3/docs/channels/list
|
|
40
|
-
* @typedef {{
|
|
41
|
-
* part: Array<string>,
|
|
42
|
-
* categoryId: (string|undefined),
|
|
43
|
-
* forUsername: (string|undefined),
|
|
44
|
-
* id: (string|undefined),
|
|
45
|
-
* managedByMe: (boolean|undefined),
|
|
46
|
-
* mine: (boolean|undefined),
|
|
47
|
-
* hl: (string|undefined),
|
|
48
|
-
* onBehalfOfContentOwner: (string|undefined),
|
|
49
|
-
* pageToken: (string|undefined),
|
|
50
|
-
* }}
|
|
51
|
-
*/
|
|
52
|
-
let ListChannelsConfig;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Configuration for listing youtube videos.
|
|
56
|
-
* @see https://developers.google.com/youtube/v3/docs/videos/list
|
|
57
|
-
* @typedef {{
|
|
58
|
-
* part: Array<string>,
|
|
59
|
-
* id: (string|undefined),
|
|
60
|
-
* chart: (string|undefined),
|
|
61
|
-
* hl: (string|undefined),
|
|
62
|
-
* maxHeight: (unsigned integer|undefined),
|
|
63
|
-
* maxResults: (unsigned integer|undefined),
|
|
64
|
-
* maxWidth: (unsigned integer|undefined),
|
|
65
|
-
* onBehalfOfContentOwner: (string|undefined),
|
|
66
|
-
* pageToken: (string|undefined),
|
|
67
|
-
* regionCode: (string|undefined),
|
|
68
|
-
* }}
|
|
69
|
-
*/
|
|
70
|
-
let ListVideosConfig;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Configuration for listing youtube comment threads by the given id.
|
|
74
|
-
* @see https://developers.google.com/youtube/v3/docs/commentThreads/list
|
|
75
|
-
* @typedef {{
|
|
76
|
-
* part: Array<string>,
|
|
77
|
-
* id: (string|undefined),
|
|
78
|
-
* allThreadsRelatedToChannelId: (string|undefined),
|
|
79
|
-
* channelId: (string|undefined),
|
|
80
|
-
* videoId: (string|undefined),
|
|
81
|
-
* maxResults: (unsigned integer|undefined),
|
|
82
|
-
* moderationStatus: (string|undefined),
|
|
83
|
-
* order: (string|undefined),
|
|
84
|
-
* pageToken: (string|undefined),
|
|
85
|
-
* searchTerms: (string|undefined),
|
|
86
|
-
* textFormat: (string|undefined),
|
|
87
|
-
* limit: (unsigned integer|undefined),
|
|
88
|
-
* }}
|
|
89
|
-
*/
|
|
90
|
-
let ListCommentThreadsConfig;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Configuration for listing youtube play list.
|
|
94
|
-
* @see https://developers.google.com/youtube/v3/docs/Playlists/list
|
|
95
|
-
* @typedef {{
|
|
96
|
-
* part: Array<string>,
|
|
97
|
-
* channelId: (string|undefined),
|
|
98
|
-
* id: (string|undefined),
|
|
99
|
-
* mine: (boolean|undefined),
|
|
100
|
-
* hl: (string|undefined),
|
|
101
|
-
* maxResults: (unsigned integer|undefined),
|
|
102
|
-
* onBehalfOfContentOwner: (string|undefined),
|
|
103
|
-
* onBehalfOfContentOwnerChannel: (string|undefined),
|
|
104
|
-
* pageToken: (string|undefined)
|
|
105
|
-
* }}
|
|
106
|
-
*/
|
|
107
|
-
let ListPlaylistConfig;
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Configuration for listing youtube search results.
|
|
111
|
-
* @see https://developers.google.com/youtube/v3/docs/search/list
|
|
112
|
-
* @typedef {{
|
|
113
|
-
* part: Array<string>,
|
|
114
|
-
* forContentOwner: (boolean|undefined),
|
|
115
|
-
* forDeveloper: (boolean|undefined),
|
|
116
|
-
* forMine: (boolean|undefined),
|
|
117
|
-
* relatedToVideoId: (string|undefined),
|
|
118
|
-
* channelId: (string|undefined),
|
|
119
|
-
* channelType: (string|undefined),
|
|
120
|
-
* eventType: (string|undefined),
|
|
121
|
-
* location: (string|undefined),
|
|
122
|
-
* locationRadius: (string|undefined),
|
|
123
|
-
* maxResults: (unsigned integer|undefined),
|
|
124
|
-
* onBehalfOfContentOwner: (string|undefined),
|
|
125
|
-
* order: (string|undefined),
|
|
126
|
-
* pageToken: (string|undefined),
|
|
127
|
-
* publishedAfter: (datetime|undefined),
|
|
128
|
-
* publishedBefore: (datetime|undefined),
|
|
129
|
-
* q: (string|undefined),
|
|
130
|
-
* regionCode: (string|undefined),
|
|
131
|
-
* relevanceLanguage: (string|undefined),
|
|
132
|
-
* safeSearch: (string|undefined),
|
|
133
|
-
* topicId: (string|undefined),
|
|
134
|
-
* type: (string|undefined),
|
|
135
|
-
* videoCaption: (string|undefined),
|
|
136
|
-
* videoCategoryId: (string|undefined),
|
|
137
|
-
* videoDefinition: (string|undefined),
|
|
138
|
-
* videoDimension: (string|undefined),
|
|
139
|
-
* videoDuration: (string|undefined),
|
|
140
|
-
* videoEmbeddable: (string|undefined),
|
|
141
|
-
* videoLicense: (string|undefined),
|
|
142
|
-
* videoSyndicated: (string|undefined),
|
|
143
|
-
* videoType: (string|undefined)
|
|
144
|
-
* }}
|
|
145
|
-
*/
|
|
146
|
-
let ListSearchConfig;
|
|
147
|
-
|
|
148
37
|
/**
|
|
149
38
|
* Youtube API v3 stub.
|
|
150
39
|
* See: https://developers.google.com/youtube/v3/docs
|
|
151
|
-
* Channel list type definition, see:
|
|
152
|
-
* https://developers.google.com/youtube/v3/docs/channels/list
|
|
153
|
-
* Video list type definition, see:
|
|
154
|
-
* https://developers.google.com/youtube/v3/docs/videos/list
|
|
155
|
-
* CommentThread list type definition, see:
|
|
156
|
-
* https://developers.google.com/youtube/v3/docs/commentThreads/list
|
|
157
|
-
* Playlist list type definition, see:
|
|
158
|
-
* https://developers.google.com/youtube/v3/docs/playlists/list
|
|
159
|
-
* Search list type definition, see:
|
|
160
|
-
* https://developers.google.com/youtube/v3/docs/search/list
|
|
161
40
|
*/
|
|
162
41
|
class YouTube extends GoogleApiClient {
|
|
163
42
|
/**
|
|
@@ -180,173 +59,10 @@ class YouTube extends GoogleApiClient {
|
|
|
180
59
|
getVersion() {
|
|
181
60
|
return API_VERSION;
|
|
182
61
|
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Returns a collection of zero or more channel resources that match the
|
|
186
|
-
* request criteria.
|
|
187
|
-
* @see https://developers.google.com/youtube/v3/docs/channels/list
|
|
188
|
-
* @param {!ListChannelsConfig} config List channels configuration.
|
|
189
|
-
* @return {!Promise<Array<Schema$Channel>>}
|
|
190
|
-
*/
|
|
191
|
-
async listChannels(config) {
|
|
192
|
-
const channelListRequest = Object.assign({}, config);
|
|
193
|
-
channelListRequest.part = channelListRequest.part.join(',')
|
|
194
|
-
try {
|
|
195
|
-
const youtube = await this.getApiClient();
|
|
196
|
-
const response = await youtube.channels.list(channelListRequest);
|
|
197
|
-
this.logger.debug('Response: ', response);
|
|
198
|
-
return response.data.items;
|
|
199
|
-
} catch (error) {
|
|
200
|
-
const errorMsg =
|
|
201
|
-
`Fail to list channels: ${JSON.stringify(channelListRequest)}`;
|
|
202
|
-
this.logger.error('YouTube list channels failed.', error.message);
|
|
203
|
-
this.logger.debug('Errors in response:', error);
|
|
204
|
-
throw new Error(errorMsg);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Returns a list of videos that match the API request parameters.
|
|
210
|
-
* @see https://developers.google.com/youtube/v3/docs/videos/list
|
|
211
|
-
* @param {!ListVideosConfig} config List videos configuration.
|
|
212
|
-
* @return {!Promise<Array<Schema$Video>>}
|
|
213
|
-
*/
|
|
214
|
-
async listVideos(config) {
|
|
215
|
-
const videoListRequest = Object.assign({}, config);
|
|
216
|
-
videoListRequest.part = videoListRequest.part.join(',')
|
|
217
|
-
try {
|
|
218
|
-
const youtube = await this.getApiClient();
|
|
219
|
-
const response = await youtube.videos.list(videoListRequest);
|
|
220
|
-
this.logger.debug('Response: ', response);
|
|
221
|
-
return response.data.items;
|
|
222
|
-
} catch (error) {
|
|
223
|
-
const errorMsg =
|
|
224
|
-
`Fail to list videos: ${JSON.stringify(videoListRequest)}`;
|
|
225
|
-
this.logger.error('YouTube list videos failed.', error.message);
|
|
226
|
-
this.logger.debug('Errors in response:', error);
|
|
227
|
-
throw new Error(errorMsg);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Returns a list of comment threads that match the API request parameters.
|
|
233
|
-
* @see https://developers.google.com/youtube/v3/docs/videos/list
|
|
234
|
-
* @param {!ListCommentThreadsConfig} config List comment threads
|
|
235
|
-
* configuration.
|
|
236
|
-
* @return {!Promise<Array<Schema$CommentThread>>}
|
|
237
|
-
*/
|
|
238
|
-
async listCommentThreads(config) {
|
|
239
|
-
const commentThreadsRequest = Object.assign({}, config);
|
|
240
|
-
commentThreadsRequest.part = commentThreadsRequest.part.join(',')
|
|
241
|
-
try {
|
|
242
|
-
const youtube = await this.getApiClient();
|
|
243
|
-
const response = await youtube.commentThreads.list(commentThreadsRequest);
|
|
244
|
-
this.logger.debug('Response: ', response.data);
|
|
245
|
-
return response.data.items;
|
|
246
|
-
} catch (error) {
|
|
247
|
-
const errorMsg =
|
|
248
|
-
`Fail to list comment threads: ${JSON.stringify(commentThreadsRequest)}`;
|
|
249
|
-
this.logger.error('YouTube list comment threads failed.', error.message);
|
|
250
|
-
this.logger.debug('Errors in response:', error);
|
|
251
|
-
throw new Error(errorMsg);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Returns a collection of playlists that match the API request parameters.
|
|
257
|
-
* @see https://developers.google.com/youtube/v3/docs/playlists/list
|
|
258
|
-
* @param {!ListPlaylistConfig} config List playlist configuration.
|
|
259
|
-
* @param {number} resultLimit The limit of the number of results.
|
|
260
|
-
* @param {string} pageToken Token to identify a specific page in the result.
|
|
261
|
-
* @return {!Promise<Array<Schema$Playlist>>}
|
|
262
|
-
*/
|
|
263
|
-
async listPlaylists(config, resultLimit = 1000, pageToken = null) {
|
|
264
|
-
if (resultLimit <= 0) return [];
|
|
265
|
-
|
|
266
|
-
const playlistsRequest = Object.assign({
|
|
267
|
-
// auth: this.auth,
|
|
268
|
-
}, config, {
|
|
269
|
-
pageToken
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
if (Array.isArray(playlistsRequest.part)) {
|
|
273
|
-
playlistsRequest.part = playlistsRequest.part.join(',');
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
try {
|
|
277
|
-
const youtube = await this.getApiClient();
|
|
278
|
-
const response = await youtube.playlists.list(playlistsRequest);
|
|
279
|
-
this.logger.debug('Response: ', response.data);
|
|
280
|
-
if (response.data.nextPageToken) {
|
|
281
|
-
this.logger.debug(
|
|
282
|
-
'Call youtube playlist:list API agian with Token: ',
|
|
283
|
-
response.data.nextPageToken);
|
|
284
|
-
const nextResponse = await this.listPlaylists(
|
|
285
|
-
config,
|
|
286
|
-
resultLimit - response.data.items.length,
|
|
287
|
-
response.data.nextPageToken);
|
|
288
|
-
return response.data.items.concat(nextResponse);
|
|
289
|
-
}
|
|
290
|
-
return response.data.items;
|
|
291
|
-
} catch (error) {
|
|
292
|
-
const errorMsg =
|
|
293
|
-
`Fail to list playlists: ${JSON.stringify(playlistsRequest)}`;
|
|
294
|
-
this.logger.error('YouTube list playlists failed.', error.message);
|
|
295
|
-
this.logger.debug('Errors in response:', error);
|
|
296
|
-
throw new Error(errorMsg);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Returns a collection of search results that match the query parameters
|
|
302
|
-
* specified in the API request.
|
|
303
|
-
* @see https://developers.google.com/youtube/v3/docs/search/list
|
|
304
|
-
* @param {!ListSearchConfig} config List search result configuration.
|
|
305
|
-
* @param {number} resultLimit The limit of the number of results.
|
|
306
|
-
* @param {string} pageToken Token to identify a specific page in the result.
|
|
307
|
-
* @return {!Promise<Array<Schema$Search>>}
|
|
308
|
-
*/
|
|
309
|
-
async listSearchResults(config, resultLimit = 1000, pageToken = null) {
|
|
310
|
-
if (resultLimit <= 0) return [];
|
|
311
|
-
|
|
312
|
-
const searchRequest = Object.assign({}, config, { pageToken });
|
|
313
|
-
|
|
314
|
-
if (Array.isArray(searchRequest.part)) {
|
|
315
|
-
searchRequest.part = searchRequest.part.join(',');
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
try {
|
|
319
|
-
const youtube = await this.getApiClient();
|
|
320
|
-
const response = await youtube.search.list(searchRequest);
|
|
321
|
-
this.logger.debug('Response: ', response.data);
|
|
322
|
-
if (response.data.nextPageToken) {
|
|
323
|
-
this.logger.debug(
|
|
324
|
-
'Call youtube search:list API agian with Token: ',
|
|
325
|
-
response.data.nextPageToken);
|
|
326
|
-
const nextResponse = await this.listSearchResults(
|
|
327
|
-
config,
|
|
328
|
-
resultLimit - response.data.items.length,
|
|
329
|
-
response.data.nextPageToken);
|
|
330
|
-
return response.data.items.concat(nextResponse);
|
|
331
|
-
}
|
|
332
|
-
return response.data.items;
|
|
333
|
-
} catch (error) {
|
|
334
|
-
const errorMsg =
|
|
335
|
-
`Fail to list search results: ${JSON.stringify(searchRequest)}`;
|
|
336
|
-
this.logger.error('YouTube list search results failed.', error.message);
|
|
337
|
-
this.logger.debug('Errors in response:', error);
|
|
338
|
-
throw new Error(errorMsg);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
62
|
}
|
|
342
63
|
|
|
343
64
|
module.exports = {
|
|
344
65
|
YouTube,
|
|
345
|
-
ListChannelsConfig,
|
|
346
|
-
ListVideosConfig,
|
|
347
|
-
ListCommentThreadsConfig,
|
|
348
|
-
ListPlaylistConfig,
|
|
349
|
-
ListSearchConfig,
|
|
350
66
|
API_VERSION,
|
|
351
67
|
API_SCOPES,
|
|
352
68
|
};
|
|
@@ -54,16 +54,16 @@ class DataAccessObject {
|
|
|
54
54
|
/** @const {string} */ this.namespace = namespace;
|
|
55
55
|
/** @const {!DataSource} */ this.dataSource =
|
|
56
56
|
typeof database === 'string' ? database : database.source;
|
|
57
|
-
|
|
57
|
+
this.databaseId = database.id || DEFAULT_DATABASE;
|
|
58
58
|
/** @type {!FirestoreAccessBase} */ this.accessObject = undefined;
|
|
59
59
|
switch (this.dataSource) {
|
|
60
60
|
case DataSource.FIRESTORE:
|
|
61
61
|
this.accessObject = new NativeModeAccess(
|
|
62
|
-
`${this.namespace}/database/${kind}`, projectId, databaseId);
|
|
62
|
+
`${this.namespace}/database/${kind}`, projectId, this.databaseId);
|
|
63
63
|
break;
|
|
64
64
|
case DataSource.DATASTORE:
|
|
65
65
|
this.accessObject = new DatastoreModeAccess(this.namespace, kind,
|
|
66
|
-
projectId, databaseId);
|
|
66
|
+
projectId, this.databaseId);
|
|
67
67
|
break;
|
|
68
68
|
default:
|
|
69
69
|
throw new Error(`Unknown DataSource item: ${this.dataSource}.`);
|