Skip to content

Commit 7fbf8f8

Browse files
authored
Merge pull request #68 from matheusvnm/dev
feat: PubSubTestClient and example docs
2 parents 351d5aa + 8e0c1f7 commit 7fbf8f8

36 files changed

+2585
-253
lines changed

AGENTS.md

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

examples/basic_usage/e1_01_basic_subscriber.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
"""Title: Basic Subscriber Setup
2+
3+
Demonstrates the simplest way to create a Pub/Sub subscriber with FastPubSub.
4+
5+
This example shows:
6+
- Creating a PubSubBroker with a project ID
7+
- Defining a subscriber using the @broker.subscriber decorator
8+
- Handling incoming messages with an async handler function
9+
- Publishing a test message on application startup
10+
11+
The subscriber listens to 'subscriber-topic' and logs each received message.
12+
13+
Run with:
14+
fastpubsub run examples.basic_usage.e1_01_basic_subscriber:app
15+
16+
Requirements:
17+
- Set PUBSUB_EMULATOR_HOST for local testing, or
18+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
19+
"""
20+
121
from fastpubsub import FastPubSub, Message, PubSubBroker
222
from fastpubsub.logger import logger
323

examples/basic_usage/e1_02_multiple_subscribers.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
"""Title: Multiple Subscribers on Different Topics
2+
3+
Demonstrates how to create multiple subscribers within a single FastPubSub application.
4+
5+
This example shows:
6+
- Creating multiple subscribers with different aliases
7+
- Subscribing to the same topic with different subscription names
8+
- Subscribing to different topics
9+
- How messages are routed to the appropriate handlers
10+
11+
The example creates three subscribers:
12+
1. 'first-alias' - listens to 'first-topic'
13+
2. 'second-alias' - also listens to 'first-topic' (different subscription)
14+
3. 'third-alias' - listens to 'second-topic'
15+
16+
Run with:
17+
fastpubsub run examples.basic_usage.e1_02_multiple_subscribers:app
18+
19+
Requirements:
20+
- Set PUBSUB_EMULATOR_HOST for local testing, or
21+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
22+
"""
23+
124
from fastpubsub import FastPubSub, Message, PubSubBroker
225
from fastpubsub.logger import logger
326

examples/basic_usage/e1_03_cross_project_subscribers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
"""Title: Cross-Project Subscription
2+
3+
Demonstrates how to subscribe to topics in different Google Cloud projects.
4+
5+
This example shows:
6+
- Creating subscribers that listen to topics in different GCP projects
7+
- Using the project_id parameter on @broker.subscriber to override the default project
8+
- Publishing messages to topics in different projects
9+
10+
The example creates two subscribers:
11+
1. One listening to the default project's topic
12+
2. Another listening to the same topic name but in an alternative project
13+
14+
Run with:
15+
fastpubsub run examples.basic_usage.e1_03_cross_project_subscribers:app
16+
17+
Requirements:
18+
- Set PUBSUB_EMULATOR_HOST for local testing, or
19+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
20+
- Ensure both projects exist and are accessible (Only for GCP on Cloud)
21+
"""
22+
123
from fastpubsub import FastPubSub, Message, PubSubBroker
224
from fastpubsub.logger import logger
325

examples/basic_usage/e2_01_basic_publisher.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
"""Title: Basic Message Publishing
2+
3+
Demonstrates the simplest way to publish messages using broker.publish().
4+
5+
This example shows:
6+
- Publishing messages directly via broker.publish()
7+
- Passing the topic name and data as arguments
8+
- Using the after_startup hook to publish a test message
9+
10+
The example publishes a dictionary message to 'test-topic' which is then
11+
received by the subscriber handler.
12+
13+
Run with:
14+
fastpubsub run examples.basic_usage.e2_01_basic_publisher:app
15+
16+
Requirements:
17+
- Set PUBSUB_EMULATOR_HOST for local testing, or
18+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
19+
"""
20+
121
from fastpubsub import FastPubSub, Message, PubSubBroker
222
from fastpubsub.logger import logger
323

