Skip to content

Commit

Permalink
Merge pull request #597 from AnswerDotAI/apirouter-refactor
Browse files Browse the repository at this point in the history
Refactor APIRouter and update documentation
  • Loading branch information
jph00 authored Dec 5, 2024
2 parents 41de556 + b921512 commit 72dbc04
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 64 deletions.
13 changes: 3 additions & 10 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,19 +667,12 @@ class APIRouter:
"Add routes to an app"
def __init__(self, prefix:str|None=None):
self.routes,self.wss = [],[]
self.rt_funcs = RouteFuncs() # Store wrapped functions for discoverability
self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability
self.prefix = prefix if prefix else ""

def _wrap_func(self, func, path=None):
name = func.__name__

class _lf:
def __init__(s): update_wrapper(s, func)
def __call__(s, *args, **kw): return func(*args, **kw)
def to(s, **kw): return qp(path, **kw)
def __str__(s): return path

wrapped = _lf()
wrapped = _mk_locfunc(func, path)
wrapped.__routename__ = name
# If you are using the def get or def post method names, this approach is not supported
if name not in all_meths: setattr(self.rt_funcs, name, wrapped)
Expand All @@ -701,7 +694,7 @@ def to_app(self, app):

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

# %% ../nbs/api/00_core.ipynb
Expand Down
56 changes: 31 additions & 25 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
{
"data": {
"text/plain": [
"datetime.datetime(2024, 12, 3, 14, 0)"
"datetime.datetime(2024, 12, 4, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2423,13 +2423,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Set to 2024-12-03 13:51:41.200169\n"
"Set to 2024-12-04 09:20:09.428221\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2024-12-03 13:51:41.200169'"
"'Session time: 2024-12-04 09:20:09.428221'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2637,19 +2637,12 @@
" \"Add routes to an app\"\n",
" def __init__(self, prefix:str|None=None): \n",
" self.routes,self.wss = [],[]\n",
" self.rt_funcs = RouteFuncs() # Store wrapped functions for discoverability\n",
" self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability\n",
" self.prefix = prefix if prefix else \"\"\n",
"\n",
" def _wrap_func(self, func, path=None):\n",
" name = func.__name__\n",
" \n",
" class _lf:\n",
" def __init__(s): update_wrapper(s, func)\n",
" def __call__(s, *args, **kw): return func(*args, **kw)\n",
" def to(s, **kw): return qp(path, **kw)\n",
" def __str__(s): return path\n",
" \n",
" wrapped = _lf()\n",
" wrapped = _mk_locfunc(func, path)\n",
" wrapped.__routename__ = name\n",
" # If you are using the def get or def post method names, this approach is not supported\n",
" if name not in all_meths: setattr(self.rt_funcs, name, wrapped)\n",
Expand All @@ -2671,7 +2664,7 @@
" \n",
" def ws(self, path:str, conn=None, disconn=None, name=None, middleware=None):\n",
" \"Add a websocket route at `path`\"\n",
" def f(func=noop): return self.wss.append((func, path, conn, disconn, name, middleware))\n",
" def f(func=noop): return self.wss.append((func, f\"{self.prefix}{path}\", conn, disconn, name, middleware))\n",
" return f"
]
},
Expand Down Expand Up @@ -2712,24 +2705,24 @@
{
"cell_type": "code",
"execution_count": null,
"id": "8c265ff8",
"id": "cd413b0d",
"metadata": {},
"outputs": [],
"source": [
"assert str(ar.rt_funcs.index) == '/'\n",
"assert str(yoyo) == '/yoyo'\n",
"assert \"get\" not in ar.rt_funcs._funcs.keys()"
"app,cli,_ = get_cli(FastHTML())\n",
"ar.to_app(app)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd413b0d",
"id": "8c265ff8",
"metadata": {},
"outputs": [],
"source": [
"app,cli,_ = get_cli(FastHTML())\n",
"ar.to_app(app)"
"assert str(ar.rt_funcs.index) == '/'\n",
"assert str(yoyo) == '/yoyo'\n",
"assert \"get\" not in ar.rt_funcs._funcs.keys()"
]
},
{
Expand Down Expand Up @@ -2806,6 +2799,18 @@
"ar2.to_app(app)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1fc8425",
"metadata": {},
"outputs": [],
"source": [
"assert str(ar2.rt_funcs.index) == '/products/'\n",
"assert str(yoyo) == '/products/yoyo'\n",
"assert \"get\" not in ar2.rt_funcs._funcs.keys()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -2825,13 +2830,14 @@
{
"cell_type": "code",
"execution_count": null,
"id": "f1fc8425",
"id": "e38d99cf",
"metadata": {},
"outputs": [],
"source": [
"assert str(ar2.rt_funcs.index) == '/products/'\n",
"assert str(yoyo) == '/products/yoyo'\n",
"assert \"get\" not in ar2.rt_funcs._funcs.keys()"
"with cli.websocket_connect('/products/ws') as ws:\n",
" ws.send_text('{\"msg\":\"Hi!\"}')\n",
" data = ws.receive_text()\n",
" assert data == 'Message text was: Hi!'"
]
},
{
Expand Down Expand Up @@ -2927,7 +2933,7 @@
{
"data": {
"text/plain": [
"'Cookie was set at time 13:51:41.310412'"
"'Cookie was set at time 09:20:09.542569'"
]
},
"execution_count": null,
Expand Down
Loading

0 comments on commit 72dbc04

Please sign in to comment.