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

16 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

5from typing_extensions import Annotated 1abcde

6 

7app = FastAPI() 1abcde

8 

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

10 

11 

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

13async def upsert_item( 1abcde

14 item_id: str, 

15 name: Annotated[Union[str, None], Body()] = None, 

16 size: Annotated[Union[int, None], Body()] = None, 

17): 

18 if item_id in items: 1abcde

19 item = items[item_id] 1abcde

20 item["name"] = name 1abcde

21 item["size"] = size 1abcde

22 return item 1abcde

23 else: 

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

25 items[item_id] = item 1abcde

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