Client(batch_settings=(), **kwargs)
A publisher client for Google Cloud Pub/Sub.
This creates an object that is capable of publishing messages. Generally, you can instantiate this client with no arguments, and you get sensible defaults.
Parameters | |
---|---|
Name | Description |
kwargs |
dict
Any additional arguments provided are sent as keyword arguments to the underlying |
batch_settings |
BatchSettings
The settings for batch publishing. |
Properties
target
Return the target (where the API is).
Returns | |
---|---|
Type | Description |
str | The location of the API. |
Methods
create_topic
create_topic(name, labels=None, message_storage_policy=None, kms_key_name=None, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
Creates the given topic with the given name. See the resource name rules.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
name = client.topic_path('[PROJECT]', '[TOPIC]')
response = client.create_topic(name)
Parameters | |
---|---|
Name | Description |
name |
str
The name of the topic. It must have the format |
labels |
dict[str -> str]
|
message_storage_policy |
Union[dict, MessageStoragePolicy]
Policy constraining how messages published to the topic may be stored. It is determined when the topic is created based on the policy configured at the project level. It must not be set by the caller in the request to CreateTopic or to UpdateTopic. This field will be populated in the responses for GetTopic, CreateTopic, and UpdateTopic: if not present in the response, then no constraints are in effect. If a dict is provided, it must be of the same form as the protobuf message MessageStoragePolicy |
kms_key_name |
str
The resource name of the Cloud KMS CryptoKey to be used to protect access to messages published on this topic. The expected format is |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |
delete_topic
delete_topic(topic, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
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_
.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
topic = client.topic_path('[PROJECT]', '[TOPIC]')
client.delete_topic(topic)
Parameters | |
---|---|
Name | Description |
topic |
str
Name of the topic to delete. Format is |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |
from_service_account_file
from_service_account_file(filename, batch_settings=(), **kwargs)
Creates an instance of this client using the provided credentials file.
Parameters | |
---|---|
Name | Description |
filename |
str
The path to the service account private key json file. |
batch_settings |
BatchSettings
The settings for batch publishing. |
Returns | |
---|---|
Type | Description |
PublisherClient | The constructed client. |
from_service_account_json
from_service_account_json(filename, batch_settings=(), **kwargs)
Creates an instance of this client using the provided credentials file.
Parameters | |
---|---|
Name | Description |
filename |
str
The path to the service account private key json file. |
batch_settings |
BatchSettings
The settings for batch publishing. |
Returns | |
---|---|
Type | Description |
PublisherClient | The constructed client. |
get_iam_policy
get_iam_policy(resource, options_=None, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
Gets the access control policy for a resource. Returns an empty policy if the resource exists and does not have a policy set.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
resource = client.topic_path('[PROJECT]', '[TOPIC]')
response = client.get_iam_policy(resource)
Parameters | |
---|---|
Name | Description |
resource |
str
REQUIRED: The resource for which the policy is being requested. See the operation documentation for the appropriate value for this field. |
options_ |
Union[dict, GetPolicyOptions]
OPTIONAL: A |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |
get_topic
get_topic(topic, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
Gets the configuration of a topic.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
topic = client.topic_path('[PROJECT]', '[TOPIC]')
response = client.get_topic(topic)
Parameters | |
---|---|
Name | Description |
topic |
str
The name of the topic to get. Format is |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |
list_topic_subscriptions
list_topic_subscriptions(topic, page_size=None, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
Lists the names of the subscriptions on this topic.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
topic = client.topic_path('[PROJECT]', '[TOPIC]')
Iterate over all results
for element in client.list_topic_subscriptions(topic): ... # process element ... pass
Alternatively:
Iterate over results one page at a time
for page in client.list_topic_subscriptions(topic).pages: ... for element in page: ... # process element ... pass
Parameters | |
---|---|
Name | Description |
topic |
str
The name of the topic that subscriptions are attached to. Format is |
page_size |
int
The maximum number of resources contained in the underlying API response. If page streaming is performed per- resource, this parameter does not affect the return value. If page streaming is performed per-page, this determines the maximum number of resources in a page. |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |
list_topics
list_topics(project, page_size=None, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
Lists matching topics.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
project = client.project_path('[PROJECT]')
Iterate over all results
for element in client.list_topics(project): ... # process element ... pass
Alternatively:
Iterate over results one page at a time
for page in client.list_topics(project).pages: ... for element in page: ... # process element ... pass
Parameters | |
---|---|
Name | Description |
project |
str
The name of the project in which to list topics. Format is |
page_size |
int
The maximum number of resources contained in the underlying API response. If page streaming is performed per- resource, this parameter does not affect the return value. If page streaming is performed per-page, this determines the maximum number of resources in a page. |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |
project_path
project_path(project)
Return a fully-qualified project string.
publish
publish(topic, data, **attrs)
Publish a single message.
The reason that this is so important (and why we do not try to coerce for you) is because Pub/Sub is also platform independent and there is no way to know how to decode messages properly on the other side; therefore, encoding and decoding is a required exercise for the developer.
Add the given message to this object; this will cause it to be published once the batch either has enough messages or a sufficient period of time has elapsed.
.. rubric:: Example
from google.cloud import pubsub_v1 client = pubsub_v1.PublisherClient() topic = client.topic_path('[PROJECT]', '[TOPIC]') data = b'The rain in Wales falls mainly on the snails.' response = client.publish(topic, data, username='guido')
Parameters | |
---|---|
Name | Description |
attrs |
Mapping[str, str]
A dictionary of attributes to be sent as metadata. (These may be text strings or byte strings.) |
topic |
str
The topic to publish messages to. |
data |
bytes
A bytestring representing the message body. This must be a bytestring. |
Returns | |
---|---|
Type | Description |
| An object conforming to the concurrent.futures.Future interface (but not an instance of that class). |
set_iam_policy
set_iam_policy(resource, policy, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
Sets the access control policy on the specified resource. Replaces any existing policy.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
resource = client.topic_path('[PROJECT]', '[TOPIC]')
TODO: Initialize
policy
:policy = {}
response = client.set_iam_policy(resource, policy)
Parameters | |
---|---|
Name | Description |
resource |
str
REQUIRED: The resource for which the policy is being specified. See the operation documentation for the appropriate value for this field. |
policy |
Union[dict, Policy]
REQUIRED: The complete policy to be applied to the |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |
test_iam_permissions
test_iam_permissions(resource, permissions, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
Returns permissions that a caller has on the specified resource. If the resource does not exist, this will return an empty set of permissions, not a NOT_FOUND error.
Note: This operation is designed to be used for building permission-aware UIs and command-line tools, not for authorization checking. This operation may "fail open" without warning.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
resource = client.topic_path('[PROJECT]', '[TOPIC]')
TODO: Initialize
permissions
:permissions = []
response = client.test_iam_permissions(resource, permissions)
Parameters | |
---|---|
Name | Description |
resource |
str
REQUIRED: The resource for which the policy detail is being requested. See the operation documentation for the appropriate value for this field. |
permissions |
list[str]
The set of permissions to check for the |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |
topic_path
topic_path(project, topic)
Return a fully-qualified topic string.
update_topic
update_topic(topic, update_mask, retry=<_MethodDefault._DEFAULT_VALUE: <object object>>, timeout=<_MethodDefault._DEFAULT_VALUE: <object object>>, metadata=None)
Updates an existing topic. Note that certain properties of a topic are not modifiable.
.. rubric:: Example
from google.cloud import pubsub_v1
client = pubsub_v1.PublisherClient()
TODO: Initialize
topic
:topic = {}
TODO: Initialize
update_mask
:update_mask = {}
response = client.update_topic(topic, update_mask)
Parameters | |
---|---|
Name | Description |
topic |
Union[dict, Topic]
The updated topic object. If a dict is provided, it must be of the same form as the protobuf message Topic |
update_mask |
Union[dict, FieldMask]
Indicates which fields in the provided topic to update. Must be specified and non-empty. Note that if |
retry |
Optional[google.api_core.retry.Retry]
A retry object used to retry requests. If |
timeout |
Optional[float]
The amount of time, in seconds, to wait for the request to complete. Note that if |
metadata |
Optional[Sequence[Tuple[str, str]]]
Additional metadata that is provided to the method. |
Exceptions | |
---|---|
Type | Description |
google.api_core.exceptions.GoogleAPICallError | If the request failed for any reason. |
google.api_core.exceptions.RetryError | If the request failed due to a retryable error and retry attempts failed. |
ValueError | If the parameters are invalid. |