Coverage for docs_src/background_tasks/tutorial002_an.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
1from typing import Union 1fghij
3from fastapi import BackgroundTasks, Depends, FastAPI 1fghij
4from typing_extensions import Annotated 1fghij
6app = FastAPI() 1fghij
9def write_log(message: str): 1fghij
10 with open("log.txt", mode="a") as log: 1abcde
11 log.write(message) 1abcde
14def get_query(background_tasks: BackgroundTasks, q: Union[str, None] = None): 1fghij
15 if q: 1abcde
16 message = f"found query: {q}\n" 1abcde
17 background_tasks.add_task(write_log, message) 1abcde
18 return q 1abcde
21@app.post("/send-notification/{email}") 1fghij
22async def send_notification( 1fghij
23 email: str, background_tasks: BackgroundTasks, q: Annotated[str, Depends(get_query)]
24):
25 message = f"message to {email}\n" 1abcde
26 background_tasks.add_task(write_log, message) 1abcde
27 return {"message": "Message sent"} 1abcde