Skip to content

Commit fda26bc

Browse files
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
1 parent eb8d63a commit fda26bc

27 files changed

+387
-80
lines changed

docs/admin/api/client.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Admin Client API
2+
================
3+
4+
.. automodule:: google.cloud.pubsublite.admin_client
5+
:members:
6+
:inherited-members:

docs/admin/index.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Admin Operations
2+
================
3+
4+
Admin operations are handled through the
5+
:class:`~.pubsublite.admin_client.AdminClient` class (aliased as
6+
``google.cloud.pubsublite.AdminClient``).
7+
8+
Instantiating an admin client requires you to provide a valid cloud region
9+
for the Pub/Sub Lite service:
10+
11+
.. code-block:: python
12+
13+
from google.cloud.pubsublite import AdminClient
14+
cloud_region = CloudRegion("us-central1")
15+
admin_client = AdminClient(cloud_region)
16+
17+
18+
Create a topic
19+
--------------
20+
21+
To create a message, use the
22+
:meth:`~.pubsublite.admin_client.AdminClient.create_topic` method. This method accepts
23+
one positional arguments: a :class:`~.pubsublite_v1.types.Topic` object, where the name
24+
of the topic is passed along as a string.
25+
26+
Pub/Sub Lite topics have the canonical form of
27+
28+
``projects/{project_number}/locations/{location}/topics/{topic_id}``
29+
30+
A location (a.k.a. `zone`_) is comprised of a cloud region and a zone ID.
31+
32+
.. _zone: https://cloud.google.com/pubsub/lite/docs/locations/
33+
34+
A call to create a Pub/Sub Lite topic looks like:
35+
36+
.. code-block:: python
37+
38+
from google.cloud.pubsublite import AdminClient, Topic
39+
from google.cloud.pubsublite.types import (
40+
CloudRegion, CloudZone, TopicPath,
41+
)
42+
43+
project_number = 1122334455
44+
zone_id = "a"
45+
topic_id = "your-topic-id"
46+
47+
cloud_region = CloudRegion(cloud_region)
48+
location = CloudZone(cloud_region, zone_id)
49+
topic_path = TopicPath(project_number, location, topic_id)
50+
51+
topic = Topic(
52+
name=str(topic_path),
53+
partition_config=Topic.PartitionConfig(
54+
# 1 partition
55+
count=1,
56+
# Publish at 4 MiB/s and subscribe at 8 MiB/s
57+
capacity=Topic.PartitionConfig.Capacity(
58+
publish_mib_per_sec=4,
59+
subscribe_mib_per_sec=8,
60+
),
61+
),
62+
retention_config=Topic.RetentionConfig(
63+
# 30 GiB
64+
per_partition_bytes=30 * 1024 * 1024 * 1024,
65+
# 7 days
66+
period=Duration(seconds=60 * 60 * 24 * 7),
67+
),
68+
)
69+
70+
admin_client = AdminClient(cloud_region)
71+
response = admin_client.create_topic(topic)
72+
73+
74+
API Reference
75+
-------------
76+
77+
.. toctree::
78+
:maxdepth: 2
79+
80+
api/client

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,10 @@
345345

346346
# Example configuration for intersphinx: refer to the Python standard library.
347347
intersphinx_mapping = {
348-
"python": ("http://python.readthedocs.org/en/latest/", None),
348+
"python": ("https://python.readthedocs.io/en/latest/", None),
349349
"google-auth": ("https://google-auth.readthedocs.io/en/stable", None),
350350
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,),
351-
"grpc": ("https://grpc.io/grpc/python/", None),
351+
"grpc": ("https://grpc.github.io/grpc/python/", None),
352352
"proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None),
353353
}
354354

docs/index.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
API Reference
66
-------------
77
.. toctree::
8-
:maxdepth: 2
8+
:maxdepth: 3
99

10-
pubsublite_v1/services
11-
pubsublite_v1/types
10+
Admin Client <admin/index>
11+
Publisher Client <publisher/index>
12+
Subscriber Client <subscriber/index>
13+
Types <types>
1214

1315
Changelog
1416
---------

docs/publisher/api/client.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Publisher Client API
2+
====================
3+
4+
.. automodule:: google.cloud.pubsublite.cloudpubsub.publisher_client
5+
:members:
6+
:inherited-members:

docs/publisher/index.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Publisher Client
2+
================
3+
4+
Publish operations are handled through the
5+
:class:`~.pubsublite.cloudpubsub.publisher_client.PublisherClient` class (aliased as
6+
``google.cloud.pubsublite.cloudpubsub.PublisherClient``).
7+
8+
You should instantiate a publisher client using a context manager:
9+
10+
.. code-block:: python
11+
12+
from google.cloud.pubsublite.cloudpubsub import PublisherClient
13+
14+
with PublisherClient() as publisher_client:
15+
# Use publisher_client
16+
17+
When not using a context manager, you need to call
18+
:meth:`~.pubsublite.cloudpubsub.publisher_client.PublisherClient.__enter__`.
19+
20+
Publish a message
21+
-----------------
22+
23+
To publish a message, use the
24+
:meth:`~.pubsublite.cloudpubsub.publisher_client.PublisherClient.publish`
25+
method. This method accepts
26+
two positional arguments: a :class:`~.pubsublite.types.TopicPath` object
27+
and a message in byte string.
28+
29+
A call to publish a message looks like:
30+
31+
.. code-block:: python
32+
33+
from google.cloud.pubsublite.cloudpubsub import PublisherClient
34+
from google.cloud.pubsublite.types import (
35+
CloudRegion, CloudZone, TopicPath,
36+
)
37+
38+
project_number = 1122334455
39+
cloud_region = "us-central1"
40+
zone_id = "a"
41+
topic_id = "your-topic-id"
42+
43+
location = CloudZone(CloudRegion(cloud_region), zone_id)
44+
topic_path = TopicPath(project_number, location, topic_id)
45+
46+
with PublisherClient() as publisher_client:
47+
data = "Hello world!"
48+
api_future = publisher_client.publish(t
49+
opic_path, data.encode("utf-8")
50+
)
51+
message_id = api_future.result()
52+
53+
54+
API Reference
55+
-------------
56+
57+
.. toctree::
58+
:maxdepth: 2
59+
60+
api/client

