|
| 1 | +``gcloud.pubsub`` API |
| 2 | +===================== |
| 3 | + |
| 4 | +Connection / Authorization |
| 5 | +-------------------------- |
| 6 | + |
| 7 | +- Inferred defaults used to create connection if none configured explicitly: |
| 8 | + |
| 9 | + - credentials (derived from GAE / GCE environ if present). |
| 10 | + |
| 11 | + - ``project_id`` (derived from GAE / GCE environ if present). |
| 12 | + |
| 13 | + - ``scopes`` |
| 14 | + |
| 15 | + |
| 16 | +Manage topics for a project |
| 17 | +--------------------------- |
| 18 | + |
| 19 | +Create a new topic for the default project: |
| 20 | + |
| 21 | +.. doctest:: |
| 22 | + |
| 23 | + >>> from gcloud.pubsub.topic import Topic |
| 24 | + >>> topic = Topic('topic_name') |
| 25 | + >>> topic.create() # API request |
| 26 | + |
| 27 | +Create a new topic for an explicit project: |
| 28 | + |
| 29 | +.. doctest:: |
| 30 | + |
| 31 | + >>> from gcloud.pubsub.topic import Topic |
| 32 | + >>> topic = Topic('topic_name', project_id='my.project') |
| 33 | + >>> topic.create() # API request |
| 34 | + |
| 35 | +Check for the existance of a topic: |
| 36 | + |
| 37 | +.. doctest:: |
| 38 | + |
| 39 | + >>> from gcloud.pubsub.topic import Topic |
| 40 | + >>> topic = Topic('topic_name') |
| 41 | + >>> topic.exists() # API request |
| 42 | + True |
| 43 | + |
| 44 | +List topics for the default project: |
| 45 | + |
| 46 | +.. doctest:: |
| 47 | + |
| 48 | + >>> from gcloud import pubsub |
| 49 | + >>> [topic.name for topic in pubsub.list_topics()] # API request |
| 50 | + ['topic_name'] |
| 51 | + |
| 52 | +List topics for an explicit project: |
| 53 | + |
| 54 | +.. doctest:: |
| 55 | + |
| 56 | + >>> from gcloud import pubsub |
| 57 | + >>> topics = pubsub.list_topics(project_id='my.project') # API request |
| 58 | + >>> [topic.name for topic in topics] |
| 59 | + ['topic_name'] |
| 60 | + |
| 61 | +Delete a topic: |
| 62 | + |
| 63 | +.. doctest:: |
| 64 | + |
| 65 | + >>> from gcloud.pubsub.topic import Topic |
| 66 | + >>> topic = Topic('topic_name') |
| 67 | + >>> topic.delete() # API request |
| 68 | + |
| 69 | + |
| 70 | +Publish messages to a topic |
| 71 | +--------------------------- |
| 72 | + |
| 73 | +Publish a single message to a topic, without attributes: |
| 74 | + |
| 75 | +.. doctest:: |
| 76 | + |
| 77 | + >>> from gcloud.pubsub.topic import Topic |
| 78 | + >>> topic = Topic('topic_name') |
| 79 | + >>> topic.publish('this is the message_payload') # API request |
| 80 | + <message_id> |
| 81 | + |
| 82 | +Publish a single message to a topic, with attributes: |
| 83 | + |
| 84 | +.. doctest:: |
| 85 | + |
| 86 | + >>> from gcloud.pubsub.topic import Topic |
| 87 | + >>> topic = Topic('topic_name') |
| 88 | + >>> topic.publish('this is another message_payload', |
| 89 | + ... attr1='value1', attr2='value2') # API request |
| 90 | + <message_id> |
| 91 | + |
| 92 | +Publish a set of messages to a topic (as a single request): |
| 93 | + |
| 94 | +.. doctest:: |
| 95 | + |
| 96 | + >>> from gcloud.pubsub.topic import Topic |
| 97 | + >>> topic = Topic('topic_name') |
| 98 | + >>> with topic.batch() as batch: |
| 99 | + ... batch.publish('this is the first message_payload') |
| 100 | + ... batch.publish('this is the second message_payload', |
| 101 | + ... attr1='value1', attr2='value2') |
| 102 | + >>> list(batch) |
| 103 | + [<message_id1>, <message_id2>] |
| 104 | + |
| 105 | +.. note:: |
| 106 | + |
| 107 | + The only API request happens during the ``__exit__()`` of the topic |
| 108 | + used as a context manager. |
| 109 | + |
| 110 | + |
| 111 | +Manage subscriptions to topics |
| 112 | +------------------------------ |
| 113 | + |
| 114 | +Create a new pull subscription for a topic: |
| 115 | + |
| 116 | +.. doctest:: |
| 117 | + |
| 118 | + >>> from gcloud.pubsub.topic import Topic |
| 119 | + >>> from gcloud.pubsub.subscription import Subscription |
| 120 | + >>> topic = Topic('topic_name') |
| 121 | + >>> subscription = Subscription('subscription_name', topic) |
| 122 | + >>> subscription.create() # API request |
| 123 | + |
| 124 | +Create a new pull subscription for a topic with a non-default ACK deadline: |
| 125 | + |
| 126 | +.. doctest:: |
| 127 | + |
| 128 | + >>> from gcloud.pubsub.topic import Topic |
| 129 | + >>> from gcloud.pubsub.subscription import Subscription |
| 130 | + >>> topic = Topic('topic_name') |
| 131 | + >>> subscription = Subscription('subscription_name', ack_deadline=90) |
| 132 | + >>> subscription.create() # API request |
| 133 | + |
| 134 | +Create a new push subscription for a topic: |
| 135 | + |
| 136 | +.. doctest:: |
| 137 | + |
| 138 | + >>> ENDPOINT = 'https://example.com/hook' |
| 139 | + >>> from gcloud.pubsub.topic import Topic |
| 140 | + >>> from gcloud.pubsub.subscription import Subscription |
| 141 | + >>> topic = Topic('topic_name') |
| 142 | + >>> subscription = Subscription('subscription_name', push_endpoint=ENDPOINT) |
| 143 | + >>> subscription.create() # API request |
| 144 | + |
| 145 | +Check for the existence of a subscription: |
| 146 | + |
| 147 | +.. doctest:: |
| 148 | + |
| 149 | + >>> from gcloud.pubsub.topic import Topic |
| 150 | + >>> from gcloud.pubsub.subscription import Subscription |
| 151 | + >>> topic = Topic('topic_name') |
| 152 | + >>> subscription = Subscription('subscription_name', topic) |
| 153 | + >>> subscription.exists() # API request |
| 154 | + True |
| 155 | + |
| 156 | +Convert a pull subscription to push: |
| 157 | + |
| 158 | +.. doctest:: |
| 159 | + |
| 160 | + >>> ENDPOINT = 'https://example.com/hook' |
| 161 | + >>> from gcloud.pubsub.topic import Topic |
| 162 | + >>> from gcloud.pubsub.subscription import Subscription |
| 163 | + >>> topic = Topic('topic_name') |
| 164 | + >>> subscription = Subscription('subscription_name', topic) |
| 165 | + >>> subscription.modify_push_configuration(push_endpoint=ENDPOINT) # API request |
| 166 | + |
| 167 | +Convert a push subscription to pull: |
| 168 | + |
| 169 | +.. doctest:: |
| 170 | + |
| 171 | + >>> ENDPOINT = 'https://example.com/hook' |
| 172 | + >>> from gcloud.pubsub.topic import Topic |
| 173 | + >>> topic = Topic('topic_name') |
| 174 | + >>> subscription = Subscription('subscription_name', topic, |
| 175 | + ... push_endpoint=ENDPOINT) |
| 176 | + >>> subscription.modify_push_configuration(push_endpoint=None) # API request |
| 177 | + |
| 178 | +List subscriptions for a topic: |
| 179 | + |
| 180 | +.. doctest:: |
| 181 | + |
| 182 | + >>> from gcloud.pubsub.topic import Topic |
| 183 | + >>> topic = Topic('topic_name') |
| 184 | + >>> subscriptions = topic.list_subscriptions() # API request |
| 185 | + >>> [subscription.name for subscription in subscriptions] |
| 186 | + ['subscription_name'] |
| 187 | + |
| 188 | +Delete a subscription: |
| 189 | + |
| 190 | +.. doctest:: |
| 191 | + |
| 192 | + >>> from gcloud.pubsub.topic import Topic |
| 193 | + >>> from gcloud.pubsub.subscription import Subscription |
| 194 | + >>> topic = Topic('topic_name') |
| 195 | + >>> subscription = Subscription('subscription_name', topic) |
| 196 | + >>> subscription.delete() # API request |
| 197 | + |
| 198 | + |
| 199 | +Pull messages from a subscription |
| 200 | +--------------------------------- |
| 201 | + |
| 202 | +Fetch pending messages for a pull subscription |
| 203 | + |
| 204 | +.. note:: |
| 205 | + |
| 206 | + The messages will have been ACKed already. |
| 207 | + |
| 208 | +.. doctest:: |
| 209 | + |
| 210 | + >>> from gcloud.pubsub.topic import Topic |
| 211 | + >>> from gcloud.pubsub.subscription import Subscription |
| 212 | + >>> topic = Topic('topic_name') |
| 213 | + >>> subscription = Subscription('subscription_name', topic) |
| 214 | + >>> with topic: |
| 215 | + ... topic.publish('this is the first message_payload') |
| 216 | + ... topic.publish('this is the second message_payload', |
| 217 | + ... attr1='value1', attr2='value2') |
| 218 | + >>> messages = subscription.pull() # API request |
| 219 | + >>> [message.id for message in messages] |
| 220 | + [<message_id1>, <message_id2>] |
| 221 | + >>> [message.data for message in messages] |
| 222 | + ['this is the first message_payload', 'this is the second message_payload'] |
| 223 | + >>> [message.attrs for message in messages] |
| 224 | + [{}, {'attr1': 'value1', 'attr2': 'value2'}] |
| 225 | + |
| 226 | +Fetch a limited number of pending messages for a pull subscription: |
| 227 | + |
| 228 | +.. doctest:: |
| 229 | + |
| 230 | + >>> from gcloud.pubsub.topic import Topic |
| 231 | + >>> from gcloud.pubsub.subscription import Subscription |
| 232 | + >>> topic = Topic('topic_name') |
| 233 | + >>> subscription = Subscription('subscription_name', topic) |
| 234 | + >>> with topic: |
| 235 | + ... topic.publish('this is the first message_payload') |
| 236 | + ... topic.publish('this is the second message_payload', |
| 237 | + ... attr1='value1', attr2='value2') |
| 238 | + >>> [message.id for message in subscription.pull(max_messages=1)] |
| 239 | + [<message_id1>] |
| 240 | + |
| 241 | +Fetch messages for a pull subscription without blocking (none pending): |
| 242 | + |
| 243 | +.. doctest:: |
| 244 | + |
| 245 | + >>> from gcloud.pubsub.topic import Topic |
| 246 | + >>> from gcloud.pubsub.subscription import Subscription |
| 247 | + >>> topic = Topic('topic_name') |
| 248 | + >>> subscription = Subscription('subscription_name', topic) |
| 249 | + >>> [message.id for message in subscription.pull(return_immediately=True)] |
| 250 | + [] |
| 251 | + |
0 commit comments