@@ -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
129147def __map_app_static_folders (fast_api_app : FastAPI ):
130148 """Function to mount all necessary static folders to FastAPI."""
0 commit comments