docs/pubsublite_v1/services.rst

Lines changed: 0 additions & 21 deletions
This file was deleted.

docs/pubsublite_v1/types.rst

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/subscriber/api/client.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Subscriber Client API
2+
=====================
3+
4+
.. automodule:: google.cloud.pubsublite.cloudpubsub.subscriber_client
5+
:members:
6+
:inherited-members:

docs/subscriber/index.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Subscriber Client
2+
=================
3+
4+
Subscribe operations are handled through the
5+
:class:`~.pubsublite.cloudpubsub.subscriber_client.SubscriberClient` class (aliased as
6+
``google.cloud.pubsublite.cloudpubsub.SubscriberClient``).
7+
8+
You should instantiate a subscriber client using a context manager:
9+
10+
.. code-block:: python
11+
12+
from google.cloud.pubsublite.cloudpubsub import SubscriberClient
13+
14+
with SubscriberClient() as subscriber_client:
15+
# Use subscriber_client
16+
17+
When not using a context manager, you need to call
18+
:meth:`~.pubsublite.cloudpubsub.subscriber_client.SubscriberClient.__enter__`.
19+
20+
Receive messages
21+
----------------
22+
23+
To receive messages, use the
24+
:meth:`~.pubsublite.cloudpubsub.subscriber_client.SubscriberClient.subscribe`
25+
method. This method requires
26+
three positional arguments: a :class:`~.pubsublite.types.SubscriptionPath` object,
27+
a callback function, and a :class:`~.pubsublite.types.FlowControlSettings` object.
28+
29+
Receiving messages looks like:
30+
31+
.. code-block:: python
32+
33+
from google.cloud.pubsublite.cloudpubsub import SubscriberClient
34+
from google.cloud.pubsublite.types import (
35+
CloudRegion,
36+
CloudZone,
37+
SubscriptionPath,
38+
DISABLED_FLOW_CONTROL,
39+
)
40+
41+
project_number = 1122334455
42+
cloud_region = "us-central1"
43+
zone_id = "a"
44+
subscription_id = "your-subscription-id"
45+
46+
location = CloudZone(CloudRegion(cloud_region), zone_id)
47+
subscription_path = SubscriptionPath(project_number, location, subscription_id)
48+
49+
with SubscriberClient() as subscriber_client:
50+
streaming_pull_future = subscriber_client.subscribe(
51+
subscription_path,
52+
callback=callback,
53+
per_partition_flow_control_settings=DISABLED_FLOW_CONTROL,
54+
)
55+
56+
streaming_pull_future.result()
57+
58+
Subscriber Callbacks
59+
--------------------
60+
61+
Received messages are processed in a callback function. This callback function
62+
only takes one argument of type :class:`google.cloud.pubsub_v1.subscriber.message.Message`.
63+
After this message has been processed, the function should either call
64+
:meth:`~.pubsub_v1.subscriber.message.Message.ack` to acknowledge the message or
65+
:meth:`~.pubsub_v1.subscriber.message.Message.nack` to send a negative acknowledgement.
66+
67+
.. code-block:: python
68+
69+
def callback(message):
70+
message_data = message.data.decode("utf-8")
71+
print(f"Received {message_data}.")
72+
message.ack()
73+
74+
Flow Control Settings
75+
---------------------
76+
77+
Flow control settings are applied per partition.
78+
They control when to pause the message stream to a partition so the server temporarily
79+
stops sending out more messages (known as outstanding messages) from this partition.
80+
81+
You can configure flow control settings by setting the maximum number and size of
82+
outstanding messages. The message stream is paused when either condition is met.
83+
84+
.. code-block:: python
85+
86+
from google.cloud.pubsublite.types import FlowControlSettings
87+
88+
flow_control_settings = FlowControlSettings(
89+
# 1,000 outstanding messages. Must be >0.
90+
messages_outstanding=1000,
91+
# 10 MiB. Must be greater than the allowed size of the largest message (1 MiB).
92+
bytes_outstanding=10 * 1024 * 1024,
93+
)
94+
95+
You may also turn off flow control settings by setting it to
96+
:class:`google.cloud.pubsublite.types.DISABLED_FLOW_CONTROL`.
97+
98+
API Reference
99+
-------------
100+
101+
.. toctree::
102+
:maxdepth: 2
103+
104+
api/client

0 commit comments

Comments
 (0)