Skip to content

Commit

Permalink
Merge pull request #612 from AnswerDotAI/apirouter-refactor
Browse files Browse the repository at this point in the history
Adds "body_wrap" bits to APIRouter
  • Loading branch information
jph00 authored Dec 21, 2024
2 parents 81cf119 + a6adf30 commit 065965a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 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 Down
15 changes: 8 additions & 7 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:21:33.748637\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2024-12-10 09:54:42.331681'"
"'Session time: 2024-12-20 19:21:33.748637'"
]
},
"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 Down Expand Up @@ -2949,7 +2950,7 @@
{
"data": {
"text/plain": [
"'Cookie was set at time 09:54:43.255286'"
"'Cookie was set at time 19:21:34.644743'"
]
},
"execution_count": null,
Expand Down

0 comments on commit 065965a

Please sign in to comment.