Coverage for docs_src/additional_responses/tutorial002.py: 100%

13 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 FastAPI 1abcde

4from fastapi.responses import FileResponse 1abcde

5from pydantic import BaseModel 1abcde

6 

7 

8class Item(BaseModel): 1abcde

9 id: str 1abcde

10 value: str 1abcde

11 

12 

13app = FastAPI() 1abcde

14 

15 

16@app.get( 1abcde

17 "/items/{item_id}", 

18 response_model=Item, 

19 responses={ 

20 200: { 

21 "content": {"image/png": {}}, 

22 "description": "Return the JSON item or an image.", 

23 } 

24 }, 

25) 

26async def read_item(item_id: str, img: Union[bool, None] = None): 1abcde

27 if img: 1abcde

28 return FileResponse("image.png", media_type="image/png") 1abcde

29 else: 

30 return {"id": "foo", "value": "there goes my hero"} 1abcde