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