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