Coverage for docs_src/app_testing/app_b_py310/main.py: 100%

24 statements  

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

1from fastapi import FastAPI, Header, HTTPException 1abc

2from pydantic import BaseModel 1abc

3 

4fake_secret_token = "coneofsilence" 1abc

5 

6fake_db = { 1abc

7 "foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"}, 

8 "bar": {"id": "bar", "title": "Bar", "description": "The bartenders"}, 

9} 

10 

11app = FastAPI() 1abc

12 

13 

14class Item(BaseModel): 1abc

15 id: str 1abc

16 title: str 1abc

17 description: str | None = None 1abc

18 

19 

20@app.get("/items/{item_id}", response_model=Item) 1abc

21async def read_main(item_id: str, x_token: str = Header()): 1abc

22 if x_token != fake_secret_token: 1abc

23 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1abc

24 if item_id not in fake_db: 1abc

25 raise HTTPException(status_code=404, detail="Item not found") 1abc

26 return fake_db[item_id] 1abc

27 

28 

29@app.post("/items/", response_model=Item) 1abc

30async def create_item(item: Item, x_token: str = Header()): 1abc

31 if x_token != fake_secret_token: 1abc

32 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1abc

33 if item.id in fake_db: 1abc

34 raise HTTPException(status_code=409, detail="Item already exists") 1abc

35 fake_db[item.id] = item 1abc

36 return item 1abc