Skip to content

Commit da0a3d5

Browse files
feat(TaskProcessing): Add trigger_handler to set_handlers() (#382)
See nextcloud/app_api#683 for the corresponding AppAPI PR. Changes proposed in this pull request: * Add a `trigger_handler` param to set_handlers() that will be called when the /trigger endpoint is requested by AppAPI. --------- Signed-off-by: Marcel Klehr <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 816e4e0 commit da0a3d5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

nc_py_api/ex_app/integration_fastapi.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def set_handlers(
6161
default_init: bool = True,
6262
models_to_fetch: dict[str, dict] | None = None,
6363
map_app_static: bool = True,
64+
trigger_handler: typing.Callable[[str], typing.Awaitable[None] | None] | None = None,
6465
):
6566
"""Defines handlers for the application.
6667
@@ -93,6 +94,8 @@ def set_handlers(
9394
:param map_app_static: Should be folders ``js``, ``css``, ``l10n``, ``img`` automatically mounted in FastAPI or not.
9495
9596
.. note:: First, presence of these directories in the current working dir is checked, then one directory higher.
97+
98+
:param trigger_handler: callback that is called for task processing `trigger` events with the id of the provider.
9699
"""
97100
if models_to_fetch is not None and default_init is False:
98101
raise ValueError("`models_to_fetch` can be defined only with `default_init`=True.")
@@ -125,6 +128,21 @@ async def init_callback(b_tasks: BackgroundTasks, nc: typing.Annotated[Nextcloud
125128
if map_app_static:
126129
__map_app_static_folders(fast_api_app)
127130

131+
if trigger_handler:
132+
if asyncio.iscoroutinefunction(trigger_handler):
133+
134+
@fast_api_app.post("/trigger")
135+
async def trigger_callback(providerId: str): # pylint: disable=invalid-name
136+
await trigger_handler(providerId)
137+
return JSONResponse(content={})
138+
139+
else:
140+
141+
@fast_api_app.post("/trigger")
142+
def trigger_callback(providerId: str): # pylint: disable=invalid-name
143+
trigger_handler(providerId)
144+
return JSONResponse(content={})
145+
128146

129147
def __map_app_static_folders(fast_api_app: FastAPI):
130148
"""Function to mount all necessary static folders to FastAPI."""

0 commit comments

Comments
 (0)