Coverage for docs_src/additional_responses/tutorial004.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 typing import Union 1abcde
3from fastapi import FastAPI 1abcde
4from fastapi.responses import FileResponse 1abcde
5from pydantic import BaseModel 1abcde
8class Item(BaseModel): 1abcde
9 id: str 1abcde
10 value: str 1abcde
13responses = { 1abcde
14 404: {"description": "Item not found"},
15 302: {"description": "The item was moved"},
16 403: {"description": "Not enough privileges"},
17}
20app = FastAPI() 1abcde
23@app.get( 1abcde
24 "/items/{item_id}",
25 response_model=Item,
26 responses={**responses, 200: {"content": {"image/png": {}}}},
27)
28async def read_item(item_id: str, img: Union[bool, None] = None): 1abcde
29 if img: 1abcde
30 return FileResponse("image.png", media_type="image/png") 1abcde
31 else:
32 return {"id": "foo", "value": "there goes my hero"} 1abcde