Skip to content

Commit

Permalink
docs: add real time feed api sample code (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwxie-google authored and bcoe committed Aug 26, 2019
1 parent f0f6386 commit 6a21548
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 0 deletions.
60 changes: 60 additions & 0 deletions asset/snippets/createFeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2019, 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.
*/

'use strict';

// sample-metadata:
// title: Create Feed
// description: Create Feed.
// usage: node createFeed <FEED_ID> "//storage.googleapis.com/<BUCKET_NAME>", projects/<PROJECT_ID>/topics/<TOPIC_ID>

async function main(feedId, assetNames, topicName) {
// [START asset_quickstart_create_feed]
const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset/src/v1p2beta1');

const client = new AssetServiceClient();

async function createFeed() {
const projectId = await client.getProjectId();
// TODO(developer): Choose asset names, such as //storage.googleapis.com/[YOUR_BUCKET_NAME].
// const assetNames = ['ASSET_NAME1', 'ASSET_NAME2', ...];

const request = {
parent: `projects/${projectId}`,
feedId: feedId,
feed: {
assetNames: assetNames.split(','),
feedOutputConfig: {
pubsubDestination: {
topic: topicName,
},
},
},
};

// Handle the operation using the promise pattern.
const result = await client.createFeed(request);
// Do things with with the response.
console.log(util.inspect(result, {depth: null}));
// [END asset_quickstart_create_feed]
}
createFeed();
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
47 changes: 47 additions & 0 deletions asset/snippets/deleteFeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2019, 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.
*/

'use strict';

// sample-metadata:
// title: Delete Feed
// description: Delete Feed.
// usage: node deleteFeed "project/<PROJECT_NUMBER>/feeds/<FEED_ID>"

async function main(feedName) {
// [START asset_quickstart_delete_feed]
const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset/src/v1p2beta1');

const client = new AssetServiceClient();

async function deleteFeed() {
const request = {
name: feedName,
};

// Handle the operation using the promise pattern.
const result = await client.deleteFeed(request);
// Do things with with the response.
console.log(util.inspect(result, {depth: null}));
// [END asset_quickstart_delete_feed]
}
deleteFeed();
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
47 changes: 47 additions & 0 deletions asset/snippets/getFeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2019, 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.
*/

'use strict';

// sample-metadata:
// title: Get Feed
// description: Get Feed.
// usage: node getFeed "project/<PROJECT_NUMBER>/feeds/<FEED_ID>"

async function main(feedName) {
// [START asset_quickstart_get_feed]
const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset/src/v1p2beta1');

const client = new AssetServiceClient();

async function getFeed() {
const request = {
name: feedName,
};

// Handle the operation using the promise pattern.
const result = await client.getFeed(request);
// Do things with with the response.
console.log(util.inspect(result, {depth: null}));
// [END asset_quickstart_get_feed]
}
getFeed();
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
49 changes: 49 additions & 0 deletions asset/snippets/listFeeds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2019, 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.
*/

'use strict';

// sample-metadata:
// title: List Feeds
// description: List Feeds.
// usage: node listFeeds

async function main() {
// [START asset_quickstart_list_feeds]
const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset/src/v1p2beta1');

const client = new AssetServiceClient();

async function listFeeds() {
const projectId = await client.getProjectId();

const request = {
parent: `projects/${projectId}`,
};

// Handle the operation using the promise pattern.
const result = await client.listFeeds(request);
// Do things with with the response.
console.log(util.inspect(result, {depth: null}));
// [END asset_quickstart_list_feeds]
}
listFeeds();
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
57 changes: 57 additions & 0 deletions asset/snippets/updateFeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2019, 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.
*/

'use strict';

// sample-metadata:
// title: Update Feed
// description: Update Feed.
// usage: node updateFeed "project/<PROJECT_NUMBER>/feeds/<FEED_ID>" projects/<PROJECT_ID>/topics/<TOPIC_ID>

async function main(feedName, topicName) {
// [START asset_quickstart_update_feed]
const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset/src/v1p2beta1');

const client = new AssetServiceClient();

async function updateFeed() {
const request = {
feed: {
name: feedName,
feedOutputConfig: {
pubsubDestination: {
topic: topicName,
},
},
},
updateMask: {
paths: ['feed_output_config.pubsub_destination.topic'],
},
};

// Handle the operation using the promise pattern.
const result = await client.updateFeed(request);
// Do things with with the response.
console.log(util.inspect(result, {depth: null}));
// [END asset_quickstart_update_feed]
}
updateFeed();
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});

0 comments on commit 6a21548

Please sign in to comment.