google.pubsub.v1.publisher_api

Accesses the google.pubsub.v1 Publisher API.

Classes

PublisherApi([service_path, port, channel, ...]) The service that an application uses to manipulate topics, and to send messages to a topic.
class google.pubsub.v1.publisher_api.PublisherApi(service_path='pubsub.googleapis.com', port=443, channel=None, metadata_transformer=None, ssl_creds=None, scopes=None, client_config=None, timeout=30, app_name='gax', app_version='0.12.2')[source]

The service that an application uses to manipulate topics, and to send messages to a topic.

Constructor.

Parameters:
  • service_path (string) – The domain name of the API remote host.
  • port (int) – The port on which to connect to the remote host.
  • channel (grpc.beta.implementations.Channel) – A Channel object through which to make calls.
  • ssl_creds (grpc.beta.implementations.ClientCredentials) – A ClientCredentials for use with an SSL-enabled channel.
  • client_config (dict) – A dictionary for call options for each method. See google.gax.construct_settings() for the structure of this data. Falls back to the default config if not specified or the specified config is missing data points.
  • metadata_transformer (Callable[[], list]) – A function that creates the metadata for requests.
  • timeout (int) – The default timeout, in seconds, for calls made through this client
  • app_name (string) – The codename of the calling service.
  • app_version (string) – The version of the calling service.
Returns:

A PublisherApi object.

DEFAULT_SERVICE_PORT = 443

The default port of the service.

SERVICE_ADDRESS = 'pubsub.googleapis.com'

The default address of the service.

create_topic(name, options=None)[source]

Creates the given topic with the given name.

Example

>>> from google.pubsub.v1.publisher_api import PublisherApi
>>> api = PublisherApi()
>>> name = api.topic_path('[PROJECT]', '[TOPIC]')
>>> response = api.create_topic(name)
Parameters:
  • name (string) – The name of the topic. It must have the format "projects/{project}/topics/{topic}". {topic} must start with a letter, and contain only letters ([A-Za-z]), numbers ([0-9]), dashes (-), underscores (_), periods (.), tildes (~), plus (+) or percent signs (%). It must be between 3 and 255 characters in length, and it must not start with "goog".
  • options (google.gax.CallOptions) – Overrides the default settings for this call, e.g, timeout, retries etc.
Returns:

A google.pubsub.v1.pubsub_pb2.Topic instance.

Raises:

google.gax.errors.GaxError if the RPC is aborted.

delete_topic(topic, options=None)[source]

Deletes the topic with the given name. Returns NOT_FOUND if the topic does not exist. After a topic is deleted, a new topic may be created with the same name; this is an entirely new topic with none of the old configuration or subscriptions. Existing subscriptions to this topic are not deleted, but their topic field is set to _deleted-topic_.

Example

>>> from google.pubsub.v1.publisher_api import PublisherApi
>>> api = PublisherApi()
>>> topic = api.topic_path('[PROJECT]', '[TOPIC]')
>>> api.delete_topic(topic)
Parameters:
  • topic (string) – Name of the topic to delete.
  • options (google.gax.CallOptions) – Overrides the default settings for this call, e.g, timeout, retries etc.
Raises:

google.gax.errors.GaxError if the RPC is aborted.

get_topic(topic, options=None)[source]

Gets the configuration of a topic.

Example

>>> from google.pubsub.v1.publisher_api import PublisherApi
>>> api = PublisherApi()
>>> topic = api.topic_path('[PROJECT]', '[TOPIC]')
>>> response = api.get_topic(topic)
Parameters:
  • topic (string) – The name of the topic to get.
  • options (google.gax.CallOptions) – Overrides the default settings for this call, e.g, timeout, retries etc.
Returns:

A google.pubsub.v1.pubsub_pb2.Topic instance.

Raises:

google.gax.errors.GaxError if the RPC is aborted.

list_topic_subscriptions(topic, page_size=0, options=None)[source]

