From fda26bcb2cbf91edb6061884d18a7401ad2c72cd Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Thu, 19 Nov 2020 12:33:44 -0800 Subject: [PATCH] docs: add documentation (#66) * add license header and use try * add types to nav * add admin docs * add publisher * add subscriber and update index * remove docs for api services * clean up types * mention disabled flow control * update subscriber sample * clean up types * use CloudRegion in samples and docs * add space after code-block * add empty line after code-block --- docs/admin/api/client.rst | 6 + docs/admin/index.rst | 80 ++++++++++++++ docs/conf.py | 4 +- docs/index.rst | 8 +- docs/publisher/api/client.rst | 6 + docs/publisher/index.rst | 60 ++++++++++ docs/pubsublite_v1/services.rst | 21 ---- docs/pubsublite_v1/types.rst | 6 - docs/subscriber/api/client.rst | 6 + docs/subscriber/index.rst | 104 ++++++++++++++++++ docs/types.rst | 18 +++ google/__init__.py | 16 +++ google/cloud/__init__.py | 16 +++ .../cloud/pubsublite/cloudpubsub/__init__.py | 21 +++- google/cloud/pubsublite/types/__init__.py | 11 ++ .../create_lite_subscription_example.py | 10 +- samples/snippets/create_lite_topic_example.py | 9 +- .../delete_lite_subscription_example.py | 6 +- samples/snippets/delete_lite_topic_example.py | 6 +- .../snippets/get_lite_subscription_example.py | 6 +- samples/snippets/get_lite_topic_example.py | 6 +- ...t_lite_subscriptions_in_project_example.py | 9 +- ...ist_lite_subscriptions_in_topic_example.py | 6 +- samples/snippets/list_lite_topics_example.py | 6 +- samples/snippets/subscriber_example.py | 2 + .../update_lite_subscription_example.py | 9 +- samples/snippets/update_lite_topic_example.py | 9 +- 27 files changed, 387 insertions(+), 80 deletions(-) create mode 100644 docs/admin/api/client.rst create mode 100644 docs/admin/index.rst create mode 100644 docs/publisher/api/client.rst create mode 100644 docs/publisher/index.rst delete mode 100644 docs/pubsublite_v1/services.rst delete mode 100644 docs/pubsublite_v1/types.rst create mode 100644 docs/subscriber/api/client.rst create mode 100644 docs/subscriber/index.rst create mode 100644 docs/types.rst diff --git a/docs/admin/api/client.rst b/docs/admin/api/client.rst new file mode 100644 index 00000000..f503b341 --- /dev/null +++ b/docs/admin/api/client.rst @@ -0,0 +1,6 @@ +Admin Client API +================ + +.. automodule:: google.cloud.pubsublite.admin_client + :members: + :inherited-members: \ No newline at end of file diff --git a/docs/admin/index.rst b/docs/admin/index.rst new file mode 100644 index 00000000..31f356fa --- /dev/null +++ b/docs/admin/index.rst @@ -0,0 +1,80 @@ +Admin Operations +================ + +Admin operations are handled through the +:class:`~.pubsublite.admin_client.AdminClient` class (aliased as +``google.cloud.pubsublite.AdminClient``). + +Instantiating an admin client requires you to provide a valid cloud region +for the Pub/Sub Lite service: + +.. code-block:: python + + from google.cloud.pubsublite import AdminClient + cloud_region = CloudRegion("us-central1") + admin_client = AdminClient(cloud_region) + + +Create a topic +-------------- + +To create a message, use the +:meth:`~.pubsublite.admin_client.AdminClient.create_topic` method. This method accepts +one positional arguments: a :class:`~.pubsublite_v1.types.Topic` object, where the name +of the topic is passed along as a string. + +Pub/Sub Lite topics have the canonical form of + + ``projects/{project_number}/locations/{location}/topics/{topic_id}`` + +A location (a.k.a. `zone`_) is comprised of a cloud region and a zone ID. + +.. _zone: https://cloud.google.com/pubsub/lite/docs/locations/ + +A call to create a Pub/Sub Lite topic looks like: + +.. code-block:: python + + from google.cloud.pubsublite import AdminClient, Topic + from google.cloud.pubsublite.types import ( + CloudRegion, CloudZone, TopicPath, + ) + + project_number = 1122334455 + zone_id = "a" + topic_id = "your-topic-id" + + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) + topic_path = TopicPath(project_number, location, topic_id) + + topic = Topic( + name=str(topic_path), + partition_config=Topic.PartitionConfig( + # 1 partition + count=1, + # Publish at 4 MiB/s and subscribe at 8 MiB/s + capacity=Topic.PartitionConfig.Capacity( + publish_mib_per_sec=4, + subscribe_mib_per_sec=8, + ), + ), + retention_config=Topic.RetentionConfig( + # 30 GiB + per_partition_bytes=30 * 1024 * 1024 * 1024, + # 7 days + period=Duration(seconds=60 * 60 * 24 * 7), + ), + ) + + admin_client = AdminClient(cloud_region) + response = admin_client.create_topic(topic) + + +API Reference +------------- + +.. toctree:: + :maxdepth: 2 + + api/client diff --git a/docs/conf.py b/docs/conf.py index e76c0d24..ff0e2917 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -345,10 +345,10 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { - "python": ("http://python.readthedocs.org/en/latest/", None), + "python": ("https://python.readthedocs.io/en/latest/", None), "google-auth": ("https://google-auth.readthedocs.io/en/stable", None), "google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,), - "grpc": ("https://grpc.io/grpc/python/", None), + "grpc": ("https://grpc.github.io/grpc/python/", None), "proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None), } diff --git a/docs/index.rst b/docs/index.rst index df51ac02..13e774e7 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -5,10 +5,12 @@ API Reference ------------- .. toctree:: - :maxdepth: 2 + :maxdepth: 3 - pubsublite_v1/services - pubsublite_v1/types + Admin Client + Publisher Client + Subscriber Client + Types Changelog --------- diff --git a/docs/publisher/api/client.rst b/docs/publisher/api/client.rst new file mode 100644 index 00000000..6afd2806 --- /dev/null +++ b/docs/publisher/api/client.rst @@ -0,0 +1,6 @@ +Publisher Client API +==================== + +.. automodule:: google.cloud.pubsublite.cloudpubsub.publisher_client + :members: + :inherited-members: \ No newline at end of file diff --git a/docs/publisher/index.rst b/docs/publisher/index.rst new file mode 100644 index 00000000..ce545240 --- /dev/null +++ b/docs/publisher/index.rst @@ -0,0 +1,60 @@ +Publisher Client +================ + +Publish operations are handled through the +:class:`~.pubsublite.cloudpubsub.publisher_client.PublisherClient` class (aliased as +``google.cloud.pubsublite.cloudpubsub.PublisherClient``). + +You should instantiate a publisher client using a context manager: + +.. code-block:: python + + from google.cloud.pubsublite.cloudpubsub import PublisherClient + + with PublisherClient() as publisher_client: + # Use publisher_client + +When not using a context manager, you need to call +:meth:`~.pubsublite.cloudpubsub.publisher_client.PublisherClient.__enter__`. + +Publish a message +----------------- + +To publish a message, use the +:meth:`~.pubsublite.cloudpubsub.publisher_client.PublisherClient.publish` +method. This method accepts +two positional arguments: a :class:`~.pubsublite.types.TopicPath` object +and a message in byte string. + +A call to publish a message looks like: + +.. code-block:: python + + from google.cloud.pubsublite.cloudpubsub import PublisherClient + from google.cloud.pubsublite.types import ( + CloudRegion, CloudZone, TopicPath, + ) + + project_number = 1122334455 + cloud_region = "us-central1" + zone_id = "a" + topic_id = "your-topic-id" + + location = CloudZone(CloudRegion(cloud_region), zone_id) + topic_path = TopicPath(project_number, location, topic_id) + + with PublisherClient() as publisher_client: + data = "Hello world!" + api_future = publisher_client.publish(t + opic_path, data.encode("utf-8") + ) + message_id = api_future.result() + + +API Reference +------------- + +.. toctree:: + :maxdepth: 2 + + api/client diff --git a/docs/pubsublite_v1/services.rst b/docs/pubsublite_v1/services.rst deleted file mode 100644 index 62c81825..00000000 --- a/docs/pubsublite_v1/services.rst +++ /dev/null @@ -1,21 +0,0 @@ -Services for Google Cloud Pubsublite v1 API -=========================================== - -.. automodule:: google.cloud.pubsublite_v1.services.admin_service - :members: - :inherited-members: -.. automodule:: google.cloud.pubsublite_v1.services.cursor_service - :members: - :inherited-members: -.. automodule:: google.cloud.pubsublite_v1.services.partition_assignment_service - :members: - :inherited-members: -.. automodule:: google.cloud.pubsublite_v1.services.publisher_service - :members: - :inherited-members: -.. automodule:: google.cloud.pubsublite_v1.services.subscriber_service - :members: - :inherited-members: -.. automodule:: google.cloud.pubsublite_v1.services.topic_stats_service - :members: - :inherited-members: diff --git a/docs/pubsublite_v1/types.rst b/docs/pubsublite_v1/types.rst deleted file mode 100644 index e8c0433f..00000000 --- a/docs/pubsublite_v1/types.rst +++ /dev/null @@ -1,6 +0,0 @@ -Types for Google Cloud Pubsublite v1 API -======================================== - -.. automodule:: google.cloud.pubsublite_v1.types - :members: - :show-inheritance: diff --git a/docs/subscriber/api/client.rst b/docs/subscriber/api/client.rst new file mode 100644 index 00000000..b7e16b11 --- /dev/null +++ b/docs/subscriber/api/client.rst @@ -0,0 +1,6 @@ +Subscriber Client API +===================== + +.. automodule:: google.cloud.pubsublite.cloudpubsub.subscriber_client + :members: + :inherited-members: \ No newline at end of file diff --git a/docs/subscriber/index.rst b/docs/subscriber/index.rst new file mode 100644 index 00000000..bd9dbc11 --- /dev/null +++ b/docs/subscriber/index.rst @@ -0,0 +1,104 @@ +Subscriber Client +================= + +Subscribe operations are handled through the +:class:`~.pubsublite.cloudpubsub.subscriber_client.SubscriberClient` class (aliased as +``google.cloud.pubsublite.cloudpubsub.SubscriberClient``). + +You should instantiate a subscriber client using a context manager: + +.. code-block:: python + + from google.cloud.pubsublite.cloudpubsub import SubscriberClient + + with SubscriberClient() as subscriber_client: + # Use subscriber_client + +When not using a context manager, you need to call +:meth:`~.pubsublite.cloudpubsub.subscriber_client.SubscriberClient.__enter__`. + +Receive messages +---------------- + +To receive messages, use the +:meth:`~.pubsublite.cloudpubsub.subscriber_client.SubscriberClient.subscribe` +method. This method requires +three positional arguments: a :class:`~.pubsublite.types.SubscriptionPath` object, +a callback function, and a :class:`~.pubsublite.types.FlowControlSettings` object. + +Receiving messages looks like: + +.. code-block:: python + + from google.cloud.pubsublite.cloudpubsub import SubscriberClient + from google.cloud.pubsublite.types import ( + CloudRegion, + CloudZone, + SubscriptionPath, + DISABLED_FLOW_CONTROL, + ) + + project_number = 1122334455 + cloud_region = "us-central1" + zone_id = "a" + subscription_id = "your-subscription-id" + + location = CloudZone(CloudRegion(cloud_region), zone_id) + subscription_path = SubscriptionPath(project_number, location, subscription_id) + + with SubscriberClient() as subscriber_client: + streaming_pull_future = subscriber_client.subscribe( + subscription_path, + callback=callback, + per_partition_flow_control_settings=DISABLED_FLOW_CONTROL, + ) + + streaming_pull_future.result() + +Subscriber Callbacks +-------------------- + +Received messages are processed in a callback function. This callback function +only takes one argument of type :class:`google.cloud.pubsub_v1.subscriber.message.Message`. +After this message has been processed, the function should either call +:meth:`~.pubsub_v1.subscriber.message.Message.ack` to acknowledge the message or +:meth:`~.pubsub_v1.subscriber.message.Message.nack` to send a negative acknowledgement. + +.. code-block:: python + + def callback(message): + message_data = message.data.decode("utf-8") + print(f"Received {message_data}.") + message.ack() + +Flow Control Settings +--------------------- + +Flow control settings are applied per partition. +They control when to pause the message stream to a partition so the server temporarily +stops sending out more messages (known as outstanding messages) from this partition. + +You can configure flow control settings by setting the maximum number and size of +outstanding messages. The message stream is paused when either condition is met. + +.. code-block:: python + + from google.cloud.pubsublite.types import FlowControlSettings + + flow_control_settings = FlowControlSettings( + # 1,000 outstanding messages. Must be >0. + messages_outstanding=1000, + # 10 MiB. Must be greater than the allowed size of the largest message (1 MiB). + bytes_outstanding=10 * 1024 * 1024, + ) + +You may also turn off flow control settings by setting it to +:class:`google.cloud.pubsublite.types.DISABLED_FLOW_CONTROL`. + +API Reference +------------- + +.. toctree:: + :maxdepth: 2 + + api/client diff --git a/docs/types.rst b/docs/types.rst new file mode 100644 index 00000000..28bc5f71 --- /dev/null +++ b/docs/types.rst @@ -0,0 +1,18 @@ +Pub/Sub Lite Client Types +========================= + +.. automodule:: google.cloud.pubsublite.types + :members: + :noindex: + +.. autoclass:: google.cloud.pubsublite_v1.Cursor + :members: + :noindex: + +.. autoclass:: google.cloud.pubsublite_v1.Subscription + :members: + :noindex: + +.. autoclass:: google.cloud.pubsublite_v1.Topic + :members: + :noindex: \ No newline at end of file diff --git a/google/__init__.py b/google/__init__.py index 2e435f54..d71b1e55 100644 --- a/google/__init__.py +++ b/google/__init__.py @@ -1,3 +1,19 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import pkg_resources pkg_resources.declare_namespace(__name__) diff --git a/google/cloud/__init__.py b/google/cloud/__init__.py index 2e435f54..d71b1e55 100644 --- a/google/cloud/__init__.py +++ b/google/cloud/__init__.py @@ -1,3 +1,19 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import pkg_resources pkg_resources.declare_namespace(__name__) diff --git a/google/cloud/pubsublite/cloudpubsub/__init__.py b/google/cloud/pubsublite/cloudpubsub/__init__.py index cce27228..01de92bf 100644 --- a/google/cloud/pubsublite/cloudpubsub/__init__.py +++ b/google/cloud/pubsublite/cloudpubsub/__init__.py @@ -15,13 +15,26 @@ # flake8: noqa from .message_transformer import MessageTransformer from .nack_handler import NackHandler +from .publisher_client import AsyncPublisherClient, PublisherClient from .publisher_client_interface import ( - PublisherClientInterface, AsyncPublisherClientInterface, + PublisherClientInterface, ) -from .publisher_client import PublisherClient, AsyncPublisherClient +from .subscriber_client import AsyncSubscriberClient, SubscriberClient from .subscriber_client_interface import ( - SubscriberClientInterface, AsyncSubscriberClientInterface, + SubscriberClientInterface, +) + +__all__ = ( + "AsyncPublisherClient", + "AsyncPublisherClientInterface", + "AsyncSubscriberClient", + "AsyncSubscriberClientInterface", + "MessageTransformer", + "NackHandler", + "PublisherClient", + "PublisherClientInterface", + "SubscriberClient", + "SubscriberClientInterface", ) -from .subscriber_client import SubscriberClient, AsyncSubscriberClient diff --git a/google/cloud/pubsublite/types/__init__.py b/google/cloud/pubsublite/types/__init__.py index 146755d0..d6e3e4ec 100644 --- a/google/cloud/pubsublite/types/__init__.py +++ b/google/cloud/pubsublite/types/__init__.py @@ -18,3 +18,14 @@ from .paths import LocationPath, TopicPath, SubscriptionPath from .publish_metadata import PublishMetadata from .flow_control_settings import FlowControlSettings, DISABLED_FLOW_CONTROL + +__all__ = ( + "CloudRegion", + "CloudZone", + "FlowControlSettings", + "LocationPath", + "Partition", + "PublishMetadata", + "SubscriptionPath", + "TopicPath", +) diff --git a/samples/snippets/create_lite_subscription_example.py b/samples/snippets/create_lite_subscription_example.py index ddb13212..c86459e8 100644 --- a/samples/snippets/create_lite_subscription_example.py +++ b/samples/snippets/create_lite_subscription_example.py @@ -27,14 +27,13 @@ def create_lite_subscription( ): # [START pubsublite_create_subscription] from google.api_core.exceptions import AlreadyExists - from google.cloud.pubsublite import AdminClient + from google.cloud.pubsublite import AdminClient, Subscription from google.cloud.pubsublite.types import ( CloudRegion, CloudZone, SubscriptionPath, TopicPath, ) - from google.cloud.pubsublite_v1 import Subscription # TODO(developer): # project_number = 1122334455 @@ -43,12 +42,10 @@ def create_lite_subscription( # topic_id = "your-topic-id" # subscription_id = "your-subscription-id" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) subscription_path = SubscriptionPath(project_number, location, subscription_id) - subscription = Subscription( name=str(subscription_path), topic=str(topic_path), @@ -63,6 +60,7 @@ def create_lite_subscription( ), ) + client = AdminClient(cloud_region) try: response = client.create_subscription(subscription) print(f"{response.name} created successfully.") diff --git a/samples/snippets/create_lite_topic_example.py b/samples/snippets/create_lite_topic_example.py index f550ea79..34e403c6 100644 --- a/samples/snippets/create_lite_topic_example.py +++ b/samples/snippets/create_lite_topic_example.py @@ -25,9 +25,8 @@ def create_lite_topic(project_number, cloud_region, zone_id, topic_id, num_partitions): # [START pubsublite_create_topic] from google.api_core.exceptions import AlreadyExists - from google.cloud.pubsublite import AdminClient + from google.cloud.pubsublite import AdminClient, Topic from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath - from google.cloud.pubsublite_v1 import Topic from google.protobuf.duration_pb2 import Duration # TODO(developer): @@ -37,9 +36,8 @@ def create_lite_topic(project_number, cloud_region, zone_id, topic_id, num_parti # topic_id = "your-topic-id" # num_partitions = 1 - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) topic = Topic( name=str(topic_path), @@ -65,6 +63,7 @@ def create_lite_topic(project_number, cloud_region, zone_id, topic_id, num_parti ), ) + client = AdminClient(cloud_region) try: response = client.create_topic(topic) print(f"{response.name} created successfully.") diff --git a/samples/snippets/delete_lite_subscription_example.py b/samples/snippets/delete_lite_subscription_example.py index 6e4ba52e..4583c391 100644 --- a/samples/snippets/delete_lite_subscription_example.py +++ b/samples/snippets/delete_lite_subscription_example.py @@ -34,11 +34,11 @@ def delete_lite_subscription(project_number, cloud_region, zone_id, subscription # zone_id = "a" # subscription_id = "your-subscription-id" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) subscription_path = SubscriptionPath(project_number, location, subscription_id) + client = AdminClient(cloud_region) try: client.delete_subscription(subscription_path) print(f"{subscription_path} deleted successfully.") diff --git a/samples/snippets/delete_lite_topic_example.py b/samples/snippets/delete_lite_topic_example.py index 85cffc3f..12274c68 100644 --- a/samples/snippets/delete_lite_topic_example.py +++ b/samples/snippets/delete_lite_topic_example.py @@ -34,11 +34,11 @@ def delete_lite_topic(project_number, cloud_region, zone_id, topic_id): # zone_id = "a" # topic_id = "your-topic-id" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) + client = AdminClient(cloud_region) try: client.delete_topic(topic_path) print(f"{topic_path} deleted successfully.") diff --git a/samples/snippets/get_lite_subscription_example.py b/samples/snippets/get_lite_subscription_example.py index 9deac709..19124b4a 100644 --- a/samples/snippets/get_lite_subscription_example.py +++ b/samples/snippets/get_lite_subscription_example.py @@ -34,11 +34,11 @@ def get_lite_subscription(project_number, cloud_region, zone_id, subscription_id # zone_id = "a" # subscription_id = "your-subscription-id" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) subscription_path = SubscriptionPath(project_number, location, subscription_id) + client = AdminClient(cloud_region) try: response = client.get_subscription(subscription_path) print(f"{response.name} exists.") diff --git a/samples/snippets/get_lite_topic_example.py b/samples/snippets/get_lite_topic_example.py index ecebceb5..67c29a39 100644 --- a/samples/snippets/get_lite_topic_example.py +++ b/samples/snippets/get_lite_topic_example.py @@ -34,11 +34,11 @@ def get_lite_topic(project_number, cloud_region, zone_id, topic_id): # zone_id = "a" # topic_id = "your-topic-id" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) + client = AdminClient(cloud_region) try: response = client.get_topic(topic_path) num_partitions = client.get_topic_partition_count(topic_path) diff --git a/samples/snippets/list_lite_subscriptions_in_project_example.py b/samples/snippets/list_lite_subscriptions_in_project_example.py index 93d7bb22..b4245081 100644 --- a/samples/snippets/list_lite_subscriptions_in_project_example.py +++ b/samples/snippets/list_lite_subscriptions_in_project_example.py @@ -32,12 +32,11 @@ def list_lite_subscriptions_in_project(project_number, cloud_region, zone_id): # cloud_region = "us-central1" # zone_id = "a" - client = AdminClient(cloud_region) - - location_path = LocationPath( - project_number, CloudZone(CloudRegion(cloud_region), zone_id) - ) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) + location_path = LocationPath(project_number, location) + client = AdminClient(cloud_region) response = client.list_subscriptions(location_path) for subscription in response: diff --git a/samples/snippets/list_lite_subscriptions_in_topic_example.py b/samples/snippets/list_lite_subscriptions_in_topic_example.py index e83c40e0..ab32b6ce 100644 --- a/samples/snippets/list_lite_subscriptions_in_topic_example.py +++ b/samples/snippets/list_lite_subscriptions_in_topic_example.py @@ -33,11 +33,11 @@ def list_lite_subscriptions_in_topic(project_number, cloud_region, zone_id, topi # zone_id = "a" # topic_id = "your-topic-id" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) + client = AdminClient(cloud_region) response = client.list_topic_subscriptions(topic_path) for subscription_path in response: diff --git a/samples/snippets/list_lite_topics_example.py b/samples/snippets/list_lite_topics_example.py index c58500e0..df272e83 100644 --- a/samples/snippets/list_lite_topics_example.py +++ b/samples/snippets/list_lite_topics_example.py @@ -32,11 +32,11 @@ def list_lite_topics(project_number, cloud_region, zone_id): # cloud_region = "us-central1" # zone_id = "a" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) location_path = LocationPath(project_number, location) + client = AdminClient(cloud_region) response = client.list_topics(location_path) for topic in response: diff --git a/samples/snippets/subscriber_example.py b/samples/snippets/subscriber_example.py index a50c0f72..b4e32ee5 100644 --- a/samples/snippets/subscriber_example.py +++ b/samples/snippets/subscriber_example.py @@ -59,7 +59,9 @@ def callback(message): print(f"Received {message_data} of ordering key {message.ordering_key}.") message.ack() + # SubscriberClient() must be used in a `with` block or have __enter__() called before use. with SubscriberClient() as subscriber_client: + streaming_pull_future = subscriber_client.subscribe( subscription_path, callback=callback, diff --git a/samples/snippets/update_lite_subscription_example.py b/samples/snippets/update_lite_subscription_example.py index 7750f902..31ad79d5 100644 --- a/samples/snippets/update_lite_subscription_example.py +++ b/samples/snippets/update_lite_subscription_example.py @@ -25,9 +25,8 @@ def update_lite_subscription(project_number, cloud_region, zone_id, subscription_id): # [START pubsublite_update_subscription] from google.api_core.exceptions import NotFound - from google.cloud.pubsublite import AdminClient + from google.cloud.pubsublite import AdminClient, Subscription from google.cloud.pubsublite.types import CloudRegion, CloudZone, SubscriptionPath - from google.cloud.pubsublite_v1 import Subscription from google.protobuf.field_mask_pb2 import FieldMask # TODO(developer): @@ -37,9 +36,8 @@ def update_lite_subscription(project_number, cloud_region, zone_id, subscription # topic_id = "your-topic-id" # subscription_id = "your-subscription-id" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) subscription_path = SubscriptionPath(project_number, location, subscription_id) field_mask = FieldMask(paths=["delivery_config.delivery_requirement"]) @@ -55,6 +53,7 @@ def update_lite_subscription(project_number, cloud_region, zone_id, subscription ), ) + client = AdminClient(cloud_region) try: response = client.update_subscription(subscription, field_mask) print(f"{response.name} updated successfully.") diff --git a/samples/snippets/update_lite_topic_example.py b/samples/snippets/update_lite_topic_example.py index 01b55dbe..90097151 100644 --- a/samples/snippets/update_lite_topic_example.py +++ b/samples/snippets/update_lite_topic_example.py @@ -25,9 +25,8 @@ def update_lite_topic(project_number, cloud_region, zone_id, topic_id): # [START pubsublite_update_topic] from google.api_core.exceptions import NotFound - from google.cloud.pubsublite import AdminClient + from google.cloud.pubsublite import AdminClient, Topic from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath - from google.cloud.pubsublite_v1 import Topic from google.protobuf.duration_pb2 import Duration from google.protobuf.field_mask_pb2 import FieldMask @@ -37,9 +36,8 @@ def update_lite_topic(project_number, cloud_region, zone_id, topic_id): # zone_id = "a" # topic_id = "your-topic-id" - client = AdminClient(cloud_region) - - location = CloudZone(CloudRegion(cloud_region), zone_id) + cloud_region = CloudRegion(cloud_region) + location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) # Defines which topic fields to update. @@ -72,6 +70,7 @@ def update_lite_topic(project_number, cloud_region, zone_id, topic_id): ), ) + client = AdminClient(cloud_region) try: response = client.update_topic(topic, field_mask) print(f"{response.name} updated successfully.")