Pub/Sub Client#

Client for interacting with the Google Cloud Pub/Sub API.

class google.cloud.pubsub.client.Client(project=None, credentials=None, http=None)[source]#

Bases: google.cloud.client.JSONClient

Client to bundle configuration needed for API requests.

Parameters:
  • project (string) – the project which the client acts on behalf of. Will be passed when creating a topic. If not passed, falls back to the default inferred from the environment.
  • credentials (oauth2client.client.OAuth2Credentials or NoneType) – The OAuth2 Credentials to use for the connection owned by this client. If not passed (and if no http object is passed), falls back to the default inferred from the environment.
  • http (httplib2.Http or class that defines request().) – An optional HTTP object to make requests. If not passed, an http object is created that is bound to the credentials for the current object.
iam_policy_api#

Helper for IAM policy-related API calls.

list_subscriptions(page_size=None, page_token=None)[source]#

List subscriptions for the project associated with this client.

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

Example:

    subscriptions, token = client.list_subscriptions()   # API request
    while True:
        for subscription in subscriptions:
            do_something_with(subscription)
        if token is None:
            break
        subscriptions, token = client.list_subscriptions(
            page_token=token)                           # API request
Parameters:
  • page_size (int) – maximum number of topics to return, If not passed, defaults to a value set by the API.
  • page_token (string) – opaque marker for the next “page” of topics. If not passed, the API will return the first page of topics.
Return type:

tuple, (list, str)

Returns:

list of Subscription, plus a “next page token” string: if not None, indicates that more topics can be retrieved with another call (pass that value as page_token).

list_topics(page_size=None, page_token=None)[source]#

List topics for the project associated with this client.

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

Example:

    topics, token = client.list_topics()   # API request
    while True:
        for topic in topics:
            do_something_with(topic)
        if token is None:
            break
        topics, token = client.list_topics(page_token=token)  # API request
Parameters:
  • page_size (int) – maximum number of topics to return, If not passed, defaults to a value set by the API.
  • page_token (string) – opaque marker for the next “page” of topics. If not passed, the API will return the first page of topics.
Return type:

tuple, (list, str)

Returns:

list of google.cloud.pubsub.topic.Topic, plus a “next page token” string: if not None, indicates that more topics can be retrieved with another call (pass that value as page_token).

publisher_api#

Helper for publisher-related API calls.

subscriber_api#

Helper for subscriber-related API calls.

topic(name, timestamp_messages=False)[source]#

Creates a topic bound to the current client.

Example:


Parameters:
  • name (string) – the name of the topic to be constructed.
  • timestamp_messages (boolean) – To be passed to Topic constructor.
Return type:

google.cloud.pubsub.topic.Topic

Returns:

Topic created with the current client.

Connection#

Create / interact with Google Cloud Pub/Sub connections.

class google.cloud.pubsub.connection.Connection(credentials=None, http=None, api_base_url=None)[source]#

Bases: google.cloud.connection.JSONConnection

A connection to Google Cloud Pub/Sub via the JSON REST API.

Parameters:
API_BASE_URL = 'https://pubsub.googleapis.com'#

The base of the API call URL.

API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'#

A template for the URL of a particular API call.

API_VERSION = 'v1'#

The version of the API, used in building the API call’s URL.

SCOPE = ('https://www.googleapis.com/auth/pubsub', 'https://www.googleapis.com/auth/cloud-platform')#

The scopes required for authenticating as a Cloud Pub/Sub consumer.

build_api_url(path, query_params=None, api_base_url=None, api_version=None)[source]#

Construct an API url given a few components, some optional.

Typically, you shouldn’t need to use this method.

Parameters:
  • path (string) – The path to the resource.
  • query_params (dict or list) – A dictionary of keys and values (or list of key-value pairs) to insert into the query string of the URL.
  • api_base_url (string) – The base URL for the API endpoint. Typically you won’t have to provide this.
  • api_version (string) – The version of the API to call. Typically you shouldn’t provide this and instead use the default for the library.
Return type:

string

Returns:

The URL assembled from the pieces provided.