Coverage for docs_src / app_testing / app_b_an_py310 / main.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from typing import Annotated 1adbc
3from fastapi import FastAPI, Header, HTTPException 1adbc
4from pydantic import BaseModel 1adbc
6fake_secret_token = "coneofsilence" 1adbc
8fake_db = { 1adbc
9 "foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"},
10 "bar": {"id": "bar", "title": "Bar", "description": "The bartenders"},
11}
13app = FastAPI() 1adbc
16class Item(BaseModel): 1adbc
17 id: str 1abc
18 title: str 1abc
19 description: str | None = None 1adbc
22@app.get("/items/{item_id}", response_model=Item) 1adbc
23async def read_main(item_id: str, x_token: Annotated[str, Header()]): 1adbc
24 if x_token != fake_secret_token: 1efgh
25 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1efgh
26 if item_id not in fake_db: 1efgh
27 raise HTTPException(status_code=404, detail="Item not found") 1efgh
28 return fake_db[item_id] 1efgh
31@app.post("/items/") 1adbc
32async def create_item(item: Item, x_token: Annotated[str, Header()]) -> Item: 1adbc
33 if x_token != fake_secret_token: 1efgh
34 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1efgh
35 if item.id in fake_db: 1efgh
36 raise HTTPException(status_code=409, detail="Item already exists") 1efgh
37 fake_db[item.id] = item.model_dump() 1efgh
38 return item 1efgh