Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding ability to invoke functions directly #238

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
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
26 changes: 22 additions & 4 deletions azure/functions/decorators/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ def __init__(self, func: Callable[..., Any], script_file: str):
self.http_type = 'function'
self._is_http_function = False

def __str__(self):
"""Return the function.json representation of the function"""
return self.get_function_json()

def __call__(self, *args, **kwargs):
"""This would allow the Function object to be directly callable and runnable
directly using the interpreter locally.

Example:
@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
return "Hello, World!"

print(http_trigger(None))

➜ python function_app.py
Hello, World!
"""
return self._func(*args, **kwargs)

def add_binding(self, binding: Binding) -> None:
"""Add a binding instance to the function.

Expand Down Expand Up @@ -201,17 +221,15 @@ def get_function_json(self) -> str:
"""
return json.dumps(self.get_dict_repr(), cls=StringifyEnumJsonEncoder)

def __str__(self):
return self.get_function_json()


class FunctionBuilder(object):

def __init__(self, func, function_script_file):
self._function = Function(func, function_script_file)

def __call__(self, *args, **kwargs):
pass
"""Call the Function object directly"""
return self._function(*args, **kwargs)

def configure_http_type(self, http_type: str) -> 'FunctionBuilder':
self._function.set_http_type(http_type)
Expand Down
16 changes: 16 additions & 0 deletions tests/decorators/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,22 @@ def test_blueprint_same_function_and_method_name(name: str):
" Please change @app.function_name() or the function"
" method name to be unique.")

def test_user_function_is_directly_callable_no_args(self):
def test_validate_function_working_no_args():
return "dummy"

self.dummy = test_validate_function_working_no_args
self.fb = FunctionBuilder(self.dummy, "dummy.py")
self.assertEqual(self.fb(), "dummy")

def test_user_function_is_directly_callable_args(self):
def test_validate_function_working_sum_args(arg1: int, arg2: int):
return arg1 + arg2

self.dummy = test_validate_function_working_sum_args
self.fb = FunctionBuilder(self.dummy, "dummy.py")
self.assertEqual(self.fb(1, 2), 3)


class TestScaffold(unittest.TestCase):
def setUp(self):
Expand Down