Skip to content

Commit

Permalink
fix: update FastAPI to 0.112.2 (#1736)
Browse files Browse the repository at this point in the history
* refactor: new FastAPI integration syntax

* lint: fix confluent topic mypy

* docs: generate API References

* docs: fix FastAPI integration section

* docs: make annotation private

* chore: update FastAPI

* lint: fix precommit

* chore: trigger CI

* chore: revert CI triggers

---------

Co-authored-by: Lancetnik <[email protected]>
Co-authored-by: Davor Runje <[email protected]>
  • Loading branch information
3 people authored Aug 30, 2024
1 parent 4944180 commit 79c7b78
Show file tree
Hide file tree
Showing 42 changed files with 108 additions and 136 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class Incoming(BaseModel):
async def hello(m: Incoming):
return {"response": "Hello, world!"}

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
```

Expand Down
2 changes: 1 addition & 1 deletion docs/create_api_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def create_api_docs(

def on_page_markdown(markdown, *, page, config, files):
"""Mkdocs hook to update the edit URL for the public API pages."""
if "public_api" in page.edit_url:
if page.edit_url and "public_api" in page.edit_url:
page.edit_url = page.edit_url.replace("public_api", "api")


Expand Down
5 changes: 4 additions & 1 deletion docs/docs/en/getting-started/integrations/fastapi/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Just import a **StreamRouter** you need and declare the message handler in the s

{! includes/getting_started/integrations/fastapi/1.md !}

!!! warning
If you are using **fastapi < 0.102.2** version, you should setup lifespan manually `#!python FastAPI(lifespan=router.lifespan_context)`

When processing a message from a broker, the entire message body is placed simultaneously in both the `body` and `path` request parameters. You can access them in any way convenient for you. The message header is placed in `headers`.

Also, this router can be fully used as an `HttpRouter` (of which it is the inheritor). So, you can
Expand Down Expand Up @@ -100,7 +103,7 @@ This way the core router collects all nested routers publishers and subscribers

### Custom lifespan

Otherwise, if you want to has multiple connections to different broker instances, you should start routers independently in your custom lifespan
Otherwise, if you want to has multiple connections to different broker instances, you can just include them separately to the app (each router has own broker and connection in this case)

{! includes/getting_started/integrations/fastapi/multiple_lifespan.md !}

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/en/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ hide:

The current release is planned as a latest feature release before **0.6.0**. All other **0.5.19+** releases will contain only minor bugfixes and all the team work will be focused on next major one.

There a lot of changes we want to present you now though!
There's a lot of changes we want to present you now though!

#### New RPC feature

Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/confluent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)):
async def hello_http():
return "Hello, HTTP!"

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/confluent/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

router = fastapi.KafkaRouter("localhost:9092")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


def broker():
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/confluent/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ async def nested_handler():

core_router.include_router(nested_router)

app = FastAPI(lifespan=core_router.lifespan_context)
app = FastAPI()
app.include_router(core_router)
20 changes: 6 additions & 14 deletions docs/docs_src/integrations/fastapi/confluent/multiple_lifespan.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from faststream.confluent.fastapi import KafkaRouter

core_router = KafkaRouter()
nested_router = KafkaRouter()
one_router = KafkaRouter()
another_router = KafkaRouter()

@asynccontextmanager
async def lifespan(app: FastAPI):
async with (
core_router.lifespan_context(app),
nested_router.lifespan_context(app),
):
yield
...

app = FastAPI(lifespan=lifespan)
app.include_router(core_router)
app.include_router(nested_router)
app = FastAPI()
app.include_router(one_router)
app.include_router(another_router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/confluent/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

router = KafkaRouter("localhost:9092")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


@router.get("/")
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/confluent/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ async def test(app: FastAPI):
await router.broker.publish("Hello!", "test")


app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/kafka/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)):
async def hello_http():
return "Hello, HTTP!"

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/kafka/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

router = fastapi.KafkaRouter("localhost:9092")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


def broker():
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/kafka/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ async def nested_handler():

core_router.include_router(nested_router)

app = FastAPI(lifespan=core_router.lifespan_context)
app = FastAPI()
app.include_router(core_router)
20 changes: 6 additions & 14 deletions docs/docs_src/integrations/fastapi/kafka/multiple_lifespan.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from faststream.kafka.fastapi import KafkaRouter

core_router = KafkaRouter()
nested_router = KafkaRouter()
one_router = KafkaRouter()
another_router = KafkaRouter()

