mirror of
https://github.com/fastapi/fastapi.git
synced 2026-01-03 03:28:39 -05:00
* 🌐 Refactor file structure to support internationalization * ✅ Update tests changed after i18n * 🔀 Merge Typer style from master * 🔧 Update MkConfig with Typer-styles * 🎨 Format mkdocs.yml with cannonical form * 🎨 Format mkdocs.yml * 🔧 Update MkDocs config * ➕ Add docs translation scripts dependencies * ✨ Add Typer scripts to handle translations * ✨ Add missing translation snippet to include * ✨ Update contributing docs, add docs for translations * 🙈 Add docs_build to gitignore * 🔧 Update scripts with new locations and docs scripts * 👷 Update docs deploy action with translations * 📝 Add note about languages not supported in the theme * ✨ Add first translation, for Spanish
37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
from fastapi import FastAPI, Header, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
fake_secret_token = "coneofsilence"
|
|
|
|
fake_db = {
|
|
"foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"},
|
|
"bar": {"id": "bar", "title": "Bar", "description": "The bartenders"},
|
|
}
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Item(BaseModel):
|
|
id: str
|
|
title: str
|
|
description: str = None
|
|
|
|
|
|
@app.get("/items/{item_id}", response_model=Item)
|
|
async def read_main(item_id: str, x_token: str = Header(...)):
|
|
if x_token != fake_secret_token:
|
|
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
|
if item_id not in fake_db:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return fake_db[item_id]
|
|
|
|
|
|
@app.post("/items/", response_model=Item)
|
|
async def create_item(item: Item, x_token: str = Header(...)):
|
|
if x_token != fake_secret_token:
|
|
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
|
if item.id in fake_db:
|
|
raise HTTPException(status_code=400, detail="Item already exists")
|
|
fake_db[item.id] = item
|
|
return item
|