File: patch_requests.py

package info (click to toggle)
litestar 2.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,500 kB
  • sloc: python: 70,169; makefile: 254; javascript: 105; sh: 60
file content (35 lines) | stat: -rw-r--r-- 971 bytes parent folder | download
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 __future__ import annotations

from dataclasses import dataclass
from uuid import UUID

from litestar import Litestar, patch
from litestar.dto import DataclassDTO, DTOConfig, DTOData


@dataclass
class Person:
    id: UUID
    name: str
    age: int


class PatchDTO(DataclassDTO[Person]):
    """Don't allow client to set the id, and allow partial updates."""

    config = DTOConfig(exclude={"id"}, partial=True)


peter_uuid = UUID("f32ff2ce-e32f-4537-9dc0-26e7599f1380")
database = {peter_uuid: Person(id=peter_uuid, name="Peter", age=40)}


@patch("/person/{person_id:uuid}", dto=PatchDTO, return_dto=None, sync_to_thread=False)
def update_person(person_id: UUID, data: DTOData[Person]) -> Person:
    """Partially update a person."""
    return data.update_instance(database[person_id])


app = Litestar(route_handlers=[update_person])

# run: /person/f32ff2ce-e32f-4537-9dc0-26e7599f1380 -X PATCH -H "Content-Type: application/json" -d '{"name":"Peter Pan"}'