mirror of
https://github.com/rjNemo/fastapi
synced 2026-06-08 03:26:48 +00:00
* ✨ Add support for BackgroundTasks parameters * 🐛 Fix type declaration in dependencies * 🐛 Fix coverage of util in tests
24 lines
636 B
Python
24 lines
636 B
Python
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: str = 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"}
|