-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhooks.py
More file actions
38 lines (30 loc) · 1.14 KB
/
hooks.py
File metadata and controls
38 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import Any, Awaitable, Callable
from iii import ApiRequest, ApiResponse, FunctionInfo, get_context
from .iii import get_iii
def use_api(
config: dict[str, Any],
handler: Callable[[ApiRequest[Any], Any], Awaitable[ApiResponse[Any]]],
) -> None:
iii = get_iii()
api_path = config["api_path"]
http_method = config["http_method"]
function_id = f"api.{http_method.lower()}.{api_path}"
async def wrapped(data: Any) -> dict[str, Any]:
req = ApiRequest(**data) if isinstance(data, dict) else data
ctx = get_context()
result = await handler(req, ctx)
return result.model_dump(by_alias=True)
iii.register_function(function_id, wrapped)
iii.register_trigger(
type="http",
function_id=function_id,
config={
"api_path": api_path,
"http_method": http_method,
"description": config.get("description"),
"metadata": config.get("metadata"),
},
)
def use_functions_available(callback: Callable[[list[FunctionInfo]], None]) -> Callable[[], None]:
iii = get_iii()
return iii.on_functions_available(callback)