Coverage for docs_src / generate_clients / tutorial003_py310.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import FastAPI 1abcd

2from fastapi.routing import APIRoute 1abcd

3from pydantic import BaseModel 1abcd

4 

5 

6def custom_generate_unique_id(route: APIRoute): 1abcd

7 return f"{route.tags[0]}-{route.name}" 1abcd

8 

9 

10app = FastAPI(generate_unique_id_function=custom_generate_unique_id) 1abcd

11 

12 

13class Item(BaseModel): 1abcd

14 name: str 1abcd

15 price: float 1abcd

16 

17 

18class ResponseMessage(BaseModel): 1abcd

19 message: str 1abcd

20 

21 

22class User(BaseModel): 1abcd

23 username: str 1abcd

24 email: str 1abcd

25 

26 

27@app.post("/items/", response_model=ResponseMessage, tags=["items"]) 1abcd

28async def create_item(item: Item): 1abcd

29 return {"message": "Item received"} 1efg

30 

31 

32@app.get("/items/", response_model=list[Item], tags=["items"]) 1abcd

33async def get_items(): 1abcd

34 return [ 1hij

35 {"name": "Plumbus", "price": 3}, 

36 {"name": "Portal Gun", "price": 9001}, 

37 ] 

38 

39 

40@app.post("/users/", response_model=ResponseMessage, tags=["users"]) 1abcd

41async def create_user(user: User): 1abcd

42 return {"message": "User received"} 1klm