Skip to content

Commit

Permalink
Add create push subscription sample. [(#1331)](GoogleCloudPlatform/py…
Browse files Browse the repository at this point in the history
  • Loading branch information
noerog authored and Jon Wayne Parrott committed Jan 25, 2018
1 parent 4a63dad commit bab86e0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions samples/snippets/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

0 comments on commit bab86e0

Please sign in to comment.