Skip to content

Commit

Permalink
Updated versions of dependecies; refactory internal api classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
lushu committed Apr 9, 2024
1 parent 10569cf commit ea26e84
Show file tree
Hide file tree
Showing 49 changed files with 1,236 additions and 490 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,7 @@ set_cloud_functions_default_settings() {
default_cf_flag+=(--set-env-vars=DEBUG="${DEBUG}")
default_cf_flag+=(--set-env-vars=IN_GCP="${IN_GCP}")
default_cf_flag+=(--set-env-vars=DATABASE_ID="${DATABASE_ID}")
default_cf_flag+=(--set-env-vars=DATABASE_MODE="${DATABASE_MODE}")
}

#######################################
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@google-cloud/nodejs-common",
"version": "2.1.0",
"version": "2.2.0",
"description": "A NodeJs common library for solutions based on Cloud Functions",
"author": "Google Inc.",
"license": "Apache-2.0",
Expand All @@ -16,24 +16,24 @@
},
"homepage": "https://github.com/GoogleCloudPlatform/cloud-for-marketing/blob/master/marketing-analytics/activation/common-libs/nodejs-common/README.md",
"dependencies": {
"@google-cloud/aiplatform": "^3.10.0",
"@google-cloud/aiplatform": "^3.17.0",
"@google-cloud/automl": "^4.0.1",
"@google-cloud/bigquery": "^7.3.0",
"@google-cloud/datastore": "^8.4.0",
"@google-cloud/firestore": "^7.2.0",
"@google-cloud/bigquery": "^7.5.2",
"@google-cloud/datastore": "^8.6.0",
"@google-cloud/firestore": "^7.5.0",
"@google-cloud/logging-winston": "^6.0.0",
"@google-cloud/pubsub": "^4.1.1",
"@google-cloud/storage": "^7.7.0",
"@google-cloud/scheduler": "^4.0.1",
"@google-cloud/secret-manager": "^5.0.1",
"gaxios": "^6.1.1",
"@google-cloud/pubsub": "^4.3.3",
"@google-cloud/storage": "^7.9.0",
"@google-cloud/scheduler": "^4.1.0",
"@google-cloud/secret-manager": "^5.2.0",
"gaxios": "^6.3.0",
"google-ads-nodejs-client": "16.0.0",
"google-ads-api": "^14.1.0",
"google-ads-node": "^12.0.2",
"google-auth-library": "^9.4.2",
"googleapis": "^131.0.0",
"winston": "^3.10.0",
"@grpc/grpc-js": "1.9.14",
"google-auth-library": "^9.7.0",
"googleapis": "^134.0.0",
"winston": "^3.13.0",
"@grpc/grpc-js": "^1.10.5",
"lodash": "^4.17.21"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

const stream = require('stream');
const {google} = require('googleapis');
const { GoogleApiClient } = require('./base/google_api_client.js');
const {Schema$Upload} = google.analytics;
const AuthClient = require('./auth_client.js');
const {wait, getLogger, BatchResult} = require('../components/utils.js');
Expand Down Expand Up @@ -55,31 +56,26 @@ let DataImportClearConfig;
/**
* Google Analytics API v3 stub.
*/
class Analytics {
class Analytics extends GoogleApiClient {
/**
* @constructor
* @param {!Object<string,string>=} env The environment object to hold env
* variables.
*/
constructor(env = process.env) {
this.authClient = new AuthClient(API_SCOPES, env);
super(env);
this.googleApi = 'analytics';
this.logger = getLogger('API.GA');
}

/**
* Prepares the Google Analytics instance.
* @return {!google.analytics}
* @private
*/
async getApiClient_() {
if (this.analytics) return this.analytics;
await this.authClient.prepareCredentials();
this.logger.debug(`Initialized ${this.constructor.name} instance.`);
this.analytics = google.analytics({
version: API_VERSION,
auth: this.authClient.getDefaultAuth(),
});
return this.analytics;
/** @override */
getScope() {
return API_SCOPES;
}

/** @override */
getVersion() {
return API_VERSION;
}

/**
Expand All @@ -102,7 +98,7 @@ class Analytics {
},
config);

const analytics = await this.getApiClient_();
const analytics = await this.getApiClient();
const response = await analytics.management.uploads.uploadData(
uploadConfig);
this.logger.debug('Configuration: ', config);
Expand Down Expand Up @@ -151,7 +147,7 @@ class Analytics {
* @return {!Promise<!Schema$Upload>} Updated data import Job status.
*/
async checkJobStatus(jobConfig) {
const analytics = await this.getApiClient_();
const analytics = await this.getApiClient();
const { data: job } = await analytics.management.uploads.get(jobConfig);
if (job.status !== 'PENDING') return job;
this.logger.debug(
Expand All @@ -169,7 +165,7 @@ class Analytics {
* @return {!Promise<!Array<string>>}
*/
async listAccounts() {
const analytics = await this.getApiClient_();
const analytics = await this.getApiClient();
const response = await analytics.management.accounts.list();
return response.data.items.map(
(account) => `Account id: ${account.name}[${account.id}]`
Expand All @@ -182,7 +178,7 @@ class Analytics {
* @return {!Promise<!Array<Object>>}
*/
async listUploads(config) {
const analytics = await this.getApiClient_();
const analytics = await this.getApiClient();
const response = await analytics.management.uploads.list(config);
return response.data.items;
}
Expand All @@ -203,7 +199,7 @@ class Analytics {
const request = Object.assign({}, config, {
resource: {customDataImportUids},
});
const analytics = await this.getApiClient_();
const analytics = await this.getApiClient();
await analytics.management.uploads.deleteUploadData(request);
this.logger.debug('Delete uploads: ', customDataImportUids);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

const { Transform } = require('stream');
const {
extractObject,
changeObjectNamingFromLowerCamelToSnake,
getFilterAndStringifyFn,
getLogger,
} = require('../../components/utils.js');

Expand All @@ -37,22 +36,6 @@ const START_TAG = '"results":';
const FIELD_MASK_TAG = '"fieldMask"';
const END_TAG = '"requestId"';

/**
* Generates a function that can convert a given JSON object to a JSON string
* with only specified fields(fieldMask), in specified naming convention.
* @param {string} fieldMask The 'fieldMask' string from response.
* @param {boolean=} snakeCase Whether or not output JSON in snake naming.
*/
function generateProcessFn(fieldMask, snakeCase = false) {
const extractor = extractObject(fieldMask.split(','));
return (originalObject) => {
const extracted = extractor(originalObject);
const generatedObject = snakeCase
? changeObjectNamingFromLowerCamelToSnake(extracted) : extracted;
return JSON.stringify(generatedObject);
};
};

/**
* A stream.Transform that can extract properties and convert naming of the
* reponse of Google/Search Ads report from REST interface.
Expand Down Expand Up @@ -85,7 +68,7 @@ class RestSearchStreamTransform extends Transform {
.substring(maskIndex + FIELD_MASK_TAG.length, rawString.indexOf(END_TAG))
.split('"')[1];
this.logger.debug(`Got fieldMask: ${fieldMask}`);
this.processFn = generateProcessFn(fieldMask, this.snakeCase);
this.processFn = getFilterAndStringifyFn(fieldMask, this.snakeCase);
}
const resultsWithTailing = rawString.substring(startIndex, maskIndex);
const results = resultsWithTailing.substring(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview A base class for Google Api client library class.
*/
const { google } = require('googleapis');
const { getLogger, getObjectByPath } = require('../../components/utils.js');
const { AuthRestfulApi } = require('./auth_restful_api.js');

/**
* A Google Api client library class.
*/
class GoogleApiClient extends AuthRestfulApi {

/** @constructor */
constructor(env = process.env, options = {}) {
super(env, options);
this.logger = getLogger('API.default');
}

/**
* Returns the Api version of the Api in the current library.
* @return {string}
* @abstract
*/
getVersion() { }

/**
* Returns the Api instance.
* @return {!Promise<object>} The Api instance.
*/
async getApiClient() {
if (this.apiClient) return this.apiClient;
this.logger.info(`Initialized ${this.constructor.name} instance.`);
this.apiClient = google[this.googleApi]({
version: this.getVersion(),
auth: await this.getAuth(),
});
return this.apiClient;
}
}

module.exports = { GoogleApiClient };
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

'use strict';

const {google} = require('googleapis');
const {request} = require('gaxios');
const AuthClient = require('./auth_client.js');
const { request } = require('gaxios');
const { GoogleApiClient } = require('./base/google_api_client.js');
const {
getLogger,
getFilterFunction,
Expand Down Expand Up @@ -94,42 +93,27 @@ const MAX_IDENTIFIERS_PER_USER = 5;
* Google DfaReport API v3.0 stub.
* see https://developers.google.com/doubleclick-advertisers/service_accounts
*/
class DfaReporting {
class DfaReporting extends GoogleApiClient {

/**
* @constructor
* @param {!Object<string,string>=} env The environment object to hold env
* variables.
*/
constructor(env = process.env) {
this.authClient = new AuthClient(API_SCOPES, env);
super(env);
this.googleApi = 'dfareporting';
this.logger = getLogger('API.CM');
}

/**
* Prepares the Google DfaReport API instance.
* @return {!google.dfareporting}
* @private
*/
async getApiClient_() {
if (this.dfareporting) return this.dfareporting;
this.logger.debug(`Initialized ${this.constructor.name} instance.`);
this.dfareporting = google.dfareporting({
version: API_VERSION,
auth: await this.getAuth_(),
});
return this.dfareporting;
/** @override */
getScope() {
return API_SCOPES;
}

/**
* Gets the auth object.
* @return {!Promise<{!OAuth2Client|!JWT|!Compute}>}
*/
async getAuth_() {
if (this.auth) return this.auth;
await this.authClient.prepareCredentials();
this.auth = this.authClient.getDefaultAuth();
return this.auth;
/** @override */
getVersion() {
return API_VERSION;
}

/**
Expand All @@ -140,7 +124,7 @@ class DfaReporting {
* @return {!Promise<string>}
*/
async getProfileId(accountId) {
const dfareporting = await this.getApiClient_();
const dfareporting = await this.getApiClient();
const { data: { items } } = await dfareporting.userProfiles.list();
const profiles = items.filter(
(profile) => profile.accountId === accountId
Expand Down Expand Up @@ -237,7 +221,7 @@ class DfaReporting {
numberOfLines: lines.length,
};
try {
const dfareporting = await this.getApiClient_();
const dfareporting = await this.getApiClient();
const response = await dfareporting.conversions[operation]({
profileId: config.profileId,
requestBody: requestBody,
Expand Down Expand Up @@ -304,7 +288,7 @@ class DfaReporting {
* @return {!Promise<!Array<string>>}
*/
async listUserProfiles() {
const dfareporting = await this.getApiClient_();
const dfareporting = await this.getApiClient();
const { data: { items } } = await dfareporting.userProfiles.list();
return items.map(({profileId, userName, accountId, accountName}) => {
return `Profile: ${profileId}[${userName}] `
Expand Down Expand Up @@ -349,7 +333,7 @@ class DfaReporting {
*/
async runReport(config) {
const profileId = await this.getProfileForOperation_(config);
const dfareporting = await this.getApiClient_();
const dfareporting = await this.getApiClient();
const { startDate, endDate } = config;
if (startDate && endDate) {
const { data: report } = await dfareporting.reports.get({
Expand Down Expand Up @@ -392,7 +376,7 @@ class DfaReporting {
*/
async getReportFileUrl(config) {
const profileId = await this.getProfileForOperation_(config);
const dfareporting = await this.getApiClient_();
const dfareporting = await this.getApiClient();
const response = await dfareporting.reports.files.get({
profileId,
reportId: config.reportId,
Expand All @@ -410,7 +394,7 @@ class DfaReporting {
* @return {!Promise<stream>}
*/
async getReportFileStream(url) {
const auth = await this.getAuth_();
const auth = await this.getAuth();
const headers = await auth.getRequestHeaders();
const response = await request({
method: 'GET',
Expand Down
Loading

0 comments on commit ea26e84

Please sign in to comment.