Coverage for docs_src/additional_status_codes/tutorial001_an_py39.py: 100%
15 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 Annotated, Union 1abcd
3from fastapi import Body, FastAPI, status 1abcd
4from fastapi.responses import JSONResponse 1abcd
6app = FastAPI() 1abcd
8items = {"foo": {"name": "Fighters", "size": 6}, "bar": {"name": "Tenders", "size": 3}} 1abcd
11@app.put("/items/{item_id}") 1abcd
12async def upsert_item( 1abcd
13 item_id: str,
14 name: Annotated[Union[str, None], Body()] = None,
15 size: Annotated[Union[int, None], Body()] = None,
16):
17 if item_id in items: 1abcd
18 item = items[item_id] 1abcd
19 item["name"] = name 1abcd
20 item["size"] = size 1abcd
21 return item 1abcd
22 else:
23 item = {"name": name, "size": size} 1abcd
24 items[item_id] = item 1abcd
25 return JSONResponse(status_code=status.HTTP_201_CREATED, content=item) 1abcd