Coverage for docs_src/background_tasks/tutorial002_py310.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi import BackgroundTasks, Depends, FastAPI 1abc

2 

3app = FastAPI() 1abc

4 

5 

6def write_log(message: str): 1abc

7 with open("log.txt", mode="a") as log: 1abc

8 log.write(message) 1abc

9 

10 

11def get_query(background_tasks: BackgroundTasks, q: str | None = None): 1abc

12 if q: 1abc

13 message = f"found query: {q}\n" 1abc

14 background_tasks.add_task(write_log, message) 1abc

15 return q 1abc

16 

17 

18@app.post("/send-notification/{email}") 1abc

19async def send_notification( 1abc

20 email: str, background_tasks: BackgroundTasks, q: str = Depends(get_query) 

21): 

22 message = f"message to {email}\n" 1abc

23 background_tasks.add_task(write_log, message) 1abc

24 return {"message": "Message sent"} 1abc