Coverage for docs_src/app_testing/app_b_py310/main.py: 100%
24 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1from fastapi import FastAPI, Header, HTTPException 1abcde
2from pydantic import BaseModel 1abcde
4fake_secret_token = "coneofsilence" 1abcde
6fake_db = { 1abcde
7 "foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"},
8 "bar": {"id": "bar", "title": "Bar", "description": "The bartenders"},
9}
11app = FastAPI() 1abcde
14class Item(BaseModel): 1abcde
15 id: str 1abcde
16 title: str 1abcde
17 description: str | None = None 1abcde
20@app.get("/items/{item_id}", response_model=Item) 1abcde
21async def read_main(item_id: str, x_token: str = Header()): 1abcde
22 if x_token != fake_secret_token: 1fghij
23 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1fghij
24 if item_id not in fake_db: 1fghij
25 raise HTTPException(status_code=404, detail="Item not found") 1fghij
26 return fake_db[item_id] 1fghij
29@app.post("/items/", response_model=Item) 1abcde
30async def create_item(item: Item, x_token: str = Header()): 1abcde
31 if x_token != fake_secret_token: 1fghij
32 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1fghij
33 if item.id in fake_db: 1fghij
34 raise HTTPException(status_code=409, detail="Item already exists") 1fghij
35 fake_db[item.id] = item 1fghij
36 return item 1fghij