Coverage for docs_src/additional_status_codes/tutorial001.py: 100%

15 statements  

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

1from typing import Union 1abcde

2 

3from fastapi import Body, FastAPI, status 1abcde

4from fastapi.responses import JSONResponse 1abcde

5 

6app = FastAPI() 1abcde

7 

8items = {"foo": {"name": "Fighters", "size": 6}, "bar": {"name": "Tenders", "size": 3}} 1abcde

9 

10 

11@app.put("/items/{item_id}") 1abcde

12async def upsert_item( 1abcde

13 item_id: str, 

14 name: Union[str, None] = Body(default=None), 

15 size: Union[int, None] = Body(default=None), 

16): 

17 if item_id in items: 1abcde

18 item = items[item_id] 1abcde

19 item["name"] = name 1abcde

20 item["size"] = size 1abcde

21 return item 1abcde

22 else: 

23 item = {"name": name, "size": size} 1abcde

24 items[item_id] = item 1abcde

25 return JSONResponse(status_code=status.HTTP_201_CREATED, content=item) 1abcde