@asynccontextmanager
async def lifespan(app: FastAPI):
async with (
core_router.lifespan_context(app),
nested_router.lifespan_context(app),
):
yield
...

app = FastAPI(lifespan=lifespan)
app.include_router(core_router)
app.include_router(nested_router)
app = FastAPI()
app.include_router(one_router)
app.include_router(another_router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/kafka/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

router = KafkaRouter("localhost:9092")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


@router.get("/")
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/kafka/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ async def test(app: FastAPI):
await router.broker.publish("Hello!", "test")


app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/nats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)):
async def hello_http():
return "Hello, HTTP!"

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/nats/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

router = fastapi.NatsRouter("nats://localhost:4222")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


def broker():
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/nats/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ async def nested_handler():

core_router.include_router(nested_router)

app = FastAPI(lifespan=core_router.lifespan_context)
app = FastAPI()
app.include_router(core_router)
20 changes: 6 additions & 14 deletions docs/docs_src/integrations/fastapi/nats/multiple_lifespan.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from faststream.nats.fastapi import NatsRouter

core_router = NatsRouter()
nested_router = NatsRouter()
one_router = NatsRouter()
another_router = NatsRouter()

@asynccontextmanager
async def lifespan(app: FastAPI):
async with (
core_router.lifespan_context(app),
nested_router.lifespan_context(app),
):
yield
...

app = FastAPI(lifespan=lifespan)
app.include_router(core_router)
app.include_router(nested_router)
app = FastAPI()
app.include_router(one_router)
app.include_router(another_router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/nats/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

router = NatsRouter("nats://localhost:4222")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


@router.get("/")
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/nats/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ async def test(app: FastAPI):
await router.broker.publish("Hello!", "test")


app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/rabbit/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)):
async def hello_http():
return "Hello, HTTP!"

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/rabbit/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

router = fastapi.RabbitRouter("amqp://guest:guest@localhost:5672/")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


def broker():
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/rabbit/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ async def nested_handler():

core_router.include_router(nested_router)

app = FastAPI(lifespan=core_router.lifespan_context)
app = FastAPI()
app.include_router(core_router)
20 changes: 6 additions & 14 deletions docs/docs_src/integrations/fastapi/rabbit/multiple_lifespan.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from faststream.rabbit.fastapi import RabbitRouter

core_router = RabbitRouter()
nested_router = RabbitRouter()
one_router = RabbitRouter()
another_router = RabbitRouter()

@asynccontextmanager
async def lifespan(app: FastAPI):
async with (
core_router.lifespan_context(app),
nested_router.lifespan_context(app),
):
yield
...

app = FastAPI(lifespan=lifespan)
app.include_router(core_router)
app.include_router(nested_router)
app = FastAPI()
app.include_router(one_router)
app.include_router(another_router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/rabbit/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

router = RabbitRouter("amqp://guest:guest@localhost:5672/")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


@router.get("/")
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/rabbit/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ async def test(app: FastAPI):
await router.broker.publish("Hello!", "test")


app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/redis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)):
async def hello_http():
return "Hello, HTTP!"

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/redis/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

router = fastapi.RedisRouter("redis://localhost:6379")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


def broker():
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/redis/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ async def nested_handler():

core_router.include_router(nested_router)

app = FastAPI(lifespan=core_router.lifespan_context)
app = FastAPI()
app.include_router(core_router)
20 changes: 6 additions & 14 deletions docs/docs_src/integrations/fastapi/redis/multiple_lifespan.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from contextlib import asynccontextmanager

from fastapi import FastAPI
from faststream.redis.fastapi import RedisRouter

core_router = RedisRouter()
nested_router = RedisRouter()
one_router = RedisRouter()
another_router = RedisRouter()

@asynccontextmanager
async def lifespan(app: FastAPI):
async with (
core_router.lifespan_context(app),
nested_router.lifespan_context(app),
):
yield
...

app = FastAPI(lifespan=lifespan)
app.include_router(core_router)
app.include_router(nested_router)
app = FastAPI()
app.include_router(one_router)
app.include_router(another_router)
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/redis/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

router = RedisRouter("redis://localhost:6379")

app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()


@router.get("/")
Expand Down
2 changes: 1 addition & 1 deletion docs/docs_src/integrations/fastapi/redis/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ async def test(app: FastAPI):
await router.broker.publish("Hello!", "test")


app = FastAPI(lifespan=router.lifespan_context)
app = FastAPI()
app.include_router(router)
Loading

0 comments on commit 79c7b78

Please sign in to comment.