1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
from pydantic import BaseModel
from typing_extensions import Annotated
from litestar import Litestar, get
from litestar.exceptions import NotAuthorizedException
from litestar.params import Parameter
USER_DB = {
1: {
"id": 1,
"name": "John Doe",
},
}
VALID_TOKEN = "super-secret-secret"
VALID_COOKIE_VALUE = "cookie-secret"
class User(BaseModel):
id: int
name: str
@get(path="/users/{user_id:int}/")
async def get_user(
user_id: int,
token: Annotated[str, Parameter(header="X-API-KEY")],
cookie: Annotated[str, Parameter(cookie="my-cookie-param")],
) -> User:
if token != VALID_TOKEN or cookie != VALID_COOKIE_VALUE:
raise NotAuthorizedException
return User.model_validate(USER_DB[user_id])
app = Litestar(route_handlers=[get_user])
|