Skip to content
Merged
Changes from all commits
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
18 changes: 18 additions & 0 deletions nc_py_api/ex_app/integration_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def set_handlers(
default_init: bool = True,
models_to_fetch: dict[str, dict] | None = None,
map_app_static: bool = True,
trigger_handler: typing.Callable[[str], typing.Awaitable[None] | None] | None = None,
):
"""Defines handlers for the application.

Expand Down Expand Up @@ -93,6 +94,8 @@ def set_handlers(
:param map_app_static: Should be folders ``js``, ``css``, ``l10n``, ``img`` automatically mounted in FastAPI or not.

.. note:: First, presence of these directories in the current working dir is checked, then one directory higher.

:param trigger_handler: callback that is called for task processing `trigger` events with the id of the provider.
"""
if models_to_fetch is not None and default_init is False:
raise ValueError("`models_to_fetch` can be defined only with `default_init`=True.")
Expand Down Expand Up @@ -125,6 +128,21 @@ async def init_callback(b_tasks: BackgroundTasks, nc: typing.Annotated[Nextcloud
if map_app_static:
__map_app_static_folders(fast_api_app)

if trigger_handler:
if asyncio.iscoroutinefunction(trigger_handler):

@fast_api_app.post("/trigger")
async def trigger_callback(providerId: str): # pylint: disable=invalid-name
await trigger_handler(providerId)
return JSONResponse(content={})

else:

@fast_api_app.post("/trigger")
def trigger_callback(providerId: str): # pylint: disable=invalid-name
trigger_handler(providerId)
return JSONResponse(content={})


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