Python

Report an Issue

Topics#

Define API Topics.

class gcloud.pubsub.topic.Batch(topic, connection=None)[source]#

Bases: object

Context manager: collect messages to publish via a single API call.

Helper returned by :meth:Topic.batch

Parameters:
commit(connection=None)[source]#

Send saved messages as a single API call.

Parameters:connection (gcloud.pubsub.connection.Connection or None) – the connection to use. If not passed, falls back to the connection attribute.
publish(message, **attrs)[source]#

Emulate publishing a message, but save it.

Parameters:message (bytes) – the message payload
Message attrs:key-value pairs to send as message attributes
class gcloud.pubsub.topic.Topic(name, project=None, timestamp_messages=False)[source]#

Bases: object

Topics are targets to which messages can be published.

Subscribers then receive those messages.

See: https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics

Parameters:
  • name (string) – the name of the topic
  • project (string) – the project to which the topic belongs. If not passed, falls back to the default inferred from the environment.
  • timestamp_messages (boolean) – If true, the topic will add a timestamp key to the attributes of each published message: the value will be an RFC 3339 timestamp.
batch(connection=None)[source]#

Return a batch to use as a context manager.

Return type::class:Batch
create(connection=None)[source]#

API call: create the topic via a PUT request

See: https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/create

Parameters:connection (gcloud.pubsub.connection.Connection or None) – the connection to use. If not passed, falls back to the connection attribute.
delete(connection=None)[source]#

API call: delete the topic via a DELETE request

See: https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/delete

Parameters:connection (gcloud.pubsub.connection.Connection or None) – the connection to use. If not passed, falls back to the connection attribute.
exists(connection=None)[source]#

API call: test for the existence of the topic via a GET request

See https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/get

Parameters:connection (gcloud.pubsub.connection.Connection or None) – the connection to use. If not passed, falls back to the connection attribute.
classmethod from_api_repr(resource)[source]#

Factory: construct a topic given its API representation

Parameters:resource (dict) – topic resource representation returned from the API
Return type:gcloud.pubsub.topic.Topic
full_name#

Fully-qualified name used in topic / subscription APIs

path#

URL path for the topic’s APIs

publish(message, connection=None, **attrs)[source]#

API call: publish a message to a topic via a POST request

See: https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/publish

Parameters:
  • message (bytes) – the message payload
  • connection (gcloud.pubsub.connection.Connection or None) – the connection to use. If not passed, falls back to the connection attribute.
Message attrs:

key-value pairs to send as message attributes

Return type:

str

Returns:

message ID assigned by the server to the published message