Repository navigation
Expand file tree
/
Copy pathtodo.py
More file actions
326 lines (272 loc) · 9.46 KB
/
Copy pathtodo.py
File metadata and controls
326 lines (272 loc) · 9.46 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""A practical Todo API example for Responder.
Run it:
responder run examples/todo.py
Try it with:
curl http://127.0.0.1:5042/todos
curl -H "Authorization: Bearer demo-token" \
-H "Content-Type: application/json" \
-d '{"title": "Write the release notes", "tags": ["release"]}' \
http://127.0.0.1:5042/todos
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import UTC, date, datetime
from typing import Literal
from pydantic import BaseModel, Field, field_validator
import responder
from responder.ext.auth import BearerAuth
TodoPriority = Literal["low", "normal", "high"]
def _now() -> datetime:
return datetime.now(UTC).replace(microsecond=0)
def _clean_tags(tags: list[str]) -> list[str]:
normalized: list[str] = []
for tag in tags:
cleaned = tag.strip().lower()
if cleaned and cleaned not in normalized:
normalized.append(cleaned)
return normalized
class TodoCreate(BaseModel):
title: str = Field(min_length=1, examples=["Write the release notes"])
notes: str = Field(default="", examples=["Link to the changelog."])
due: date | None = Field(default=None, examples=["2026-07-01"])
priority: TodoPriority = Field(default="normal", examples=["high"])
tags: list[str] = Field(default_factory=list, examples=[["release", "docs"]])
@field_validator("tags")
@classmethod
def normalize_tags(cls, tags: list[str]) -> list[str]:
return _clean_tags(tags)
class TodoPatch(BaseModel):
title: str | None = Field(default=None, min_length=1)
notes: str | None = None
due: date | None = None
priority: TodoPriority | None = None
tags: list[str] | None = None
completed: bool | None = None
@field_validator("tags")
@classmethod
def normalize_tags(cls, tags: list[str] | None) -> list[str] | None:
return None if tags is None else _clean_tags(tags)
class TodoOut(TodoCreate):
id: int
completed: bool
created_at: datetime
completed_at: datetime | None
owner: str
class User(BaseModel):
name: str
scopes: list[str]
TODO_EXAMPLE = {
"id": 1,
"title": "Write the release notes",
"notes": "Link to the changelog.",
"due": "2026-07-01",
"priority": "high",
"tags": ["release", "docs"],
"completed": False,
"created_at": "2026-06-30T12:00:00Z",
"completed_at": None,
"owner": "Ada",
}
@dataclass
class TodoStore:
todos: dict[int, TodoOut] = field(default_factory=dict)
next_id: int = 1
@classmethod
def seeded(cls) -> TodoStore:
store = cls()
store.create(
TodoCreate(
title="Sketch the release post",
notes="Keep it short, useful, and link the changelog.",
due=date(2026, 7, 1),
priority="high",
tags=["release", "writing"],
),
owner="Ada",
)
done = store.create(
TodoCreate(
title="Review generated client ergonomics",
notes="Check the golden contract app before shipping.",
priority="normal",
tags=["clientgen", "quality"],
),
owner="Grace",
)
store.update(done.id, TodoPatch(completed=True))
return store
def all(
self,
*,
completed: bool | None = None,
tag: str | None = None,
) -> list[TodoOut]:
rows = sorted(self.todos.values(), key=lambda item: item.id)
if completed is not None:
rows = [item for item in rows if item.completed is completed]
if tag:
wanted = tag.strip().lower()
rows = [item for item in rows if wanted in item.tags]
return [item.model_copy(deep=True) for item in rows]
def get(self, todo_id: int) -> TodoOut | None:
todo = self.todos.get(todo_id)
return None if todo is None else todo.model_copy(deep=True)
def create(self, todo: TodoCreate, *, owner: str) -> TodoOut:
todo_id = self.next_id
self.next_id += 1
record = TodoOut(
id=todo_id,
completed=False,
created_at=_now(),
completed_at=None,
owner=owner,
**todo.model_dump(),
)
self.todos[todo_id] = record
return record.model_copy(deep=True)
def update(self, todo_id: int, patch: TodoPatch) -> TodoOut | None:
todo = self.todos.get(todo_id)
if todo is None:
return None
updates = patch.model_dump(exclude_unset=True)
updates = {
key: value
for key, value in updates.items()
if value is not None or key == "due"
}
if "completed" in updates:
completed = updates["completed"]
updates["completed_at"] = (
todo.completed_at or _now()
) if completed else None
updated = todo.model_copy(update=updates)
self.todos[todo_id] = updated
return updated.model_copy(deep=True)
def complete(self, todo_id: int) -> TodoOut | None:
return self.update(todo_id, TodoPatch(completed=True))
def delete(self, todo_id: int) -> bool:
return self.todos.pop(todo_id, None) is not None
def create_api(*, store: TodoStore | None = None) -> responder.API:
store = store or TodoStore.seeded()
users = {
"demo-token": User(name="Ada", scopes=["todos:write"]),
"ops-token": User(name="Grace", scopes=["todos:write"]),
}
bearer = BearerAuth(
verify=lambda token: users.get(token),
bearer_format="opaque",
realm="todos",
)
api = responder.API(
title="Todo API",
version="1.0",
openapi="3.1.0",
docs_route="/docs",
sessions=False,
request_id=True,
)
writer = api.policy("writer", bearer.requires("todos:write"))
@api.get("/", include_in_schema=False)
def index(req, resp):
resp.media = {"name": "Todo API", "todos": "/todos", "docs": "/docs"}
@api.get(
"/todos",
operation_id="list_todos",
tags=["todos"],
summary="List todos",
description="Return every todo, optionally filtered by completion or tag.",
examples={"seed": {"summary": "Seeded todos", "value": [TODO_EXAMPLE]}},
)
def list_todos(
req,
resp,
*,
completed: bool | None = responder.Query(
None,
description="Only return active or completed todos.",
),
tag: str | None = responder.Query(
None,
description="Only return todos with this tag.",
),
) -> list[TodoOut]:
return store.all(completed=completed, tag=tag)
@api.post(
"/todos",
auth=writer,
operation_id="create_todo",
tags=["todos"],
summary="Create a todo",
status_code=201,
responses={201: {"description": "Todo created"}},
response_examples={201: {"created": {"value": TODO_EXAMPLE}}},
)
def create_todo(req, resp, *, user: User, todo: TodoCreate) -> TodoOut:
created = store.create(todo, owner=user.name)
resp.headers["Location"] = f"/todos/{created.id}"
return created
@api.get(
"/todos/{todo_id:int}",
operation_id="get_todo",
tags=["todos"],
summary="Fetch a todo",
response_model=TodoOut,
responses={404: "Todo not found"},
examples={"release_notes": {"value": TODO_EXAMPLE}},
)
def get_todo(req, resp, *, todo_id: int):
todo = store.get(todo_id)
if todo is None:
resp.problem(404, f"Todo {todo_id} does not exist.", todo_id=todo_id)
return
resp.media = todo
@api.patch(
"/todos/{todo_id:int}",
auth=writer,
operation_id="update_todo",
tags=["todos"],
summary="Update a todo",
response_model=TodoOut,
responses={400: "No changes supplied", 404: "Todo not found"},
)
def update_todo(req, resp, *, todo_id: int, user: User, patch: TodoPatch):
if not patch.model_fields_set:
resp.problem(400, "Send at least one field to update.", todo_id=todo_id)
return
todo = store.update(todo_id, patch)
if todo is None:
resp.problem(404, f"Todo {todo_id} does not exist.", todo_id=todo_id)
return
resp.media = todo
@api.post(
"/todos/{todo_id:int}/complete",
auth=writer,
operation_id="complete_todo",
tags=["todos"],
summary="Complete a todo",
response_model=TodoOut,
responses={404: "Todo not found"},
)
def complete_todo(req, resp, *, todo_id: int, user: User):
todo = store.complete(todo_id)
if todo is None:
resp.problem(404, f"Todo {todo_id} does not exist.", todo_id=todo_id)
return
resp.media = todo
@api.delete(
"/todos/{todo_id:int}",
auth=writer,
operation_id="delete_todo",
tags=["todos"],
summary="Delete a todo",
responses={204: "Todo deleted", 404: "Todo not found"},
)
def delete_todo(req, resp, *, todo_id: int, user: User):
if not store.delete(todo_id):
resp.problem(404, f"Todo {todo_id} does not exist.", todo_id=todo_id)
return
resp.no_content(headers={"X-Deleted-Todo": str(todo_id)})
return api
api = create_api()
if __name__ == "__main__":
api.run()

