Skip to content

Commit 1fa44ff

Browse files
committed
samples: Payload Unwrapping (NoWrapper)
1 parent 961bf3e commit 1fa44ff

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

samples/snippets/subscriber.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,44 @@ def create_push_subscription(
187187
# [END pubsub_create_push_subscription]
188188

189189

190+
def create_push_no_wrapper_subscription(
191+
project_id: str, topic_id: str, subscription_id: str, endpoint: str
192+
) -> None:
193+
"""Create a new push no wrapper subscription on the given topic."""
194+
# [START pubsub_create_push_no_wrapper_subscription]
195+
from google.cloud import pubsub_v1
196+
197+
# TODO(developer)
198+
# project_id = "your-project-id"
199+
# topic_id = "your-topic-id"
200+
# subscription_id = "your-subscription-id"
201+
# endpoint = "https://my-test-project.appspot.com/push"
202+
203+
publisher = pubsub_v1.PublisherClient()
204+
subscriber = pubsub_v1.SubscriberClient()
205+
topic_path = publisher.topic_path(project_id, topic_id)
206+
subscription_path = subscriber.subscription_path(project_id, subscription_id)
207+
208+
no_wrapper = pubsub_v1.types.NoWrapper(write_metadata=True)
209+
push_config = pubsub_v1.types.PushConfig(push_endpoint=endpoint, no_wrapper=no_wrapper)
210+
211+
# Wrap the subscriber in a 'with' block to automatically call close() to
212+
# close the underlying gRPC channel when done.
213+
with subscriber:
214+
subscription = subscriber.create_subscription(
215+
request={
216+
"name": subscription_path,
217+
"topic": topic_path,
218+
"push_config": push_config,
219+
}
220+
)
221+
222+
print(f"Push no wrapper subscription created: {subscription}.")
223+
print(f"Endpoint for subscription is: {endpoint}")
224+
print(f"No wrapper configuration for subscription is: {no_wrapper}")
225+
# [END pubsub_create_push_no_wrapper_subscription]
226+
227+
190228
def create_subscription_with_ordering(
191229
project_id: str, topic_id: str, subscription_id: str
192230
) -> None:
@@ -946,6 +984,13 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None:
946984
create_push_parser.add_argument("subscription_id")
947985
create_push_parser.add_argument("endpoint")
948986

987+
create_push_no_wrapper_parser = subparsers.add_parser(
988+
"create-push-no-wrapper", help=create_push_no_wrapper_subscription.__doc__
989+
)
990+
create_push_parser.add_argument("topic_id")
991+
create_push_parser.add_argument("subscription_id")
992+
create_push_parser.add_argument("endpoint")
993+
949994
create_subscription_with_ordering_parser = subparsers.add_parser(
950995
"create-with-ordering", help=create_subscription_with_ordering.__doc__
951996
)
@@ -1092,6 +1137,10 @@ def callback(message: pubsub_v1.subscriber.message.Message) -> None:
10921137
create_push_subscription(
10931138
args.project_id, args.topic_id, args.subscription_id, args.endpoint
10941139
)
1140+
elif args.command == "create-push-no-wrapper":
1141+
create_push_no_wrapper_subscription(
1142+
args.project_id, args.topic_id, args.subscription_id, args.endpoint
1143+
)
10951144
elif args.command == "create-with-ordering":
10961145
create_subscription_with_ordering(
10971146
args.project_id, args.topic_id, args.subscription_id

0 commit comments

Comments
 (0)