Skip to content

Commit

Permalink
samples: add more TS samples (#1666)
Browse files Browse the repository at this point in the history
* samples: work in progress on sample conversion to TS

* chore: Enable requesting numeric enums in "transport=rest" responses for services supporting this (Java, Go, Python, PHP, TypeScript, C#, and Ruby), even if they do not yet turn on REST transport (#1663)

* chore: Enable requesting numeric enums in "transport=rest" responses for services supporting this (Java, Go, Python, PHP, TypeScript, C#, and Ruby), even if they do not yet turn on REST transport

chore: disallow "transport=rest" for services where numeric enums are not confirmed to be supported (except in PHP and Java)
PiperOrigin-RevId: 493113566

Source-Link: googleapis/googleapis@758f0d1

Source-Link: googleapis/googleapis-gen@78bd8f0
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNzhiZDhmMDVlMTI3NjM2M2ViMTRlYWU3MGU5MWZlNGJjMjA3MDNhYiJ9

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Megan Potter <57276408+feywind@users.noreply.github.com>

* build: use typeless bot 1.2.0

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* build: also run fix before README generation

* samples: add missing validateSchema.ts, and fix copyright dates in setTopicPolicy

* build: use hermetic version of gts for owlbot

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* samples: update copyright

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* samples: manually update validateSchema since it's not technically sample snippet

* chore: update typeless bot version

* chore: linting updates

* samples: update module export method for resumePublish.ts

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 17, 2023
1 parent 81a8611 commit faa885f
Show file tree
Hide file tree
Showing 88 changed files with 4,161 additions and 1,187 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async function quickstart(
});

// Send a message to the topic
topic.publish(Buffer.from('Test message!'));
topic.publishMessage({data: Buffer.from('Test message!')});
}

```
Expand Down Expand Up @@ -174,6 +174,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-pubsub/tree
| Test Subscription Permissions | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/testSubscriptionPermissions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/testSubscriptionPermissions.js,samples/README.md) |
| Test Topic Permissions | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/testTopicPermissions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/testTopicPermissions.js,samples/README.md) |
| Update Dead Letter Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/updateDeadLetterPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/updateDeadLetterPolicy.js,samples/README.md) |
| Validate a schema definition | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/validateSchema.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/validateSchema.js,samples/README.md) |



Expand Down
20 changes: 16 additions & 4 deletions owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,31 @@
# This code snippet can be used to manually update the typeless bot
# to a different version than OwlBot has baked in, but this shouldn't
# be needed once it's settled down.
"""
import os
from synthtool import shell
from synthtool.log import logger
old_path = os.getcwd()
os.chdir("/synthtool")
logger.debug("Update typeless sample bot [1.1.0]")
shell.run(["npm", "i", "@google-cloud/typeless-sample-bot@1.1.0"])
logger.debug("Update typeless sample bot [1.3.0]")
shell.run(["npm", "i", "@google-cloud/typeless-sample-bot@1.3.0"])
os.chdir(old_path)
"""

node.typeless_samples_hermetic()

# We need to run this before the main owlbot processing, to make
# sure quickstart.js gets gts fixed before the README is generated.
# This needs to be worked out more properly, this is temporary.
logger.debug("Copy eslint config")
shell.run(
["cp", "-r", f"/synthtool/node_modules", "."],
check=True,
)
logger.debug("Running fix...")
shell.run(
[f"/synthtool/node_modules/.bin/gts", "fix"],
check=False,
)

node.owlbot_main(templates_excludes=[
'src/index.ts',
'.github/PULL_REQUEST_TEMPLATE.md',
Expand Down
20 changes: 20 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ guides.
* [Test Subscription Permissions](#test-subscription-permissions)
* [Test Topic Permissions](#test-topic-permissions)
* [Update Dead Letter Policy](#update-dead-letter-policy)
* [Validate a schema definition](#validate-a-schema-definition)

## Before you begin

Expand Down Expand Up @@ -1051,6 +1052,25 @@ __Usage:__
`node updateDeadLetterPolicy.js <topic-name-or-id> <subscription-name-or-id>`


-----




### Validate a schema definition

Validates an Avro-based schema definition before creation (or other use).

View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/validateSchema.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/validateSchema.js,samples/README.md)

__Usage:__


`node validateSchema.js <schema-text>`





Expand Down
4 changes: 4 additions & 0 deletions samples/createAvroSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// This is a generated sample, using the typeless sample bot. Please
// look for the source TypeScript sample (.ts) for modifications.
'use strict';

/**
* This application demonstrates how to perform basic operations on
* schemas with the Google Cloud Pub/Sub API.
Expand Down
80 changes: 47 additions & 33 deletions samples/createBigQuerySubscription.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2022 Google LLC
// Copyright 2019-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// This is a generated sample, using the typeless sample bot. Please
// look for the source TypeScript sample (.ts) for modifications.
'use strict';

/**
* This application demonstrates how to perform basic operations on
* subscriptions with the Google Cloud Pub/Sub API.
Expand All @@ -20,48 +24,58 @@
* at https://cloud.google.com/pubsub/docs.
*/

'use strict';

// sample-metadata:
// title: Create BigQuery Subscription
// description: Creates a new BigQuery subscription.
// usage: node createBigQuerySubscription.js <topic-name-or-id> <subscription-name-or-id> <bigquery-table-id>

function main(
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID',
bigqueryTableId = 'YOUR_TABLE_ID'
) {
// [START pubsub_create_bigquery_subscription]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';
// const bigqueryTableId = 'YOUR_TABLE_ID';
// [START pubsub_create_bigquery_subscription]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';
// const bigqueryTableId = 'YOUR_TABLE_ID';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');
// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();
// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function createBigQuerySubscription() {
const options = {
bigqueryConfig: {
table: bigqueryTableId,
writeMetadata: true,
},
};
async function createBigQuerySubscription(
topicNameOrId,
subscriptionNameOrId,
bigqueryTableId
) {
const options = {
bigqueryConfig: {
table: bigqueryTableId,
writeMetadata: true,
},
};

await pubSubClient
.topic(topicNameOrId)
.createSubscription(subscriptionNameOrId, options);
console.log(`Subscription ${subscriptionNameOrId} created.`);
}
await pubSubClient
.topic(topicNameOrId)
.createSubscription(subscriptionNameOrId, options);

createBigQuerySubscription().catch(console.error);
// [END pubsub_create_bigquery_subscription]
console.log(`Subscription ${subscriptionNameOrId} created.`);
}
// [END pubsub_create_bigquery_subscription]

function main(
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID',
bigqueryTableId = 'YOUR_TABLE_ID'
) {
createBigQuerySubscription(
topicNameOrId,
subscriptionNameOrId,
bigqueryTableId
).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
4 changes: 4 additions & 0 deletions samples/createProtoSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// This is a generated sample, using the typeless sample bot. Please
// look for the source TypeScript sample (.ts) for modifications.
'use strict';

/**
* This application demonstrates how to perform basic operations on
* schemas with the Google Cloud Pub/Sub API.
Expand Down
69 changes: 37 additions & 32 deletions samples/createPushSubscription.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 Google LLC
// Copyright 2019-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// This is a generated sample, using the typeless sample bot. Please
// look for the source TypeScript sample (.ts) for modifications.
'use strict';

/**
* This application demonstrates how to perform basic operations on
* subscriptions with the Google Cloud Pub/Sub API.
Expand All @@ -20,47 +24,48 @@
* at https://cloud.google.com/pubsub/docs.
*/

'use strict';

// sample-metadata:
// title: Create Push Subscription
// description: Creates a new push subscription.
// usage: node createPushSubscription.js <topic-name-or-id> <subscription-name-or-id>

function main(
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID'
) {
// [START pubsub_create_push_subscription]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';
// [START pubsub_create_push_subscription]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');
// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();
// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function createPushSubscription() {
const options = {
pushConfig: {
// Set to an HTTPS endpoint of your choice. If necessary, register
// (authorize) the domain on which the server is hosted.
pushEndpoint: `https://${pubSubClient.projectId}.appspot.com/push`,
},
};
async function createPushSubscription(topicNameOrId, subscriptionNameOrId) {
const options = {
pushConfig: {
// Set to an HTTPS endpoint of your choice. If necessary, register
// (authorize) the domain on which the server is hosted.
pushEndpoint: `https://${pubSubClient.projectId}.appspot.com/push`,
},
};

await pubSubClient
.topic(topicNameOrId)
.createSubscription(subscriptionNameOrId, options);
console.log(`Subscription ${subscriptionNameOrId} created.`);
}
await pubSubClient
.topic(topicNameOrId)
.createSubscription(subscriptionNameOrId, options);
console.log(`Subscription ${subscriptionNameOrId} created.`);
}
// [END pubsub_create_push_subscription]

