Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create push subscription sample. #1331

Merged
merged 1 commit into from
Jan 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pubsub/cloud-client/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ def create_subscription(project, topic_name, subscription_name):
print('Subscription created: {}'.format(subscription))


def create_push_subscription(project,
topic_name,
subscription_name,
endpoint):
"""Create a new push subscription on the given topic."""
subscriber = pubsub_v1.SubscriberClient()
topic_path = subscriber.topic_path(project, topic_name)
subscription_path = subscriber.subscription_path(
project, subscription_name)

push_config = pubsub_v1.types.PushConfig(
push_endpoint=endpoint)

subscription = subscriber.create_subscription(
subscription_path, topic_path, push_config)

print('Push subscription created: {}'.format(subscription))
print('Endpoint for subscription is: {}'.format(endpoint))


def delete_subscription(project, subscription_name):
"""Deletes an existing Pub/Sub topic."""
subscriber = pubsub_v1.SubscriberClient()
Expand Down Expand Up @@ -153,6 +173,12 @@ def callback(message):
create_parser.add_argument('topic_name')
create_parser.add_argument('subscription_name')

create_push_parser = subparsers.add_parser(
'create-push', help=create_push_subscription.__doc__)
create_push_parser.add_argument('topic_name')
create_push_parser.add_argument('subscription_name')
create_push_parser.add_argument('endpoint')

delete_parser = subparsers.add_parser(
'delete', help=delete_subscription.__doc__)
delete_parser.add_argument('subscription_name')
Expand All @@ -179,6 +205,12 @@ def callback(message):
elif args.command == 'create':
create_subscription(
args.project, args.topic_name, args.subscription_name)
elif args.command == 'create-push':
create_push_subscription(
args.project,
args.topic_name,
args.subscription_name,
args.endpoint)
elif args.command == 'delete':
delete_subscription(
args.project, args.subscription_name)
Expand Down