| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from dataclasses import dataclass |
| 4 |
|
| 5 |
from litestar import Litestar, get |
| 6 |
from litestar.dto import DataclassDTO, DTOConfig |
| 7 |
from litestar.params import FromPath |
| 8 |
|
| 9 |
|
| 10 |
@dataclass |
| 11 |
class Address: |
| 12 |
street: str |
| 13 |
city: str |
| 14 |
country: str |
| 15 |
|
| 16 |
|
| 17 |
@dataclass |
| 18 |
class Person: |
| 19 |
name: str |
| 20 |
age: int |
| 21 |
email: str |
| 22 |
address: Address |
| 23 |
|
| 24 |
|
| 25 |
class ReadDTO(DataclassDTO[Person]): |
| 26 |
config = DTOConfig(exclude={"email", "address.street"}) |
| 27 |
|
| 28 |
|
| 29 |
@get("/person/{name:str}", return_dto=ReadDTO, sync_to_thread=False) |
| 30 |
def get_person(name: FromPath[str]) -> Person: |
| 31 |
# Your logic to retrieve the person goes here |
| 32 |
# For demonstration purposes, a placeholder Person instance is returned |
| 33 |
address = Address(street="123 Main St", city="Cityville", country="Countryland") |
| 34 |
return Person(name=name, age=30, email=f"email_of_{name}@example.com", address=address) |
| 35 |
|
| 36 |
|
| 37 |
app = Litestar(route_handlers=[get_person]) |