Skip to content

Commit 7d1434f

Browse files
committed
refactor APIRouter and update router websockets to utilize prefix if supplied
1 parent 41de556 commit 7d1434f

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

fasthtml/core.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -667,19 +667,12 @@ class APIRouter:
667667
"Add routes to an app"
668668
def __init__(self, prefix:str|None=None):
669669
self.routes,self.wss = [],[]
670-
self.rt_funcs = RouteFuncs() # Store wrapped functions for discoverability
670+
self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability
671671
self.prefix = prefix if prefix else ""
672672

673673
def _wrap_func(self, func, path=None):
674674
name = func.__name__
675-
676-
class _lf:
677-
def __init__(s): update_wrapper(s, func)
678-
def __call__(s, *args, **kw): return func(*args, **kw)
679-
def to(s, **kw): return qp(path, **kw)
680-
def __str__(s): return path
681-
682-
wrapped = _lf()
675+
wrapped = _mk_locfunc(func, path)
683676
wrapped.__routename__ = name
684677
# If you are using the def get or def post method names, this approach is not supported
685678
if name not in all_meths: setattr(self.rt_funcs, name, wrapped)
@@ -701,7 +694,7 @@ def to_app(self, app):
701694

702695
def ws(self, path:str, conn=None, disconn=None, name=None, middleware=None):
703696
"Add a websocket route at `path`"
704-
def f(func=noop): return self.wss.append((func, path, conn, disconn, name, middleware))
697+
def f(func=noop): return self.wss.append((func, f"{self.prefix}{path}", conn, disconn, name, middleware))
705698
return f
706699

707700
# %% ../nbs/api/00_core.ipynb

nbs/api/00_core.ipynb

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2024, 12, 3, 14, 0)"
134+
"datetime.datetime(2024, 12, 4, 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-03 13:51:41.200169\n"
2426+
"Set to 2024-12-04 09:20:09.428221\n"
24272427
]
24282428
},
24292429
{
24302430
"data": {
24312431
"text/plain": [
2432-
"'Session time: 2024-12-03 13:51:41.200169'"
2432+
"'Session time: 2024-12-04 09:20:09.428221'"
24332433
]
24342434
},
24352435
"execution_count": null,
@@ -2637,19 +2637,12 @@
26372637
" \"Add routes to an app\"\n",
26382638
" def __init__(self, prefix:str|None=None): \n",
26392639
" self.routes,self.wss = [],[]\n",
2640-
" self.rt_funcs = RouteFuncs() # Store wrapped functions for discoverability\n",
2640+
" self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability\n",
26412641
" self.prefix = prefix if prefix else \"\"\n",
26422642
"\n",
26432643
" def _wrap_func(self, func, path=None):\n",
26442644
" name = func.__name__\n",
2645-
" \n",
2646-
" class _lf:\n",
2647-
" def __init__(s): update_wrapper(s, func)\n",
2648-
" def __call__(s, *args, **kw): return func(*args, **kw)\n",
2649-
" def to(s, **kw): return qp(path, **kw)\n",
2650-
" def __str__(s): return path\n",
2651-
" \n",
2652-
" wrapped = _lf()\n",
2645+
" wrapped = _mk_locfunc(func, path)\n",
26532646
" wrapped.__routename__ = name\n",
26542647
" # If you are using the def get or def post method names, this approach is not supported\n",
26552648
" if name not in all_meths: setattr(self.rt_funcs, name, wrapped)\n",
@@ -2671,7 +2664,7 @@
26712664
" \n",
26722665
" def ws(self, path:str, conn=None, disconn=None, name=None, middleware=None):\n",
26732666
" \"Add a websocket route at `path`\"\n",
2674-
" def f(func=noop): return self.wss.append((func, path, conn, disconn, name, middleware))\n",
2667+
" def f(func=noop): return self.wss.append((func, f\"{self.prefix}{path}\", conn, disconn, name, middleware))\n",
26752668
" return f"
26762669
]
26772670
},
@@ -2712,24 +2705,24 @@
27122705
{
27132706
"cell_type": "code",
27142707
"execution_count": null,
2715-
"id": "8c265ff8",
2708+
"id": "cd413b0d",
27162709
"metadata": {},
27172710
"outputs": [],
27182711
"source": [
2719-
"assert str(ar.rt_funcs.index) == '/'\n",
2720-
"assert str(yoyo) == '/yoyo'\n",
2721-
"assert \"get\" not in ar.rt_funcs._funcs.keys()"
2712+
"app,cli,_ = get_cli(FastHTML())\n",
2713+
"ar.to_app(app)"
27222714
]
27232715
},
27242716
{
27252717
"cell_type": "code",
27262718
"execution_count": null,
2727-
"id": "cd413b0d",
2719+
"id": "8c265ff8",
27282720
"metadata": {},
27292721
"outputs": [],
27302722
"source": [
2731-
"app,cli,_ = get_cli(FastHTML())\n",
2732-
"ar.to_app(app)"
2723+
"assert str(ar.rt_funcs.index) == '/'\n",
2724+
"assert str(yoyo) == '/yoyo'\n",
2725+
"assert \"get\" not in ar.rt_funcs._funcs.keys()"
27332726
]
27342727
},
27352728
{
@@ -2806,6 +2799,18 @@
28062799
"ar2.to_app(app)"
28072800
]
28082801
},
2802+
{
2803+
"cell_type": "code",
2804+
"execution_count": null,
2805+
"id": "f1fc8425",
2806+
"metadata": {},
2807+
"outputs": [],
2808+
"source": [
2809+
"assert str(ar2.rt_funcs.index) == '/products/'\n",
2810+
"assert str(yoyo) == '/products/yoyo'\n",
2811+
"assert \"get\" not in ar2.rt_funcs._funcs.keys()"
2812+
]
2813+
},
28092814
{
28102815
"cell_type": "code",
28112816
"execution_count": null,
@@ -2825,13 +2830,14 @@
28252830
{
28262831
"cell_type": "code",
28272832
"execution_count": null,
2828-
"id": "f1fc8425",
2833+
"id": "e38d99cf",
28292834
"metadata": {},
28302835
"outputs": [],
28312836
"source": [
2832-
"assert str(ar2.rt_funcs.index) == '/products/'\n",
2833-
"assert str(yoyo) == '/products/yoyo'\n",
2834-
"assert \"get\" not in ar2.rt_funcs._funcs.keys()"
2837+
"with cli.websocket_connect('/products/ws') as ws:\n",
2838+
" ws.send_text('{\"msg\":\"Hi!\"}')\n",
2839+
" data = ws.receive_text()\n",
2840+
" assert data == 'Message text was: Hi!'"
28352841
]
28362842
},
28372843
{
@@ -2927,7 +2933,7 @@
29272933
{
29282934
"data": {
29292935
"text/plain": [
2930-
"'Cookie was set at time 13:51:41.310412'"
2936+
"'Cookie was set at time 09:20:09.542569'"
29312937
]
29322938
},
29332939
"execution_count": null,

0 commit comments

Comments
 (0)