Skip to content

Commit

Permalink
chore: update example
Browse files Browse the repository at this point in the history
  • Loading branch information
htdangkhoa committed Oct 9, 2024
1 parent ae621c4 commit 06b5cf1
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 88 deletions.
13 changes: 4 additions & 9 deletions example/ad-group-ad.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { google } from 'googleapis';

import { GoogleAds, ads, MessageType } from '../src';

const {
common: { AdTextAsset },
enums: { AdGroupAdStatusEnum_AdGroupAdStatus },
} = ads.googleads.v17;
import { GoogleAds, common, enums } from '../src';

const authClient = new google.auth.JWT({
keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS,
Expand All @@ -17,8 +12,8 @@ export async function createAdGroupAdApp(
customer_id: string,
login_customer_id: string,
ad_group_resource: string,
headlines: MessageType<typeof AdTextAsset>[],
descriptions: MessageType<typeof AdTextAsset>[],
headlines: common.AdTextAsset[],
descriptions: common.AdTextAsset[],
) {
const service = new GoogleAds(
{
Expand Down Expand Up @@ -53,7 +48,7 @@ export async function createAdGroupAdApp(
],
},
},
status: AdGroupAdStatusEnum_AdGroupAdStatus.PAUSED,
status: enums.AdGroupAdStatusEnum_AdGroupAdStatus.PAUSED,
},
},
},
Expand Down
10 changes: 3 additions & 7 deletions example/ad-group.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { google } from 'googleapis';

import { GoogleAds, ads } from '../src';

const {
enums: { AdGroupStatusEnum_AdGroupStatus, AdGroupTypeEnum_AdGroupType },
} = ads.googleads.v17;
import { GoogleAds, enums } from '../src';

