-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: update nats kv and os documentation
- Loading branch information
Showing
7 changed files
with
86 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,16 @@ | ||
from nats.js.kv import KeyValue as KV | ||
from typing_extensions import Annotated | ||
|
||
from faststream import Logger | ||
from faststream import Context, FastStream, Logger | ||
from faststream import FastStream | ||
from faststream.nats import NatsBroker | ||
from faststream.nats.annotations import ContextRepo | ||
|
||
KeyValue = Annotated[KV, Context("kv")] | ||
|
||
broker = NatsBroker() | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.subscriber("subject") | ||
async def handler(msg: str, kv: KeyValue, logger: Logger): | ||
logger.info(msg) | ||
kv_data = await kv.get("key") | ||
assert kv_data.value == b"Hello!" | ||
|
||
|
||
@app.on_startup | ||
async def setup_broker(context: ContextRepo): | ||
await broker.connect() | ||
|
||
kv = await broker.stream.create_key_value(bucket="bucket") | ||
context.set_global("kv", kv) | ||
@broker.subscriber("key", kv_watch="bucket") | ||
async def handler(msg: str): | ||
assert msg == "Hello!" | ||
|
||
|
||
@app.after_startup | ||
async def test_send(kv: KeyValue): | ||
await kv.put("key", b"Hello!") | ||
await broker.publish("Hi!", "subject") | ||
async def setup_broker(): | ||
key_value = await broker.key_value(bucket="bucket") | ||
await key_value.put("key", b"Hello!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,26 @@ | ||
from io import BytesIO | ||
|
||
from nats.js.object_store import ObjectStore as OS | ||
from typing_extensions import Annotated | ||
|
||
from faststream import FastStream | ||
from faststream import Logger | ||
from faststream import Context, FastStream | ||
from faststream.nats import NatsBroker | ||
from faststream.nats.annotations import ContextRepo | ||
|
||
ObjectStorage = Annotated[OS, Context("OS")] | ||
from faststream.nats.annotations import ObjectStorage | ||
|
||
broker = NatsBroker() | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.subscriber("subject") | ||
async def handler(msg: str, os: ObjectStorage, logger: Logger): | ||
logger.info(msg) | ||
obj = await os.get("file") | ||
assert obj.data == b"File mock" | ||
|
||
|
||
@app.on_startup | ||
async def setup_broker(context: ContextRepo): | ||
await broker.connect() | ||
|
||
os = await broker.stream.create_object_store("bucket") | ||
context.set_global("OS", os) | ||
@broker.subscriber("example-bucket", obj_watch=True) | ||
async def handler( | ||
filename: str, | ||
storage: ObjectStorage, | ||
logger: Logger, | ||
): | ||
assert filename == "file.txt" | ||
file = await storage.get(filename) | ||
logger.info(file.data) | ||
|
||
|
||
@app.after_startup | ||
async def test_send(os: ObjectStorage): | ||
await os.put("file", BytesIO(b"File mock")) | ||
await broker.publish("Hi!", "subject") | ||
async def test_send(): | ||
object_storage = await broker.object_storage("example-bucket") | ||
await object_storage.put("file.txt", BytesIO(b"File mock")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters