FastAPI integration
Installation
While we can install Wirio with uv add wirio, it's recommended to add the fastapi extra to ensure the installed FastAPI version is compatible.
Quickstart
from typing import Annotated
from fastapi import FastAPI
from wirio.annotations import FromServices
from wirio.service_collection import ServiceCollection
class EmailService:
pass
class UserService:
def __init__(self, email_service: EmailService) -> None:
self.email_service = email_service
async def create_user(self) -> None:
pass
app = FastAPI()
@app.post("/users")
async def create_user(
user_service: Annotated[UserService, FromServices()], # (1)!
) -> None:
pass
services = ServiceCollection()
services.add_transient(EmailService)
services.add_transient(UserService)
services.configure_fastapi(app) # (2)!
- Annotate the parameter with the type to resolve
- This will configure FastAPI to use Wirio's dependency injection
Testing
Information available here.