Skip to content

Commit 065965a

Browse files
authored
Merge pull request #612 from AnswerDotAI/apirouter-refactor
Adds "body_wrap" bits to APIRouter
2 parents 81cf119 + a6adf30 commit 065965a

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

fasthtml/core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,11 @@ def __dir__(self): return list(self._funcs.keys())
666666
# %% ../nbs/api/00_core.ipynb
667667
class APIRouter:
668668
"Add routes to an app"
669-
def __init__(self, prefix:str|None=None):
669+
def __init__(self, prefix:str|None=None, body_wrap=noop_body):
670670
self.routes,self.wss = [],[]
671671
self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability
672672
self.prefix = prefix if prefix else ""
673+
self.body_wrap = body_wrap
673674

674675
def _wrap_func(self, func, path=None):
675676
name = func.__name__
@@ -679,12 +680,12 @@ def _wrap_func(self, func, path=None):
679680
if name not in all_meths: setattr(self.rt_funcs, name, wrapped)
680681
return wrapped
681682

682-
def __call__(self, path:str=None, methods=None, name=None, include_in_schema=True, body_wrap=noop_body):
683+
def __call__(self, path:str=None, methods=None, name=None, include_in_schema=True, body_wrap=None):
683684
"Add a route at `path`"
684685
def f(func):
685686
p = self.prefix + ("/" + ('' if path.__name__=='index' else func.__name__) if callable(path) else path)
686687
wrapped = self._wrap_func(func, p)
687-
self.routes.append((func, p, methods, name, include_in_schema, body_wrap))
688+
self.routes.append((func, p, methods, name, include_in_schema, body_wrap or self.body_wrap))
688689
return wrapped
689690
return f(path) if callable(path) else f
690691

nbs/api/00_core.ipynb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2024, 12, 10, 14, 0)"
134+
"datetime.datetime(2024, 12, 20, 14, 0)"
135135
]
136136
},
137137
"execution_count": null,
@@ -2423,13 +2423,13 @@
24232423
"name": "stdout",
24242424
"output_type": "stream",
24252425
"text": [
2426-
"Set to 2024-12-10 09:54:42.331681\n"
2426+
"Set to 2024-12-20 19:21:33.748637\n"
24272427
]
24282428
},
24292429
{
24302430
"data": {
24312431
"text/plain": [
2432-
"'Session time: 2024-12-10 09:54:42.331681'"
2432+
"'Session time: 2024-12-20 19:21:33.748637'"
24332433
]
24342434
},
24352435
"execution_count": null,
@@ -2636,10 +2636,11 @@
26362636
"#| export\n",
26372637
"class APIRouter:\n",
26382638
" \"Add routes to an app\"\n",
2639-
" def __init__(self, prefix:str|None=None): \n",
2639+
" def __init__(self, prefix:str|None=None, body_wrap=noop_body): \n",
26402640
" self.routes,self.wss = [],[]\n",
26412641
" self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability\n",
26422642
" self.prefix = prefix if prefix else \"\"\n",
2643+
" self.body_wrap = body_wrap\n",
26432644
"\n",
26442645
" def _wrap_func(self, func, path=None):\n",
26452646
" name = func.__name__\n",
@@ -2649,12 +2650,12 @@
26492650
" if name not in all_meths: setattr(self.rt_funcs, name, wrapped)\n",
26502651
" return wrapped\n",
26512652
"\n",
2652-
" def __call__(self, path:str=None, methods=None, name=None, include_in_schema=True, body_wrap=noop_body):\n",
2653+
" def __call__(self, path:str=None, methods=None, name=None, include_in_schema=True, body_wrap=None):\n",
26532654
" \"Add a route at `path`\"\n",
26542655
" def f(func):\n",
26552656
" p = self.prefix + (\"/\" + ('' if path.__name__=='index' else func.__name__) if callable(path) else path)\n",
26562657
" wrapped = self._wrap_func(func, p)\n",
2657-
" self.routes.append((func, p, methods, name, include_in_schema, body_wrap))\n",
2658+
" self.routes.append((func, p, methods, name, include_in_schema, body_wrap or self.body_wrap))\n",
26582659
" return wrapped\n",
26592660
" return f(path) if callable(path) else f\n",
26602661
" \n",
@@ -2949,7 +2950,7 @@
29492950
{
29502951
"data": {
29512952
"text/plain": [
2952-
"'Cookie was set at time 09:54:43.255286'"
2953+
"'Cookie was set at time 19:21:34.644743'"
29532954
]
29542955
},
29552956
"execution_count": null,

0 commit comments

Comments
 (0)