mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-25 23:29:34 -05:00
* 📝 Add docs recommending Union over Optional * 📝 Update docs recommending Union over Optional * 📝 Update source examples for docs, recommend Union over Optional * 📝 Update highlighted lines with updated source examples * 📝 Update highlighted lines in Markdown with recent code changes * 📝 Update docs, use Union instead of Optional * ♻️ Update source examples to recommend Union over Optional * 🎨 Update highlighted code in Markdown after moving from Optional to Union
27 lines
675 B
Python
27 lines
675 B
Python
from typing import Union
|
|
|
|
from fastapi import BackgroundTasks, Depends, FastAPI
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def write_log(message: str):
|
|
with open("log.txt", mode="a") as log:
|
|
log.write(message)
|
|
|
|
|
|
def get_query(background_tasks: BackgroundTasks, q: Union[str, None] = None):
|
|
if q:
|
|
message = f"found query: {q}\n"
|
|
background_tasks.add_task(write_log, message)
|
|
return q
|
|
|
|
|
|
@app.post("/send-notification/{email}")
|
|
async def send_notification(
|
|
email: str, background_tasks: BackgroundTasks, q: str = Depends(get_query)
|
|
):
|
|
message = f"message to {email}\n"
|
|
background_tasks.add_task(write_log, message)
|
|
return {"message": "Message sent"}
|