29
29
30
30
def list_subscriptions_in_topic (project , topic_name ):
31
31
"""Lists all subscriptions for a given topic."""
32
+ # [START pubsub_list_topic_subscriptions]
32
33
subscriber = pubsub_v1 .PublisherClient ()
33
34
topic_path = subscriber .topic_path (project , topic_name )
34
35
35
36
for subscription in subscriber .list_topic_subscriptions (topic_path ):
36
37
print (subscription )
38
+ # [END pubsub_list_topic_subscriptions]
37
39
38
40
39
41
def list_subscriptions_in_project (project ):
40
42
"""Lists all subscriptions in the current project."""
43
+ # [START pubsub_list_subscriptions]
41
44
subscriber = pubsub_v1 .SubscriberClient ()
42
45
project_path = subscriber .project_path (project )
43
46
44
47
for subscription in subscriber .list_subscriptions (project_path ):
45
48
print (subscription .name )
49
+ # [END pubsub_list_subscriptions]
46
50
47
51
48
52
def create_subscription (project , topic_name , subscription_name ):
49
53
"""Create a new pull subscription on the given topic."""
54
+ # [START pubsub_create_pull_subscription]
50
55
subscriber = pubsub_v1 .SubscriberClient ()
51
56
topic_path = subscriber .topic_path (project , topic_name )
52
57
subscription_path = subscriber .subscription_path (
@@ -56,16 +61,16 @@ def create_subscription(project, topic_name, subscription_name):
56
61
subscription_path , topic_path )
57
62
58
63
print ('Subscription created: {}' .format (subscription ))
64
+ # [END pubsub_create_pull_subscription]
59
65
60
66
61
67
def create_push_subscription (project ,
62
68
topic_name ,
63
69
subscription_name ,
64
70
endpoint ):
65
- """Create a new push subscription on the given topic.
66
- For example, endpoint is
67
- "https://my-test-project.appspot.com/push".
68
- """
71
+ """Create a new push subscription on the given topic."""
72
+ # [START pubsub_create_push_subscription]
73
+ # endpoint = "https://my-test-project.appspot.com/push"
69
74
subscriber = pubsub_v1 .SubscriberClient ()
70
75
topic_path = subscriber .topic_path (project , topic_name )
71
76
subscription_path = subscriber .subscription_path (
@@ -79,26 +84,30 @@ def create_push_subscription(project,
79
84
80
85
print ('Push subscription created: {}' .format (subscription ))
81
86
print ('Endpoint for subscription is: {}' .format (endpoint ))
87
+ # [END pubsub_create_push_subscription]
82
88
83
89
84
90
def delete_subscription (project , subscription_name ):
85
91
"""Deletes an existing Pub/Sub topic."""
92
+ # [START pubsub_delete_subscription]
86
93
subscriber = pubsub_v1 .SubscriberClient ()
87
94
subscription_path = subscriber .subscription_path (
88
95
project , subscription_name )
89
96
90
97
subscriber .delete_subscription (subscription_path )
91
98
92
99
print ('Subscription deleted: {}' .format (subscription_path ))
100
+ # [END pubsub_delete_subscription]
93
101
94
102
95
103
def update_subscription (project , subscription_name , endpoint ):
96
104
"""
97
105
Updates an existing Pub/Sub subscription's push endpoint URL.
98
106
Note that certain properties of a subscription, such as
99
- its topic, are not modifiable. For example, endpoint is
100
- "https://my-test-project.appspot.com/push".
107
+ its topic, are not modifiable.
101
108
"""
109
+ # [START pubsub_update_push_configuration]
110
+ # endpoint = "https://my-test-project.appspot.com/push"
102
111
subscriber = pubsub_v1 .SubscriberClient ()
103
112
subscription_path = subscriber .subscription_path (
104
113
project , subscription_name )
@@ -122,10 +131,13 @@ def update_subscription(project, subscription_name, endpoint):
122
131
print ('Subscription updated: {}' .format (subscription_path ))
123
132
print ('New endpoint for subscription is: {}' .format (
124
133
result .push_config ))
134
+ # [END pubsub_update_push_configuration]
125
135
126
136
127
137
def receive_messages (project , subscription_name ):
128
138
"""Receives messages from a pull subscription."""
139
+ # [START pubsub_subscriber_async_pull]
140
+ # [START pubsub_quickstart_subscriber]
129
141
subscriber = pubsub_v1 .SubscriberClient ()
130
142
subscription_path = subscriber .subscription_path (
131
143
project , subscription_name )
@@ -141,10 +153,13 @@ def callback(message):
141
153
print ('Listening for messages on {}' .format (subscription_path ))
142
154
while True :
143
155
time .sleep (60 )
156
+ # [END pubsub_subscriber_async_pull]
157
+ # [END pubsub_quickstart_subscriber]
144
158
145
159
146
160
def receive_messages_with_custom_attributes (project , subscription_name ):
147
161
"""Receives messages from a pull subscription."""
162
+ # [START pubsub_subscriber_sync_pull_custom_attributes]
148
163
subscriber = pubsub_v1 .SubscriberClient ()
149
164
subscription_path = subscriber .subscription_path (
150
165
project , subscription_name )
@@ -165,10 +180,12 @@ def callback(message):
165
180
print ('Listening for messages on {}' .format (subscription_path ))
166
181
while True :
167
182
time .sleep (60 )
183
+ # [END pubsub_subscriber_sync_pull_custom_attributes]
168
184
169
185
170
186
def receive_messages_with_flow_control (project , subscription_name ):
171
187
"""Receives messages from a pull subscription with flow control."""
188
+ # [START pubsub_subscriber_flow_settings]
172
189
subscriber = pubsub_v1 .SubscriberClient ()
173
190
subscription_path = subscriber .subscription_path (
174
191
project , subscription_name )
@@ -187,10 +204,12 @@ def callback(message):
187
204
print ('Listening for messages on {}' .format (subscription_path ))
188
205
while True :
189
206
time .sleep (60 )
207
+ # [END pubsub_subscriber_flow_settings]
190
208
191
209
192
210
def listen_for_errors (project , subscription_name ):
193
211
"""Receives messages and catches errors from a pull subscription."""
212
+ # [START pubsub_subscriber_error_listener]
194
213
subscriber = pubsub_v1 .SubscriberClient ()
195
214
subscription_path = subscriber .subscription_path (
196
215
project , subscription_name )
@@ -210,6 +229,7 @@ def callback(message):
210
229
'Listening for messages on {} threw an Exception: {}.' .format (
211
230
subscription_name , e ))
212
231
raise
232
+ # [END pubsub_subscriber_error_listener]
213
233
214
234
215
235
if __name__ == '__main__' :
0 commit comments