From bab86e0f123de5362e0076d6c8aa96cfa0c7e2eb Mon Sep 17 00:00:00 2001 From: noerog <32459203+noerog@users.noreply.github.com> Date: Thu, 25 Jan 2018 13:41:43 -0500 Subject: [PATCH] Add create push subscription sample. [(#1331)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1331) --- samples/snippets/subscriber.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/samples/snippets/subscriber.py b/samples/snippets/subscriber.py index 1fd480e59..f8558e39f 100644 --- a/samples/snippets/subscriber.py +++ b/samples/snippets/subscriber.py @@ -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() @@ -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') @@ -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)