Forum Discussion
call function in user data functions
- 9 months ago
use this code (tested & working):
import datetime import fabric.functions as fn import logging udf = fn.UserDataFunctions() async def get_environment_local(varLib: fn.FabricVariablesClient) -> str: variables = varLib.getVariables() # sync call return variables["omgeving"] async def get_result_local(varLib: fn.FabricVariablesClient) -> str: return await get_environment_local(varLib) @udf.connection(argName="varLib", alias="varlib") @udf.function() async def hello_fabric(name: str, varLib: fn.FabricVariablesClient) -> str: logging.info("Running hello_fabric") omgeving = await get_result_local(varLib) return ( f"Welcome to Fabric Functions, {name}. " f"Omgeving: {omgeving}. " f"Time: {datetime.datetime.now()}" )
When a function has @udf.function(), it becomes a remote Fabric function, not a normal Python function. Calling it directly (await get_environment(varLib)) bypasses Fabric's runtime, so the injected connection becomes None, and Fabric later tries to access None.headers, causing the NoneType error. Make helper functions local (not decorated) so they behave like real Python functions.
Therefore I modified the code to remove the @udf.function() decorators from the helper functions and turn them into normal local Python functions, so they no longer get wrapped by the Fabric runtime and can be safely called from inside the main UDF with a valid varLib connection.
Hi bruinsmm
(Edited my reply to fix the code, due the error mentioned here)
To get the correct value, modify get_result so it awaits get_environment. In Python with async/await, this means:
@udf.connection(argName="varLib", alias="varlib")
@udf.function()
async def get_environment(varLib: fn.FabricVariablesClient) -> str:
variables = varLib.getVariables()
omgeving = variables["omgeving"]
return omgeving
@udf.function()
async def get_result() -> str:
result = await get_environment(varLib) # <--- FIX
return result
Hope that helps
Onur
šIf this post helped you, feel free to give it some Kudos! š
ā And if it answered your question, please mark it as the accepted solution.