Lists the name of the subscriptions for this topic.

Example

>>> from google.pubsub.v1.publisher_api import PublisherApi
>>> from google.gax import CallOptions, INITIAL_PAGE
>>> api = PublisherApi()
>>> topic = api.topic_path('[PROJECT]', '[TOPIC]')
>>>
>>> # Iterate over all results
>>> for element in api.list_topic_subscriptions(topic):
>>>   # process element
>>>   pass
>>>
>>> # Or iterate over results one page at a time
>>> for page in api.list_topic_subscriptions(topic, options=CallOptions(page_token=INITIAL_PAGE)):
>>>   for element in page:
>>>     # process element
>>>     pass
Parameters:
  • topic (string) – The name of the topic that subscriptions are attached to.
  • page_size (int) – Maximum number of subscription names to return.
  • options (google.gax.CallOptions) – Overrides the default settings for this call, e.g, timeout, retries etc.
Returns:

A google.gax.PageIterator instance. By default, this is an iterable of string instances. This object can also be configured to iterate over the pages of the response through the CallOptions parameter.

Raises:

google.gax.errors.GaxError if the RPC is aborted.

list_topics(project, page_size=0, options=None)[source]

Lists matching topics.

Example

>>> from google.pubsub.v1.publisher_api import PublisherApi
>>> from google.gax import CallOptions, INITIAL_PAGE
>>> api = PublisherApi()
>>> project = api.project_path('[PROJECT]')
>>>
>>> # Iterate over all results
>>> for element in api.list_topics(project):
>>>   # process element
>>>   pass
>>>
>>> # Or iterate over results one page at a time
>>> for page in api.list_topics(project, options=CallOptions(page_token=INITIAL_PAGE)):
>>>   for element in page:
>>>     # process element
>>>     pass
Parameters:
  • project (string) – The name of the cloud project that topics belong to.
  • page_size (int) – Maximum number of topics to return.
  • options (google.gax.CallOptions) – Overrides the default settings for this call, e.g, timeout, retries etc.
Returns:

A google.gax.PageIterator instance. By default, this is an iterable of google.pubsub.v1.pubsub_pb2.Topic instances. This object can also be configured to iterate over the pages of the response through the CallOptions parameter.

Raises:

google.gax.errors.GaxError if the RPC is aborted.

classmethod match_project_from_project_name(project_name)[source]

Parses the project from a project resource.

Parameters:project_name (string) – A fully-qualified path representing a project resource.
Returns:A string representing the project.
classmethod match_project_from_topic_name(topic_name)[source]

Parses the project from a topic resource.

Parameters:topic_name (string) – A fully-qualified path representing a topic resource.
Returns:A string representing the project.
classmethod match_topic_from_topic_name(topic_name)[source]

Parses the topic from a topic resource.

Parameters:topic_name (string) – A fully-qualified path representing a topic resource.
Returns:A string representing the topic.
classmethod project_path(project)[source]

Returns a fully-qualified project resource name string.

publish(topic, messages, options=None)[source]

Adds one or more messages to the topic. Returns NOT_FOUND if the topic does not exist. The message payload must not be empty; it must contain either a non-empty data field, or at least one attribute.

Example

>>> from google.pubsub.v1.publisher_api import PublisherApi
>>> from google.pubsub.v1 import pubsub_pb2
>>> api = PublisherApi()
>>> topic = api.topic_path('[PROJECT]', '[TOPIC]')
>>> data = ''
>>> messages_element = pubsub_pb2.PubsubMessage(data)
>>> messages = [messages_element]
>>> response = api.publish(topic, messages)
Parameters:
Returns:

A google.pubsub.v1.pubsub_pb2.PublishResponse instance.

Raises:

google.gax.errors.GaxError if the RPC is aborted.

classmethod topic_path(project, topic)[source]

Returns a fully-qualified topic resource name string.