Coverage for docs_src/app_testing/app_b_an/main.py: 100%
26 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 Union 1abcde
3from fastapi import FastAPI, Header, HTTPException 1abcde
4from pydantic import BaseModel 1abcde
5from typing_extensions import Annotated 1abcde
7fake_secret_token = "coneofsilence" 1abcde
9fake_db = { 1abcde
10 "foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"},
11 "bar": {"id": "bar", "title": "Bar", "description": "The bartenders"},
12}
14app = FastAPI() 1abcde
17class Item(BaseModel): 1abcde
18 id: str 1abcde
19 title: str 1abcde
20 description: Union[str, None] = None 1abcde
23@app.get("/items/{item_id}", response_model=Item) 1abcde
24async def read_main(item_id: str, x_token: Annotated[str, Header()]): 1abcde
25 if x_token != fake_secret_token: 1abcde
26 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1abcde
27 if item_id not in fake_db: 1abcde
28 raise HTTPException(status_code=404, detail="Item not found") 1abcde
29 return fake_db[item_id] 1abcde
32@app.post("/items/", response_model=Item) 1abcde
33async def create_item(item: Item, x_token: Annotated[str, Header()]): 1abcde
34 if x_token != fake_secret_token: 1abcde
35 raise HTTPException(status_code=400, detail="Invalid X-Token header") 1abcde
36 if item.id in fake_db: 1abcde
37 raise HTTPException(status_code=400, detail="Item already exists") 1abcde
38 fake_db[item.id] = item 1abcde
39 return item 1abcde