Skip to content

Commit 6383de6

Browse files
committed
added "body_wrap" bits to APIRouter
1 parent 81cf119 commit 6383de6

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

fasthtml/core.py

Lines changed: 7 additions & 4 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

@@ -694,7 +695,9 @@ def __getattr__(self, name):
694695

695696
def to_app(self, app):
696697
"Add routes to `app`"
697-
for args in self.routes: app._add_route(*args)
698+
for args in self.routes:
699+
print(args)
700+
app._add_route(*args)
698701
for args in self.wss: app._add_ws(*args)
699702

700703
def ws(self, path:str, conn=None, disconn=None, name=None, middleware=None):

nbs/api/00_core.ipynb

Lines changed: 39 additions & 10 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:13:21.676722\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:13:21.676722'"
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",
@@ -2664,7 +2665,9 @@
26642665
"\n",
26652666
" def to_app(self, app):\n",
26662667
" \"Add routes to `app`\"\n",
2667-
" for args in self.routes: app._add_route(*args)\n",
2668+
" for args in self.routes: \n",
2669+
" print(args)\n",
2670+
" app._add_route(*args)\n",
26682671
" for args in self.wss: app._add_ws(*args)\n",
26692672
" \n",
26702673
" def ws(self, path:str, conn=None, disconn=None, name=None, middleware=None):\n",
@@ -2712,7 +2715,20 @@
27122715
"execution_count": null,
27132716
"id": "cd413b0d",
27142717
"metadata": {},
2715-
"outputs": [],
2718+
"outputs": [
2719+
{
2720+
"name": "stdout",
2721+
"output_type": "stream",
2722+
"text": [
2723+
"(<function get>, '/hi', None, None, True, <function noop_body>)\n",
2724+
"(<function post>, '/hi', None, None, True, <function noop_body>)\n",
2725+
"(<function ho>, '/ho', None, None, True, <function noop_body>)\n",
2726+
"(<function show_host>, '/hostie', None, None, True, <function noop_body>)\n",
2727+
"(<function yoyo>, '/yoyo', None, None, True, <function noop_body>)\n",
2728+
"(<function index>, '/', None, None, True, <function noop_body>)\n"
2729+
]
2730+
}
2731+
],
27162732
"source": [
27172733
"app,cli,_ = get_cli(FastHTML())\n",
27182734
"ar.to_app(app)"
@@ -2804,7 +2820,20 @@
28042820
"execution_count": null,
28052821
"id": "77ce8548",
28062822
"metadata": {},
2807-
"outputs": [],
2823+
"outputs": [
2824+
{
2825+
"name": "stdout",
2826+
"output_type": "stream",
2827+
"text": [
2828+
"(<function get>, '/products/hi', None, None, True, <function noop_body>)\n",
2829+
"(<function post>, '/products/hi', None, None, True, <function noop_body>)\n",
2830+
"(<function ho>, '/products/ho', None, None, True, <function noop_body>)\n",
2831+
"(<function show_host>, '/products/hostie', None, None, True, <function noop_body>)\n",
2832+
"(<function yoyo>, '/products/yoyo', None, None, True, <function noop_body>)\n",
2833+
"(<function index>, '/products/', None, None, True, <function noop_body>)\n"
2834+
]
2835+
}
2836+
],
28082837
"source": [
28092838
"app,cli,_ = get_cli(FastHTML())\n",
28102839
"ar2.to_app(app)"
@@ -2949,7 +2978,7 @@
29492978
{
29502979
"data": {
29512980
"text/plain": [
2952-
"'Cookie was set at time 09:54:43.255286'"
2981+
"'Cookie was set at time 19:13:22.543473'"
29532982
]
29542983
},
29552984
"execution_count": null,

0 commit comments

Comments
 (0)