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

24 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from typing import List 1abcde

2 

3from fastapi import FastAPI 1abcde

4from fastapi.routing import APIRoute 1abcde

5from pydantic import BaseModel 1abcde

6 

7 

8def custom_generate_unique_id(route: APIRoute): 1abcde

9 return f"{route.tags[0]}-{route.name}" 1abcde

10 

11 

12app = FastAPI(generate_unique_id_function=custom_generate_unique_id) 1abcde

13 

14 

15class Item(BaseModel): 1abcde

16 name: str 1abcde

17 price: float 1abcde

18 

19 

20class ResponseMessage(BaseModel): 1abcde

21 message: str 1abcde

22 

23 

24class User(BaseModel): 1abcde

25 username: str 1abcde

26 email: str 1abcde

27 

28 

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

30async def create_item(item: Item): 1abcde

31 return {"message": "Item received"} 1abcde

32 

33 

34@app.get("/items/", response_model=List[Item], tags=["items"]) 1abcde

35async def get_items(): 1abcde

36 return [ 1abcde

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

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

39 ] 

40 

41 

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

43async def create_user(user: User): 1abcde

44 return {"message": "User received"} 1abcde