Skip to content

Commit

Permalink
samples: add sample for unwrapped push subscriptions (#1865)
Browse files Browse the repository at this point in the history
* samples: add sample for push subscription with no wrapper

* samples: update samples/createPushSubscriptionNoWrapper.js

Co-authored-by: Alex Hong <[email protected]>

* samples: update samples/createPushSubscriptionNoWrapper.js

Co-authored-by: Alex Hong <[email protected]>

* samples: update copyright createPushSubscriptionNoWrapper.js

* samples: update copyright createPushSubscriptionNoWrapper.ts

* samples: update region tag in ts as well

* chore: remove commented code in subscriptions.test.ts

* docs: update comments to be clearer

* samples: update push subscription samples to take a URL parameter for extra clarity

* 🦉 Updates from OwlBot post-processor

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

* tests: use a real URL for the actual testing

---------

Co-authored-by: Alex Hong <[email protected]>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 17, 2024
1 parent f953b64 commit 5bb3c50
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-pubsub/tree
| Create BigQuery Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createBigQuerySubscription.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/createBigQuerySubscription.js,samples/README.md) |
| Create a Proto based Schema | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createProtoSchema.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/createProtoSchema.js,samples/README.md) |
| Create Push Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createPushSubscription.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/createPushSubscription.js,samples/README.md) |
| Create Push Subscription With No Wrapper | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createPushSubscriptionNoWrapper.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/createPushSubscriptionNoWrapper.js,samples/README.md) |
| Create Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscription.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/createSubscription.js,samples/README.md) |
| Create Subscription With Dead Letter Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithDeadLetterPolicy.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/createSubscriptionWithDeadLetterPolicy.js,samples/README.md) |
| Create an exactly-once delivery subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createSubscriptionWithExactlyOnceDelivery.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/createSubscriptionWithExactlyOnceDelivery.js,samples/README.md) |
Expand Down
20 changes: 20 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ guides.
* [Create BigQuery Subscription](#create-bigquery-subscription)
* [Create a Proto based Schema](#create-a-proto-based-schema)
* [Create Push Subscription](#create-push-subscription)
* [Create Push Subscription With No Wrapper](#create-push-subscription-with-no-wrapper)
* [Create Subscription](#create-subscription)
* [Create Subscription With Dead Letter Policy](#create-subscription-with-dead-letter-policy)
* [Create an exactly-once delivery subscription](#create-an-exactly-once-delivery-subscription)
Expand Down Expand Up @@ -165,6 +166,25 @@ __Usage:__



### Create Push Subscription With No Wrapper

Creates a new push subscription, but disables wrapping for payloads.

View the [source code](https://github.com/googleapis/nodejs-pubsub/blob/main/samples/createPushSubscriptionNoWrapper.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/createPushSubscriptionNoWrapper.js,samples/README.md)

__Usage:__


`node createPushSubscriptionNoWrapper.js <topic-name-or-id> <subscription-name-or-id>`


-----




### Create Subscription

Creates a new subscription.
Expand Down
16 changes: 13 additions & 3 deletions samples/createPushSubscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const pushEndpoint = 'YOUR_ENDPOINT_URL';
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';

Expand All @@ -42,12 +43,16 @@ const {PubSub} = require('@google-cloud/pubsub');
// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function createPushSubscription(topicNameOrId, subscriptionNameOrId) {
async function createPushSubscription(
pushEndpoint,
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`,
pushEndpoint,
},
};

Expand All @@ -59,10 +64,15 @@ async function createPushSubscription(topicNameOrId, subscriptionNameOrId) {
// [END pubsub_create_push_subscription]

function main(
pushEndpoint = 'YOUR_ENDPOINT_URL',
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID'
) {
createPushSubscription(topicNameOrId, subscriptionNameOrId).catch(err => {
createPushSubscription(
pushEndpoint,
topicNameOrId,
subscriptionNameOrId
).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
Expand Down
87 changes: 87 additions & 0 deletions samples/createPushSubscriptionNoWrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2024 Google LLC
//
// 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.

// 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.
*
* For more information, see the README.md under /pubsub and the documentation
* at https://cloud.google.com/pubsub/docs.
*/

// sample-metadata:
// title: Create Push Subscription With No Wrapper
// description: Creates a new push subscription, but disables wrapping for payloads.
// usage: node createPushSubscriptionNoWrapper.js <topic-name-or-id> <subscription-name-or-id>

// [START pubsub_create_unwrapped_push_subscription]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const pushEndpoint = 'YOUR_ENDPOINT_URL';
// 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 createPushSubscriptionNoWrapper(
pushEndpoint,
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,
// When true, writes the Pub/Sub message metadata to
// `x-goog-pubsub-<KEY>:<VAL>` headers of the HTTP request. Writes the
// Pub/Sub message attributes to `<KEY>:<VAL>` headers of the HTTP request.
noWrapper: {
writeMetadata: true,
},
},
};

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

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

main(...process.argv.slice(2));
21 changes: 20 additions & 1 deletion samples/system-test/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,33 @@ describe('subscriptions', () => {
const testId = 'push_sub';
const topic = await createTopic(testId);
const subName = reserveSub(testId);
const url = `https://${pubsub.projectId}.appspot.com/push`;
const output = execSync(
`${commandFor('createPushSubscription')} ${topic.name} ${subName}`
`${commandFor('createPushSubscription')} ${url} ${topic.name} ${subName}`
);
assert.include(output, `Subscription ${subName} created.`);
const [subscriptions] = await pubsub.topic(topic.name).getSubscriptions();
assert(subscriptions.some(s => s.name === fullSubName(subName)));
});

it('should create a push subscription w/no wrapper', async () => {
const testId = 'push_sub_nw';
const topic = await createTopic(testId);
const subName = reserveSub(testId);
const url = `https://${pubsub.projectId}.appspot.com/push`;
const output = execSync(
`${commandFor('createPushSubscriptionNoWrapper')} ${url} ${
topic.name
} ${subName}`
);
assert.include(output, `Subscription ${subName} created.`);
const sub = await pubsub.subscription(subName);
const [subInfo] = await sub.get();
assert(subInfo.name === fullSubName(subName));
const [meta] = await subInfo.getMetadata();
assert(meta.pushConfig!.noWrapper!.writeMetadata === true);
});

it('should create a BigQuery subscription', async () => {
const testId = 'bigquery_sub';
const topic = await createTopic(testId);
Expand Down
11 changes: 9 additions & 2 deletions samples/typescript/createPushSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const pushEndpoint = 'YOUR_ENDPOINT_URL';
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID';
// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';

Expand All @@ -39,14 +40,15 @@ import {PubSub, CreateSubscriptionOptions} from '@google-cloud/pubsub';
const pubSubClient = new PubSub();

async function createPushSubscription(
pushEndpoint: string,
topicNameOrId: string,
subscriptionNameOrId: string
) {
const options: CreateSubscriptionOptions = {
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`,
pushEndpoint,
},
};

Expand All @@ -58,10 +60,15 @@ async function createPushSubscription(
// [END pubsub_create_push_subscription]

function main(
pushEndpoint = 'YOUR_ENDPOINT_URL',
topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID',
subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID'
) {
createPushSubscription(topicNameOrId, subscriptionNameOrId).catch(err => {
createPushSubscription(
pushEndpoint,
topicNameOrId,
subscriptionNameOrId
).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
Expand Down
83 changes: 83 additions & 0 deletions samples/typescript/createPushSubscriptionNoWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2024 Google LLC
//
// 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.

/**
* This application demonstrates how to perform basic operations on
* subscriptions with the Google Cloud Pub/Sub API.
*
* For more information, see the README.md under /pubsub and the documentation
* at https://cloud.google.com/pubsub/docs.
*/

// sample-metadata:
// title: Create Push Subscription With No Wrapper
// description: Creates a new push subscription, but disables wrapping for payloads.
// usage: node createPushSubscriptionNoWrapper.js <topic-name-or-id> <subscription-name-or-id>

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

// Imports the Google Cloud client library
import {PubSub, CreateSubscriptionOptions} from '@google-cloud/pubsub';

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

async function createPushSubscriptionNoWrapper(
pushEndpoint: string,
topicNameOrId: string,
subscriptionNameOrId: string
) {
const options: CreateSubscriptionOptions = {
pushConfig: {
// Set to an HTTPS endpoint of your choice. If necessary, register
// (authorize) the domain on which the server is hosted.
pushEndpoint,
// When true, writes the Pub/Sub message metadata to
// `x-goog-pubsub-<KEY>:<VAL>` headers of the HTTP request. Writes the
// Pub/Sub message attributes to `<KEY>:<VAL>` headers of the HTTP request.
noWrapper: {
writeMetadata: true,
},
},
};

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

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

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

0 comments on commit 5bb3c50

Please sign in to comment.