const authClient = new google.auth.JWT({
keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS,
Expand Down Expand Up @@ -35,9 +31,9 @@ export async function createAdGroup(
ad_group_operation: {
create: {
name,
status: AdGroupStatusEnum_AdGroupStatus.PAUSED,
status: enums.AdGroupStatusEnum_AdGroupStatus.PAUSED,
campaign: campaign_resource,
type: AdGroupTypeEnum_AdGroupType.SEARCH_STANDARD,
type: enums.AdGroupTypeEnum_AdGroupType.SEARCH_STANDARD,
},
},
},
Expand Down
21 changes: 7 additions & 14 deletions example/asset.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { google } from 'googleapis';

import { GoogleAds, ads, MessageType } from '../src';

const {
resources: { Asset },
services: { MutateOperation },
} = ads.googleads.v17;
import { GoogleAds, resources, services } from '../src';

const authClient = new google.auth.JWT({
keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS,
Expand All @@ -16,7 +11,7 @@ const authClient = new google.auth.JWT({
export async function uploadAsset(
customer_id: string,
login_customer_id: string,
assets: MessageType<typeof Asset>[],
assets: resources.Asset[],
) {
const service = new GoogleAds(
{
Expand All @@ -29,13 +24,11 @@ export async function uploadAsset(
},
);

const mutate_operations: MessageType<typeof MutateOperation>[] = assets.map(
(asset) => ({
asset_operation: {
create: asset,
},
}),
);
const mutate_operations: services.MutateOperation[] = assets.map((asset) => ({
asset_operation: {
create: asset,
},
}));

try {
const { mutate_operation_responses, partial_failure_error } =
Expand Down
29 changes: 15 additions & 14 deletions example/campaign.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { google } from 'googleapis';

import { GoogleAds } from '../src';
import { GoogleAds, QueryBuilder } from '../src';

const authClient = new google.auth.JWT({
keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS,
Expand All @@ -20,17 +20,18 @@ export function getCampaigns(customer_id: string, login_customer_id: string) {
},
);

return service.search({
query: `
SELECT
campaign.id,
campaign.name,
campaign.status,
campaign.start_date,
campaign.end_date,
campaign.serving_status,
campaign.resource_name
FROM campaign
`,
});
const query = new QueryBuilder()
.select(
'campaign.id',
'campaign.name',
'campaign.status',
'campaign.start_date',
'campaign.end_date',
'campaign.serving_status',
'campaign.resource_name',
)
.from('campaign')
.build();

return service.search({ query });
}
22 changes: 11 additions & 11 deletions example/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ async function main() {
},
);

const { results: customerClients } = await googleAdsService.search({
query: new QueryBuilder()
.select(
'customer_client.resource_name',
'customer_client.client_customer',
'customer_client.level',
'customer_client.hidden',
)
.from('customer_client')
.build(),
});
const query = new QueryBuilder()
.select(
'customer_client.resource_name',
'customer_client.client_customer',
'customer_client.level',
'customer_client.hidden',
)
.from('customer_client')
.build();

const { results: customerClients } = await googleAdsService.search({ query });

const customer_client = customerClients!
.find(
Expand Down
71 changes: 60 additions & 11 deletions example/youtube.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { Readable } from 'stream';
import * as fs from 'fs';
import { PassThrough, Readable } from 'stream';
import { google } from 'googleapis';
import { GoogleAds, enums } from '../src';

const authClient = new google.auth.JWT({
keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS,
subject: '<YOUR_EMAIL>',
scopes: ['https://www.googleapis.com/auth/youtube'],
});

export function upload(stream: Readable) {
const yt = google.youtube({ version: 'v3', auth: authClient });

// const videoPath = path.join(__dirname, 'video.mp4');

// const buffer = fs.readFileSync(videoPath);

// // convert buffer to stream
// const videoStream = new PassThrough();
// videoStream.end(buffer);
const yt = google.youtube({ version: 'v3', auth: authClient });

function upload(stream: Readable) {
return yt.videos.insert({
part: ['snippet', 'contentDetails', 'status'],
requestBody: {
Expand All @@ -35,3 +29,58 @@ export function upload(stream: Readable) {
},
});
}

async function main(customer_id: string, login_customer_id: string) {
/* video */
const videoPath = '<example_video_file>';

const buffer = fs.readFileSync(videoPath);

const videoStream = new PassThrough();
videoStream.end(buffer);

const resp = await upload(videoStream);

/* image */
const imagePath = '<example_image_file>';

const imageBuffer = fs.readFileSync(imagePath);

const imageBase64 = Buffer.from(imageBuffer).toString('base64');

const service = new GoogleAds({
auth: authClient,
developer_token: process.env.DEVELOPER_TOKEN!,
});

return service
.setLoginCustomerId(login_customer_id)
.setCustomerId(customer_id)
.mutate({
mutate_operations: [
{
asset_operation: {
create: {
name: '<YOUR_ASSET_NAME>',
type: enums.AssetTypeEnum_AssetType.YOUTUBE_VIDEO,
youtube_video_asset: {
youtube_video_id: resp.data.id,
youtube_video_title: '<YOUR_VIDEO_TITLE>',
},
},
},
},
{
asset_operation: {
create: {
name: '<YOUR_ASSET_NAME>',
type: enums.AssetTypeEnum_AssetType.IMAGE,
image_asset: {
data: Buffer.from(imageBase64),
},
},
},
},
],
});
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@
"generate": "./scripts/generate.sh"
},
"dependencies": {
"@bufbuild/protobuf": "^2.1.0",
"@grpc/grpc-js": "^1.12.0",
"@bufbuild/protobuf": "^2.2.0",
"@grpc/grpc-js": "^1.12.1",
"deepmerge": "^4.3.1",
"log4js": "^6.9.1"
},
"devDependencies": {
"@types/node": "^22.7.4",
"@types/node": "^22.7.5",
"@vitest/coverage-v8": "^2.1.2",
"coveralls-next": "^4.2.0",
"fast-glob": "^3.3.2",
"googleapis": "^144.0.0",
"ts-node": "^10.9.2",
"ts-proto": "^2.2.2",
"typescript": "5.6.2",
"ts-proto": "^2.2.3",
"typescript": "5.6.3",
"vitest": "^2.1.2"
},
"publishConfig": {
Expand Down
39 changes: 22 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==

"@bufbuild/protobuf@^2.0.0", "@bufbuild/protobuf@^2.1.0":
"@bufbuild/protobuf@^2.0.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@bufbuild/protobuf/-/protobuf-2.1.0.tgz#6925f30c25789b4f74d7c505e731c96f79fb48a7"
integrity sha512-+2Mx67Y3skJ4NCD/qNSdBJNWtu6x6Qr53jeNg+QcwiL6mt0wK+3jwHH2x1p7xaYH6Ve2JKOVn0OxU35WsmqI9A==

"@bufbuild/protobuf@^2.2.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@bufbuild/protobuf/-/protobuf-2.2.0.tgz#3c63dcbb6ef4f6a9f587e3d412c08f9a8f5bcd00"
integrity sha512-+imAQkHf7U/Rwvu0wk1XWgsP3WnpCWmK7B48f0XqSNzgk64+grljTKC7pnO/xBiEMUziF7vKRfbBnOQhg126qQ==

"@cspotcode/source-map-support@^0.8.0":
version "0.8.1"
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz#00629c35a688e05a88b1cda684fb9d5e73f000a1"
Expand Down Expand Up @@ -168,10 +173,10 @@
resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.21.5.tgz#acad351d582d157bb145535db2a6ff53dd514b5c"
integrity sha512-tQd/1efJuzPC6rCFwEvLtci/xNFcTZknmXs98FYDfGE4wP9ClFV98nyKrzJKVPMhdDnjzLhdUyMX4PsQAPjwIw==

"@grpc/grpc-js@^1.12.0":
version "1.12.0"
resolved "https://registry.yarnpkg.com/@grpc/grpc-js/-/grpc-js-1.12.0.tgz#3f2294c7b685d2df4dc5b0b9b74b595ff3affed0"
integrity sha512-eWdP97A6xKtZXVP/ze9y8zYRB2t6ugQAuLXFuZXAsyqmyltaAjl4yPkmIfc0wuTFJMOUF1AdvIFQCL7fMtaX6g==
"@grpc/grpc-js@^1.12.1":
version "1.12.1"
resolved "https://registry.yarnpkg.com/@grpc/grpc-js/-/grpc-js-1.12.1.tgz#e34ac2b7f30cfb1814c0dcf47ba5c69ce7a65858"
integrity sha512-VYrLinJNeW3idIZHTy7BsVL7bsPF9+l89WDYTDV8o1QYJzkf3BSoPXdfaz4c8DyeY12LGSaS8m8BgbT8W66h6Q==
dependencies:
"@grpc/proto-loader" "^0.7.13"
"@js-sdsl/ordered-map" "^4.4.2"
Expand Down Expand Up @@ -444,10 +449,10 @@
dependencies:
undici-types "~5.26.4"

"@types/node@^22.7.4":
version "22.7.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.7.4.tgz#e35d6f48dca3255ce44256ddc05dee1c23353fcc"
integrity sha512-y+NPi1rFzDs1NdQHHToqeiX2TIS79SWEAw9GYhkkx8bD0ChpfqC+n2j5OXOCpzfojBEBt6DnEnnG9MY0zk1XLg==
"@types/node@^22.7.5":
version "22.7.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.7.5.tgz#cfde981727a7ab3611a481510b473ae54442b92b"
integrity sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==
dependencies:
undici-types "~6.19.2"

Expand Down Expand Up @@ -1649,20 +1654,20 @@ [email protected]:
dependencies:
"@bufbuild/protobuf" "^2.0.0"

ts-proto@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/ts-proto/-/ts-proto-2.2.2.tgz#1f5cf4312c580937078026b590e1875b7f9f49fa"
integrity sha512-wHA6y1W6jyDM/G5GI1DrG84Aqz5UZdKXigSVmTYkauH8fjahag4D16nNvPeXpALJZML0FdgC/F9b4O0NFNZ62Q==
ts-proto@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/ts-proto/-/ts-proto-2.2.3.tgz#693714e83e17141af2dd749b07a929a432930010"
integrity sha512-sI9qyTcMg7syJjk0CvkRVxZyAuCbpweO2Kq5Dz/JXaRwpaB9ujMHdFF06raXj5nLxDknzV5cUIcdCuUThPXuYg==
dependencies:
"@bufbuild/protobuf" "^2.0.0"
case-anything "^2.1.13"
ts-poet "^6.7.0"
ts-proto-descriptors "2.0.0"

[email protected].2:
version "5.6.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.2.tgz#d1de67b6bef77c41823f822df8f0b3bcff60a5a0"
integrity sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==
[email protected].3:
version "5.6.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.3.tgz#5f3449e31c9d94febb17de03cc081dd56d81db5b"
integrity sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==

undici-types@~5.26.4:
version "5.26.5"
Expand Down

0 comments on commit 06b5cf1

Please sign in to comment.