Skip to content

Commit b9939f3

Browse files
committed
feat: adds pydantic serialization, dependency injection and custom encoding/decoding
1 parent 32882ab commit b9939f3

File tree

91 files changed

+4585
-677
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+4585
-677
lines changed

examples/basic_usage/e1_01_basic_subscriber.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
"""Example: Basic subscriber setup.
2+
3+
This example demonstrates the simplest way to create a message subscriber
4+
with FastPubSub. It shows how to:
5+
6+
1. Create a PubSubBroker with your Google Cloud project ID
7+
2. Create a FastPubSub application
8+
3. Define a subscriber handler using the @broker.subscriber decorator
9+
4. Access the raw Message object in your handler
10+
11+
Run with: fastpubsub run examples.basic_usage.e1_01_basic_subscriber:app
12+
"""
13+
14+
import asyncio
15+
116
from fastpubsub import FastPubSub, Message, PubSubBroker
217
from fastpubsub.logger import logger
318

@@ -12,8 +27,14 @@
1227
)
1328
async def process_message(message: Message) -> None:
1429
logger.info(f"Processed message: {message}")
30+
await asyncio.sleep(1)
1531

1632

1733
@app.after_startup
1834
async def test_publish() -> None:
19-
await broker.publish("subscriber-topic", {"message": "streaming a message"})
35+
sent = 0
36+
max_messages = 100
37+
while sent < max_messages:
38+
await broker.publish("subscriber-topic", {"message": "streaming a message"})
39+
sent += 1
40+
# await asyncio.sleep(0.1)

examples/basic_usage/e1_02_multiple_subscribers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""Example: Multiple subscribers on different topics.
2+
3+
This example shows how to define multiple subscribers within the same
4+
application, each listening to different topics or with different
5+
subscriptions. FastPubSub manages all subscribers concurrently.
6+
7+
Run with: fastpubsub run examples.basic_usage.e1_02_multiple_subscribers:app
8+
"""
9+
110
from fastpubsub import FastPubSub, Message, PubSubBroker
211
from fastpubsub.logger import logger
312

examples/basic_usage/e1_03_cross_project_subscribers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""Example: Cross-project subscribers.
2+
3+
This example demonstrates how to subscribe to topics in different
4+
Google Cloud projects. Each subscriber can specify its own project_id,
5+
overriding the broker's default project.
6+
7+
Run with: fastpubsub run examples.basic_usage.e1_03_cross_project_subscribers:app
8+
"""
9+
110
from fastpubsub import FastPubSub, Message, PubSubBroker
211
from fastpubsub.logger import logger
312

examples/basic_usage/e2_01_basic_publisher.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""Example: Basic message publishing.
2+
3+
This example shows how to publish messages to a Pub/Sub topic using
4+
the broker's publish method. Messages can be dictionaries, strings,
5+
or bytes.
6+
7+
Run with: fastpubsub run examples.basic_usage.e2_01_basic_publisher:app
8+
"""
9+
110
from fastpubsub import FastPubSub, Message, PubSubBroker
211
from fastpubsub.logger import logger
312

examples/basic_usage/e2_02_basic_publisher.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""Example: Publisher with explicit topic declaration.
2+
3+
This example demonstrates creating a Publisher object for a specific
4+
topic, which can be reused for multiple publishes. This is useful when
5+
you need to publish to the same topic multiple times.
6+
7+
Run with: fastpubsub run examples.basic_usage.e2_02_basic_publisher:app
8+
"""
9+
110
from fastpubsub import FastPubSub, Message, Publisher, PubSubBroker
211
from fastpubsub.logger import logger
312

examples/basic_usage/e2_03_basic_publisher.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
"""Example: Publisher with explicit topic declaration.
2+
3+
This example demonstrates creating a Publisher object for a specific
4+
topic, which can be reused for multiple publishes. Using an explicit
5+
Publisher object is useful when you need to publish multiple messages
6+
to the same topic.
7+
8+
Run with: fastpubsub run examples.basic_usage.e2_03_basic_publisher:app
9+
"""
10+
111
from fastpubsub import FastPubSub, Message, Publisher, PubSubBroker
212
from fastpubsub.logger import logger
313

examples/basic_usage/e2_04_cross_project_publisher.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
from fastpubsub import FastPubSub, Message, PubSubBroker
1+
"""Example: Cross-project publishing.
2+
3+
This example demonstrates publishing messages to topics in different
4+
Google Cloud projects. Each publisher can specify its own project_id,
5+
overriding the broker's default project. This enables communication
6+
across multiple GCP projects from a single application.
7+
8+
Flow:
9+
1. Message arrives at first project's topic
10+
2. Handler publishes to second project's topic
11+
3. Handler publishes to third project's topic via explicit Publisher
12+
13+
Run with: fastpubsub run examples.basic_usage.e2_04_cross_project_publisher:app
14+
"""
15+
16+
from fastpubsub import FastPubSub, Message, Publisher, PubSubBroker
217
from fastpubsub.logger import logger
3-
from fastpubsub.pubsub import Publisher
418

519
DEFAULT_PROJECT_ID = "fastpubsub-pubsub-first-project"
620
SECOND_PROJECT_ID = "fastpubsub-pubsub-second-project"

examples/basic_usage/e2_04_message_formats.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
"""Example: Different message formats.
2+
3+
This example shows the various data formats you can publish:
4+
- Pydantic models (automatically serialized via model_dump)
5+
- Dictionaries (JSON-serialized)
6+
- Strings (UTF-8 encoded)
7+
- Raw bytes (sent as-is)
8+
9+
FastPubSub's serialization system automatically handles encoding
10+
based on the data type provided.
11+
12+
Run with: fastpubsub run examples.basic_usage.e2_04_message_formats:app
13+
"""
14+
115
from pydantic import BaseModel
216

317
from fastpubsub import FastPubSub, Message, Publisher, PubSubBroker

examples/basic_usage/e3_01_linked_pubsub.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
"""Example: Linked pub/sub (chaining topics).
2+
3+
This example demonstrates a common pattern where a subscriber processes
4+
a message and then publishes to another topic, creating a processing
5+
pipeline. This is useful for building event-driven architectures.
6+
7+
Flow: first-topic -> handle() -> second-topic -> handle_from_another_topic()
8+
9+
Run with: fastpubsub run examples.basic_usage.e3_01_linked_pubsub:app
10+
"""
11+
112
from fastpubsub import FastPubSub, Message, PubSubBroker
213
from fastpubsub.logger import logger
314

examples/basic_usage/e4_01_basic_fastapi_integration.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
"""Example: FastAPI integration.
2+
3+
This example shows how to integrate FastPubSub with a FastAPI application,
4+
allowing you to have both HTTP endpoints and Pub/Sub subscribers in the
5+
same application. FastPubSub extends FastAPI, so you can use decorators
6+
like @app.post() alongside @broker.subscriber().
7+
8+
The HTTP endpoint receives a user message and publishes it to a topic,
9+
while the subscriber processes messages from that topic.
10+
11+
Run with: fastpubsub run examples.basic_usage.e4_01_basic_fastapi_integration:app
12+
"""
13+
114
from pydantic import BaseModel
215

316
from fastpubsub import FastPubSub, Message, Publisher, PubSubBroker

0 commit comments

Comments
 (0)