Skip to content

Commit 1ca4145

Browse files
committed
wip
1 parent d4f635e commit 1ca4145

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

piccolo_api/fastapi/endpoints.py

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
"""
23
Enhancing Piccolo integration with FastAPI.
34
"""
@@ -8,7 +9,7 @@
89
from collections import defaultdict
910
from decimal import Decimal
1011
from enum import Enum
11-
from inspect import Parameter, Signature
12+
from inspect import Parameter, Signature, iscoroutinefunction
1213

1314
from fastapi import APIRouter, FastAPI, Request
1415
from fastapi.params import Query
@@ -76,12 +77,13 @@ class ReferencesModel(BaseModel):
7677
references: t.List[ReferenceModel]
7778

7879

79-
class CallbackModel(BaseModel):
80-
callback_name: str
80+
class ActionsModel(BaseModel):
81+
action_id: int
82+
action_name: str
8183

8284

83-
class ExecuteCallbackModel(BaseModel):
84-
callback_response: str
85+
class ExecuteActionsModel(BaseModel):
86+
actions_response: str
8587

8688

8789
class FastAPIWrapper:
@@ -103,8 +105,8 @@ class FastAPIWrapper:
103105
and ``allow_bulk_delete``.
104106
:param fastapi_kwargs:
105107
Specifies the extra kwargs to pass to FastAPI's ``add_api_route``.
106-
:param callback
107-
Callback fn passed in via create_admin TableConfig
108+
:param actions
109+
List of action handlers passed in via create_admin TableConfig
108110
109111
"""
110112

@@ -114,15 +116,16 @@ def __init__(
114116
fastapi_app: t.Union[FastAPI, APIRouter],
115117
piccolo_crud: PiccoloCRUD,
116118
fastapi_kwargs: t.Optional[FastAPIKwargs] = None,
117-
callback: t.Optional[t.Callable] = None,
119+
actions: t.Optional[t.List[t.Callable]] = None,
118120
):
119121
fastapi_kwargs = fastapi_kwargs or FastAPIKwargs()
120122

121123
self.root_url = root_url
122124
self.fastapi_app = fastapi_app
123125
self.piccolo_crud = piccolo_crud
124126
self.fastapi_kwargs = fastapi_kwargs
125-
self.callback = callback
127+
self.actions = actions
128+
self._actions_map = []
126129

127130
self.ModelOut = piccolo_crud.pydantic_model_output
128131
self.ModelIn = piccolo_crud.pydantic_model
@@ -257,55 +260,69 @@ async def references(request: Request):
257260
)
258261

259262
#######################################################################
260-
# Root - Callback
263+
# Root - Actions
261264

262-
async def get_callback(request: Request) -> JSONResponse:
265+
async def get_actions(request: Request) -> JSONResponse:
263266
"""
264-
Return the name of the callback function
267+
Return the names of the actions
265268
This is specified on the table config
266269
"""
267-
if self.callback:
268-
return JSONResponse(
269-
f"Configured callback for table: {self.callback.__name__}"
270-
)
270+
if self.actions:
271+
actions_list = []
272+
for action in self._actions_map:
273+
actions_list.append({"action_id": action['action_id'], "action_name": action['action_handler'].__name__})
274+
275+
print(actions_list)
276+
return actions_list
271277
else:
272278
return JSONResponse(
273-
content="No callback configured", status_code=500
279+
content="No actions configured", status_code=500
274280
)
275281

276-
if self.callback:
282+
if self.actions:
283+
for action_id, action in enumerate(actions):
284+
self._actions_map.append({"action_id": action_id, "action_handler": action})
285+
277286
fastapi_app.add_api_route(
278-
path=self.join_urls(root_url, "/callback"),
279-
endpoint=get_callback,
287+
path=self.join_urls(root_url, "/actions"),
288+
endpoint=get_actions,
280289
methods=["GET"],
281-
response_model=CallbackModel,
290+
response_model=t.List[ActionsModel],
282291
**fastapi_kwargs.get_kwargs("get"),
283292
)
284293

285294
#######################################################################
286-
# Root - Callback execute
295+
# Root - Actions execute
287296

288-
async def run_callback(request: Request) -> JSONResponse:
297+
async def run_action(request: Request) -> JSONResponse:
289298
"""
290-
Execute the configured callback for this table
299+
Execute the configured actions for this table
291300
:param request_params:
292301
The request params must contain the arguments
293-
required by the callback
302+
required by the actions handler function
294303
"""
295-
if self.callback:
296-
return await self.callback(request_params=request.json())
304+
action_id = request.path_params.get("action_id", None)
305+
if self._actions_map and action_id:
306+
for action in self._actions_map:
307+
if action['action_id'] == int(action_id):
308+
action_handler = action['action_handler']
309+
req_data = await request.json()
310+
if iscoroutinefunction(action_handler):
311+
return await action_handler(request_params=req_data)
312+
else:
313+
return action_handler(request_params=req_data)
297314
else:
298315
return JSONResponse(
299-
content="No callback configured", status_code=500
316+
content="No actions registered", status_code=500
300317
)
301318

302-
if self.callback:
319+
if self.actions:
303320
fastapi_app.add_api_route(
304-
path=self.join_urls(root_url, "/callback/execute"),
305-
endpoint=run_callback,
321+
path=self.join_urls(root_url, "/actions/{action_id:str}/execute"),
322+
endpoint=run_action,
306323
methods=["POST"],
307-
response_model=ExecuteCallbackModel,
308-
**fastapi_kwargs.get_kwargs("post"),
324+
response_model=ExecuteActionsModel,
325+
**fastapi_kwargs.get_kwargs("POST"),
309326
)
310327
#######################################################################
311328
# Root - DELETE

0 commit comments

Comments
 (0)