-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: create documentation navigation structure and some examples (#563)
* docs: create documentation navigation structure and some examples * test: refactor CLI tests without chdir * test: test custom naming option * refactor: use only tests directory to store tests * test: mv all tests to tests dir * Update mkdocs.yml
- Loading branch information
Showing
166 changed files
with
1,310 additions
and
340 deletions.
There are no files selected for viewing
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
# Features |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
# First Steps |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
41 changes: 41 additions & 0 deletions
41
docs/docs_src/getting_started/context/custom_context_kafka.py
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from faststream import Context, FastStream, apply_types | ||
from faststream.kafka import KafkaBroker | ||
from faststream.kafka.annotations import ContextRepo, KafkaMessage | ||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.subscriber("test-topic") | ||
async def handle( | ||
msg: str, | ||
message: KafkaMessage, | ||
context: ContextRepo, | ||
secret=Context(), | ||
): | ||
with context.scope("correlation_id", message.correlation_id): | ||
call_a(secret) | ||
|
||
|
||
@apply_types | ||
def call_a(s, secret=Context()): # get from call # get from global context | ||
assert s == secret | ||
call_b() | ||
|
||
|
||
@apply_types | ||
def call_b( | ||
message: KafkaMessage, # get from local context | ||
correlation_id=Context(), # get from local context | ||
): | ||
assert correlation_id == message.correlation_id | ||
|
||
|
||
@app.on_startup | ||
async def set_global(context: ContextRepo): | ||
context.set_global("secret", "my-perfect-secret") | ||
|
||
|
||
@app.after_startup | ||
async def test(): | ||
await broker.publish("Hi!", "test-topic") |
50 changes: 50 additions & 0 deletions
50
docs/docs_src/getting_started/context/existed_context_kafka.py
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from faststream import Context, FastStream | ||
from faststream.kafka import KafkaBroker | ||
from faststream.kafka.annotations import ( | ||
ContextRepo, | ||
KafkaMessage, | ||
Logger, | ||
) | ||
from faststream.kafka.annotations import ( | ||
KafkaBroker as BrokerAnnotation, | ||
) | ||
|
||
broker_object = KafkaBroker("localhost:9092") | ||
app = FastStream(broker_object) | ||
|
||
|
||
@broker_object.subscriber("test-topic") | ||
async def handle( | ||
msg: str, | ||
logger=Context(), | ||
message=Context(), | ||
broker=Context(), | ||
context=Context(), | ||
): | ||
logger.info(msg) | ||
context.set_global("correlation_id", message.correlation_id) | ||
|
||
await broker.publish( | ||
"Hi!", | ||
topic="response-topic", | ||
correlation_id=message.correlation_id, | ||
) | ||
|
||
|
||
@broker_object.subscriber("response-topic") | ||
async def handle_response( | ||
msg: str, | ||
logger: Logger, | ||
message: KafkaMessage, | ||
context: ContextRepo, | ||
correlation_id=Context(), | ||
): | ||
logger.info(msg) | ||
|
||
assert msg == "Hi!" | ||
assert correlation_id == message.correlation_id == context.get("correlation_id") | ||
|
||
|
||
@app.after_startup | ||
async def test(broker: BrokerAnnotation): | ||
await broker.publish("Hi!", "test-topic") |
23 changes: 23 additions & 0 deletions
23
docs/docs_src/getting_started/context/extra_arguments_kafka.py
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from faststream import Context, FastStream | ||
from faststream.kafka import KafkaBroker | ||
from faststream.kafka.annotations import ContextRepo | ||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.subscriber("test-topic") | ||
async def handle( | ||
secret: int = Context("secret_int"), | ||
casted_secret: int = Context("secret_int", cast=True), | ||
not_existed: None = Context("not_existed", default=None), | ||
): | ||
assert secret == "1" | ||
assert casted_secret == 1 | ||
assert not_existed is None | ||
|
||
|
||
@app.after_startup | ||
async def test(context: ContextRepo): | ||
context.set_global("secret_int", "1") | ||
await broker.publish("", "test-topic") |
19 changes: 19 additions & 0 deletions
19
docs/docs_src/getting_started/context/fields_access_kafka.py
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from faststream import Context, FastStream | ||
from faststream.kafka import KafkaBroker | ||
from faststream.kafka.annotations import KafkaMessage | ||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.subscriber("test-topic") | ||
async def handle( | ||
message: KafkaMessage, | ||
correlation_id: str = Context("message.correlation_id"), | ||
): | ||
assert message.correlation_id == correlation_id | ||
|
||
|
||
@app.after_startup | ||
async def test(): | ||
await broker.publish("", "test-topic") |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from faststream import Depends, FastStream | ||
from faststream.kafka import KafkaBroker | ||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
async def get_user_from_db(name: str, user_id: int): | ||
"""Emulate DB request""" | ||
return { | ||
"name": name, | ||
"user_id": user_id, | ||
} | ||
|
||
|
||
@broker.subscriber("test-topic") | ||
async def handle(user=Depends(get_user_from_db)): | ||
assert user == { | ||
"name": "john", | ||
"user_id": 1, | ||
} | ||
|
||
|
||
@app.after_startup | ||
async def test(): | ||
await broker.publish({"name": "john", "user_id": 1}, topic="test-topic") |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from faststream import Depends, FastStream | ||
from faststream.kafka import KafkaBroker | ||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
class UserSerialized: | ||
def __init__(self, name: str, user_id: int): | ||
self.name = name | ||
self.user_id = user_id | ||
|
||
|
||
@broker.subscriber("test-topic") | ||
async def handle(user=Depends(UserSerialized)): | ||
assert user.name == "john" | ||
assert user.user_id == 1 | ||
|
||
|
||
@app.after_startup | ||
async def test(): | ||
await broker.publish({"name": "john", "user_id": 1}, topic="test-topic") |
27 changes: 27 additions & 0 deletions
27
docs/docs_src/getting_started/dependencies/global_broker_kafka.py
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from faststream import Depends, FastStream | ||
from faststream.kafka import KafkaBroker | ||
|
||
|
||
async def validate_user(name: str, user_id: int): | ||
"""Emulate DB request""" | ||
user = { | ||
"name": "john", | ||
"user_id": user_id, | ||
} | ||
|
||
if user["name"] != name: | ||
raise ValueError() | ||
|
||
|
||
broker = KafkaBroker("localhost:9092", dependencies=(Depends(validate_user),)) | ||
app = FastStream(broker) | ||
|
||
|
||
@broker.subscriber("test-topic") | ||
async def handle(name: str): | ||
assert name == "john" | ||
|
||
|
||
@app.after_startup | ||
async def test(): | ||
await broker.publish({"name": "john", "user_id": 1}, topic="test-topic") |
26 changes: 26 additions & 0 deletions
26
docs/docs_src/getting_started/dependencies/global_kafka.py
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from faststream import Depends, FastStream | ||
from faststream.kafka import KafkaBroker | ||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
async def validate_user(name: str, user_id: int): | ||
"""Emulate DB request""" | ||
user = { | ||
"name": "john", | ||
"user_id": user_id, | ||
} | ||
|
||
if user["name"] != name: | ||
raise ValueError() | ||
|
||
|
||
@broker.subscriber("test-topic", dependencies=(Depends(validate_user),)) | ||
async def handle(name: str): | ||
assert name == "john" | ||
|
||
|
||
@app.after_startup | ||
async def test(): | ||
await broker.publish({"name": "john", "user_id": 1}, topic="test-topic") |
30 changes: 30 additions & 0 deletions
30
docs/docs_src/getting_started/dependencies/sub_dep_kafka.py
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from faststream import Depends, FastStream | ||
from faststream.kafka import KafkaBroker | ||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
async def get_user_from_db(user_id: int): | ||
"""Emulate DB request""" | ||
return { | ||
"name": "john", | ||
"user_id": user_id, | ||
} | ||
|
||
|
||
async def validate_db_user( | ||
name: str, | ||
user=Depends(get_user_from_db), | ||
): | ||
return user["name"] == name | ||
|
||
|
||
@broker.subscriber("test-topic") | ||
async def handle(is_user_valid=Depends(validate_db_user)): | ||
assert is_user_valid | ||
|
||
|
||
@app.after_startup | ||
async def test(): | ||
await broker.publish({"name": "john", "user_id": 1}, topic="test-topic") |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from contextlib import contextmanager | ||
|
||
from faststream import Depends, FastStream | ||
from faststream.kafka import KafkaBroker | ||
|
||
broker = KafkaBroker("localhost:9092") | ||
app = FastStream(broker) | ||
|
||
|
||
@contextmanager | ||
def fake_session_builder(): | ||
yield "session" | ||
|
||
|
||
async def db_session(): | ||
with fake_session_builder() as session: | ||
yield session | ||
|
||
|
||
@broker.subscriber("test-topic") | ||
async def handle(db_session=Depends(db_session)): | ||
assert db_session == "session" | ||
|
||
|
||
@app.after_startup | ||
async def test(): | ||
await broker.publish("", topic="test-topic") |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from faststream import ContextRepo, FastStream | ||
from faststream.kafka import KafkaBroker | ||
from faststream.kafka.annotations import KafkaBroker as BrokerAnnotation | ||
|
||
app = FastStream(KafkaBroker("localhost:9092")) | ||
|
||
|
||
@app.on_startup | ||
def cli_run( # sync or async function | ||
context: ContextRepo, # get from global context | ||
env: str = ".env", # get from CLI option `--env=...` | ||
): | ||
context.set_global("use_env", env) | ||
|
||
|
||
@app.after_startup | ||
async def broker_available( | ||
broker: BrokerAnnotation, # get from global context | ||
): | ||
await broker.publish("Service started", topic="logs") | ||
|
||
|
||
@app.on_shutdown | ||
async def broker_still_available( | ||
broker: BrokerAnnotation, # get from global context | ||
): | ||
await broker.publish("Service stopped", topic="logs") | ||
|
||
|
||
@app.after_shutdown | ||
async def broker_stopped( | ||
broker: BrokerAnnotation, # get from global context | ||
): | ||
assert broker._connection is None |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pytest | ||
|
||
from faststream.kafka import TestKafkaBroker | ||
|
||
from .all_hooks_kafka import app | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_lifespan(): | ||
async with TestKafkaBroker(app.broker): | ||
|
||
async def TestApp(app): | ||
# test smth | ||
pass |
Empty file.
Oops, something went wrong.