Coverage for docs_src/additional_responses/tutorial003.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 FastAPI 1abcde
2from fastapi.responses import JSONResponse 1abcde
3from pydantic import BaseModel 1abcde
6class Item(BaseModel): 1abcde
7 id: str 1abcde
8 value: str 1abcde
11class Message(BaseModel): 1abcde
12 message: str 1abcde
15app = FastAPI() 1abcde
18@app.get( 1abcde
19 "/items/{item_id}",
20 response_model=Item,
21 responses={
22 404: {"model": Message, "description": "The item was not found"},
23 200: {
24 "description": "Item requested by ID",
25 "content": {
26 "application/json": {
27 "example": {"id": "bar", "value": "The bar tenders"}
28 }
29 },
30 },
31 },
32)
33async def read_item(item_id: str): 1abcde
34 if item_id == "foo": 1abcde
35 return {"id": "foo", "value": "there goes my hero"} 1abcde
36 else:
37 return JSONResponse(status_code=404, content={"message": "Item not found"}) 1abcde