createPushSubscription().catch(console.error);
// [END pubsub_create_push_subscription]
function main(
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID'
) {
createPushSubscription(topicNameOrId, subscriptionNameOrId).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
57 changes: 31 additions & 26 deletions samples/createSubscription.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 Google LLC
// Copyright 2019-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// This is a generated sample, using the typeless sample bot. Please
// look for the source TypeScript sample (.ts) for modifications.
'use strict';

/**
* This sample demonstrates how to create subscriptions with the
* Google Cloud Pub/Sub API.
Expand All @@ -20,40 +24,41 @@
* at https://cloud.google.com/pubsub/docs.
*/

'use strict';

// sample-metadata:
// title: Create Subscription
// description: Creates a new subscription.
// usage: node createSubscription.js <topic-name-or-id> <subscription-name-or-id>

// [START pubsub_create_pull_subscription]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function createSubscription(topicNameOrId, subscriptionNameOrId) {
// Creates a new subscription
await pubSubClient
.topic(topicNameOrId)
.createSubscription(subscriptionNameOrId);
console.log(`Subscription ${subscriptionNameOrId} created.`);
}
// [END pubsub_create_pull_subscription]

function main(
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID'
) {
// [START pubsub_create_pull_subscription]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';

// Imports the Google Cloud client library
const {PubSub} = require('@google-cloud/pubsub');

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function createSubscription() {
// Creates a new subscription
await pubSubClient
.topic(topicNameOrId)
.createSubscription(subscriptionNameOrId);
console.log(`Subscription ${subscriptionNameOrId} created.`);
}

createSubscription().catch(console.error);
// [END pubsub_create_pull_subscription]
createSubscription(topicNameOrId, subscriptionNameOrId).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
}

main(...process.argv.slice(2));
Loading

0 comments on commit faa885f

Please sign in to comment.