Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds "body_wrap" bits to APIRouter #612

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,11 @@ def __dir__(self): return list(self._funcs.keys())
# %% ../nbs/api/00_core.ipynb
class APIRouter:
"Add routes to an app"
def __init__(self, prefix:str|None=None):
def __init__(self, prefix:str|None=None, body_wrap=noop_body):
self.routes,self.wss = [],[]
self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability
self.prefix = prefix if prefix else ""
self.body_wrap = body_wrap

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

def __call__(self, path:str=None, methods=None, name=None, include_in_schema=True, body_wrap=noop_body):
def __call__(self, path:str=None, methods=None, name=None, include_in_schema=True, body_wrap=None):
"Add a route at `path`"
def f(func):
p = self.prefix + ("/" + ('' if path.__name__=='index' else func.__name__) if callable(path) else path)
wrapped = self._wrap_func(func, p)
self.routes.append((func, p, methods, name, include_in_schema, body_wrap))
self.routes.append((func, p, methods, name, include_in_schema, body_wrap or self.body_wrap))
return wrapped
return f(path) if callable(path) else f

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

def to_app(self, app):
"Add routes to `app`"
for args in self.routes: app._add_route(*args)
for args in self.routes:
print(args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(args)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah was hoping to get that fixed before you reviewed :). It should be good now.

app._add_route(*args)
for args in self.wss: app._add_ws(*args)

def ws(self, path:str, conn=None, disconn=None, name=None, middleware=None):
Expand Down
49 changes: 39 additions & 10 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, 10, 14, 0)"
"datetime.datetime(2024, 12, 20, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2423,13 +2423,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Set to 2024-12-10 09:54:42.331681\n"
"Set to 2024-12-20 19:13:21.676722\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2024-12-10 09:54:42.331681'"
"'Session time: 2024-12-20 19:13:21.676722'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2636,10 +2636,11 @@
"#| export\n",
"class APIRouter:\n",
" \"Add routes to an app\"\n",
" def __init__(self, prefix:str|None=None): \n",
" def __init__(self, prefix:str|None=None, body_wrap=noop_body): \n",
" self.routes,self.wss = [],[]\n",
" self.rt_funcs = RouteFuncs() # Store wrapped route function for discoverability\n",
" self.prefix = prefix if prefix else \"\"\n",
" self.body_wrap = body_wrap\n",
"\n",
" def _wrap_func(self, func, path=None):\n",
" name = func.__name__\n",
Expand All @@ -2649,12 +2650,12 @@
" if name not in all_meths: setattr(self.rt_funcs, name, wrapped)\n",
" return wrapped\n",
"\n",
" def __call__(self, path:str=None, methods=None, name=None, include_in_schema=True, body_wrap=noop_body):\n",
" def __call__(self, path:str=None, methods=None, name=None, include_in_schema=True, body_wrap=None):\n",
" \"Add a route at `path`\"\n",
" def f(func):\n",
" p = self.prefix + (\"/\" + ('' if path.__name__=='index' else func.__name__) if callable(path) else path)\n",
" wrapped = self._wrap_func(func, p)\n",
" self.routes.append((func, p, methods, name, include_in_schema, body_wrap))\n",
" self.routes.append((func, p, methods, name, include_in_schema, body_wrap or self.body_wrap))\n",
" return wrapped\n",
" return f(path) if callable(path) else f\n",
" \n",
Expand All @@ -2664,7 +2665,9 @@
"\n",
" def to_app(self, app):\n",
" \"Add routes to `app`\"\n",
" for args in self.routes: app._add_route(*args)\n",
" for args in self.routes: \n",
" print(args)\n",
" app._add_route(*args)\n",
" for args in self.wss: app._add_ws(*args)\n",
" \n",
" def ws(self, path:str, conn=None, disconn=None, name=None, middleware=None):\n",
Expand Down Expand Up @@ -2712,7 +2715,20 @@
"execution_count": null,
"id": "cd413b0d",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(<function get>, '/hi', None, None, True, <function noop_body>)\n",
"(<function post>, '/hi', None, None, True, <function noop_body>)\n",
"(<function ho>, '/ho', None, None, True, <function noop_body>)\n",
"(<function show_host>, '/hostie', None, None, True, <function noop_body>)\n",
"(<function yoyo>, '/yoyo', None, None, True, <function noop_body>)\n",
"(<function index>, '/', None, None, True, <function noop_body>)\n"
]
}
],
"source": [
"app,cli,_ = get_cli(FastHTML())\n",
"ar.to_app(app)"
Expand Down Expand Up @@ -2804,7 +2820,20 @@
"execution_count": null,
"id": "77ce8548",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(<function get>, '/products/hi', None, None, True, <function noop_body>)\n",
"(<function post>, '/products/hi', None, None, True, <function noop_body>)\n",
"(<function ho>, '/products/ho', None, None, True, <function noop_body>)\n",
"(<function show_host>, '/products/hostie', None, None, True, <function noop_body>)\n",
"(<function yoyo>, '/products/yoyo', None, None, True, <function noop_body>)\n",
"(<function index>, '/products/', None, None, True, <function noop_body>)\n"
]
}
],
"source": [
"app,cli,_ = get_cli(FastHTML())\n",
"ar2.to_app(app)"
Expand Down Expand Up @@ -2949,7 +2978,7 @@
{
"data": {
"text/plain": [
"'Cookie was set at time 09:54:43.255286'"
"'Cookie was set at time 19:13:22.543473'"
]
},
"execution_count": null,
Expand Down
Loading