examples/basic_usage/e2_02_basic_publisher.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
"""Title: Publisher Instance API
2+
3+
Demonstrates creating a reusable Publisher instance bound to a specific topic.
4+
5+
This example shows:
6+
- Creating a Publisher instance with broker.publisher()
7+
- Binding the publisher to a specific topic at creation time
8+
- Publishing messages without specifying the topic each time
9+
10+
Using a Publisher instance is useful when you need to publish multiple messages
11+
to the same topic, as it avoids repeating the topic name.
12+
13+
Run with:
14+
fastpubsub run examples.basic_usage.e2_02_basic_publisher:app
15+
16+
Requirements:
17+
- Set PUBSUB_EMULATOR_HOST for local testing, or
18+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
19+
"""
20+
121
from fastpubsub import FastPubSub, Message, Publisher, PubSubBroker
222
from fastpubsub.logger import logger
323

examples/basic_usage/e2_03_basic_publisher.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
"""Title: Publisher in Startup Hook
2+
3+
Demonstrates creating a Publisher instance inside the startup hook.
4+
5+
This example shows:
6+
- Deferring Publisher instantiation until application startup
7+
- Creating a Publisher within the after_startup hook
8+
- Publishing messages immediately after the publisher is created
9+
10+
This pattern is useful when you need the application to be fully initialized
11+
before creating publishers, or when publisher configuration depends on
12+
runtime state.
13+
14+
Run with:
15+
fastpubsub run examples.basic_usage.e2_03_basic_publisher:app
16+
17+
Requirements:
18+
- Set PUBSUB_EMULATOR_HOST for local testing, or
19+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
20+
"""
21+
122
from fastpubsub import FastPubSub, Message, Publisher, PubSubBroker
223
from fastpubsub.logger import logger
324

examples/basic_usage/e2_04_cross_project_publisher.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
"""Title: Cross-Project Publishing
2+
3+
Demonstrates publishing messages across multiple Google Cloud projects.
4+
5+
This example shows:
6+
- Creating publishers and subscribers across three different GCP projects
7+
- Using project_id parameter on broker.publisher() to target different projects
8+
- Message flow across projects: first -> second -> third
9+
- Chaining message processing across project boundaries
10+
11+
The flow:
12+
1. Message published to first project triggers subscriber
13+
2. First subscriber publishes to second project
14+
3. Second subscriber publishes to third project using a Publisher instance
15+
16+
Run with:
17+
fastpubsub run examples.basic_usage.e2_04_cross_project_publisher:app
18+
19+
Requirements:
20+
- Set PUBSUB_EMULATOR_HOST for local testing, or
21+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
22+
- Ensure all three projects exist and are accessible
23+
"""
24+
125
from fastpubsub import FastPubSub, Message, PubSubBroker
226
from fastpubsub.logger import logger
327
from fastpubsub.pubsub import Publisher

examples/basic_usage/e2_04_message_formats.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
"""Title: Different Message Format Support
2+
3+
Demonstrates publishing messages in various formats supported by FastPubSub.
4+
5+
This example shows:
6+
- Publishing Pydantic models (automatically serialized to JSON)
7+
- Publishing dictionaries (serialized to JSON)
8+
- Publishing strings (encoded to bytes)
9+
- Publishing raw bytes
10+
11+
FastPubSub automatically handles serialization for different data types,
12+
making it easy to work with structured data using Pydantic models while
13+
also supporting simple string and bytes payloads.
14+
15+
Run with:
16+
fastpubsub run examples.basic_usage.e2_04_message_formats:app
17+
18+
Requirements:
19+
- Set PUBSUB_EMULATOR_HOST for local testing, or
20+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
21+
"""
22+
123
from pydantic import BaseModel
224

325
from fastpubsub import FastPubSub, Message, Publisher, PubSubBroker

examples/basic_usage/e3_01_linked_pubsub.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
"""Title: Message Chaining Between Topics
2+
3+
Demonstrates how a subscriber can publish messages to another topic.
4+
5+
This example shows:
6+
- Chaining message processing between topics
7+
- A subscriber that receives a message and publishes to a different topic
8+
- Creating pipelines where one message triggers subsequent messages
9+
10+
The flow:
11+
1. Initial message is published to 'first-topic'
12+
2. First subscriber processes it and publishes to 'second-topic'
13+
3. Second subscriber receives the chained message
14+
15+
This pattern is useful for event-driven architectures and workflow processing.
16+
17+
Run with:
18+
fastpubsub run examples.basic_usage.e3_01_linked_pubsub:app
19+
20+
Requirements:
21+
- Set PUBSUB_EMULATOR_HOST for local testing, or
22+
- Set GOOGLE_APPLICATION_CREDENTIALS for GCP
23+
"""
24+
125
from fastpubsub import FastPubSub, Message, PubSubBroker
226
from fastpubsub.logger import logger
327

0 commit comments

Comments
 (0)