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