FastAPI integration for FastStream
Project description
FastAPI Plugin for FastStream
A plugin that allows you to use Depends and FastAPI other objects in the FastStream
Features
Use FastAPI Dependency Injection
In FastStream handlers, it will be possible to use the familiar DI from FastAPI
from fastapi import Path, Body, Header, Depends
class BodyModel(BaseModel):
field: int
@broker.subscriber("subject.{num}")
async def subscriber_handler(
num: Annotated[int, Path()],
body: Annotated[BodyModel, Body()],
x_user_id: Annotated[int, Header()],
my_dep: Annotated[int, Depends(int)],
) -> None:
...
Сan use FastAPI dependency overrides
fastapi = FastAPI()
fastapi.dependency_overrides[Dep] = lambda: "Dep"
@broker.subscriber("subject")
async def subscriber(dep: Annotated[str, De[]]) -> None:
assert dep == "Dep"
Using the FastStream Context
from faststream import Context
@broker.subscriber("subject")
async def subscriber_handler(context_data: Annotated[int, Context("data")]) -> Response: ...
Use FastAPI Request and Response
from fastapi import Request, Response
from fastapi.responses import JSONResponse
@broker.subscriber("subject")
async def subscriber_handler(request: Request) -> Response:
return JSONResponse({"data": 1})
Minimalistic plugin connection to your application
All you need to do is wrap the FastAPI with the FastStreamAPI object from the plugin
application = FastStreamAPI(
NatsBroker(),
application=FastAPI(),
)
uvicorn.run(application)
Managing a lifespan state
Now the lifespan state is also available in FastStream handlers
@asynccontextmanager
async def lifespan(app: FastAPI):
yield {"lifespan_data": "LIFESPAN DATA"}
@broker.subscriber("subject")
async def subscriber(request: Request) -> None:
assert request.state.lifespan_data == "LIFESPAN DATA"
The ability to configure AsyncAPI
The ability to configure AsyncAPI using SpecificationFactory from FastStream and AsyncAPIConfig from the plugin
FastStreamAPI(
...,
specification=AsyncAPI(
title="My app",
version="1.0.0",
description="...",
...,
),
asyncapi_path="/fs_docs",
# or
asyncapi_path=AsyncAPIRouter(
"/fs_docs",
description="...",
...
),
)
Backgrounds tasks
You can use BackgroundTasks from FastAPI in FastStream handlers
@broker.subscriber("subject")
async def handler1(
tasks: BackgroundTasks,
) -> None:
tasks.add_task(...)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file faststream_fastapi-1.1.0.tar.gz.
File metadata
- Download URL: faststream_fastapi-1.1.0.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe293c0ceab8558eebde3eb42455364549028837773dd633ef37b3f9e8e4f3f0
|
|
| MD5 |
a8182547d590daf32213727af820748c
|
|
| BLAKE2b-256 |
b9822fa3958dd6014b228ae4c11194f26bbdfd131e38e923d122238ffe72c84f
|
Provenance
The following attestation bundles were made for faststream_fastapi-1.1.0.tar.gz:
Publisher:
publish.yml on faststream-community/faststream_fastapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
faststream_fastapi-1.1.0.tar.gz -
Subject digest:
fe293c0ceab8558eebde3eb42455364549028837773dd633ef37b3f9e8e4f3f0 - Sigstore transparency entry: 2203571966
- Sigstore integration time:
-
Permalink:
faststream-community/faststream_fastapi@af26944ab791c9c902a21899fb16d31caf0375db -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/faststream-community
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@af26944ab791c9c902a21899fb16d31caf0375db -
Trigger Event:
release
-
Statement type:
File details
Details for the file faststream_fastapi-1.1.0-py3-none-any.whl.
File metadata
- Download URL: faststream_fastapi-1.1.0-py3-none-any.whl
- Upload date:
- Size: 19.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
846261d2c7e9c41945b2b66726c42bc748f198c5a8aa0560c4434bee608d511d
|
|
| MD5 |
df915a9b4ac029a023128b3d3dbe1c1e
|
|
| BLAKE2b-256 |
a92aeef05e9c7072526636f2fa89f45ab4e949a6c37e6132325e3084532f9ac7
|
Provenance
The following attestation bundles were made for faststream_fastapi-1.1.0-py3-none-any.whl:
Publisher:
publish.yml on faststream-community/faststream_fastapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
faststream_fastapi-1.1.0-py3-none-any.whl -
Subject digest:
846261d2c7e9c41945b2b66726c42bc748f198c5a8aa0560c4434bee608d511d - Sigstore transparency entry: 2203571983
- Sigstore integration time:
-
Permalink:
faststream-community/faststream_fastapi@af26944ab791c9c902a21899fb16d31caf0375db -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/faststream-community
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@af26944ab791c9c902a21899fb16d31caf0375db -
Trigger Event:
release
-
Statement type: