Coverage for docs_src/app_testing/app_b_an/main.py: 100%
26 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1from typing import Union 1abcdef
3from fastapi import FastAPI, Header, HTTPException 1abcdef
4from pydantic import BaseModel 1abcdef
5from typing_extensions import Annotated 1abcdef
7fake_secret_token = "coneofsilence" 1abcdef
9fake_db = { 1abcdef
10 "foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"},
11 "bar": {"id": "bar", "title": "Bar", "description": "The bartenders"},
12}
14app = FastAPI() 1abcdef
17class Item(BaseModel): 1abcdef
18 id: str 1abcdef
19 title: str 1abcdef
20 description: Union[str, None] = None 1abcdef
23@app.get("/items/{item_id}", response_model=Item) 1abcdef
24async def read_main(item_id: str, x_token: Annotated[str, Header()]): 1abcdef
25 if x_token != fake_secret_token: 1ghijkl
26 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1ghijkl
27 if item_id not in fake_db: 1ghijkl
28 raise HTTPException(status_code=404, detail="Item not found") 1ghijkl
29 return fake_db[item_id] 1ghijkl
32@app.post("/items/", response_model=Item) 1abcdef
33async def create_item(item: Item, x_token: Annotated[str, Header()]): 1abcdef
34 if x_token != fake_secret_token: 1ghijkl
35 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1ghijkl
36 if item.id in fake_db: 1ghijkl
37 raise HTTPException(status_code=409, detail="Item already exists") 1ghijkl
38 fake_db[item.id] = item 1ghijkl
39 return item 1ghijkl