From 5f4668ce68af22428b7acc9741710fc30846a935 Mon Sep 17 00:00:00 2001 From: peterstone2017 Date: Mon, 27 Jun 2022 15:34:36 -0500 Subject: [PATCH] labelling http function type pystein --- azure/functions/decorators/function_app.py | 38 ++++++++++++++++++++-- tests/decorators/test_function_app.py | 37 +++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/azure/functions/decorators/function_app.py b/azure/functions/decorators/function_app.py index ad185a7c..cfe36ed3 100644 --- a/azure/functions/decorators/function_app.py +++ b/azure/functions/decorators/function_app.py @@ -45,6 +45,7 @@ def __init__(self, func: Callable, script_file: str): self._trigger: Optional[Trigger] = None self._bindings: List[Binding] = [] self.function_script_file = script_file + self.http_type = 'function' def add_binding(self, binding: Binding) -> None: """Add a binding instance to the function. @@ -85,6 +86,13 @@ def set_function_name(self, function_name: Optional[str] = None) -> None: if function_name: self._name = function_name + def set_http_type(self, http_type: str) -> None: + """Set or update the http type for the function if :param:`http_type` + . + :param http_type: Http function type. + """ + self.http_type = http_type + def get_trigger(self) -> Optional[Trigger]: """Get attached trigger instance of the function. @@ -158,6 +166,11 @@ def configure_function_name(self, function_name: str) -> 'FunctionBuilder': return self + def configure_http_type(self, http_type: str) -> 'FunctionBuilder': + self._function.set_http_type(http_type) + + return self + def add_trigger(self, trigger: Trigger) -> 'FunctionBuilder': self._function.add_trigger(trigger=trigger) return self @@ -281,6 +294,23 @@ def decorator(): return wrap + def http_type(self, http_type: str) -> Callable: + """Set http type of the :class:`Function` object. + + :param http_type: Http type of the function. + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.configure_http_type(http_type) + return fb + + return decorator() + + return wrap + class HttpFunctionsAuthLevelMixin(ABC): """Interface to extend for enabling function app level http @@ -1638,7 +1668,8 @@ class ExternalHttpFunctionApp(FunctionRegister, TriggerApi, ABC): def _add_http_app(self, http_middleware: Union[ - AsgiMiddleware, WsgiMiddleware]) -> None: + AsgiMiddleware, WsgiMiddleware], + http_type: str) -> None: """Add a Wsgi or Asgi app integrated http function. :param http_middleware: :class:`AsgiMiddleware` or @@ -1647,6 +1678,7 @@ def _add_http_app(self, :return: None """ + @self.http_type(http_type=http_type) @self.route(methods=(method for method in HttpMethod), auth_level=self.auth_level, route="/{*route}") @@ -1662,7 +1694,7 @@ def __init__(self, app, :param app: asgi app object. """ super().__init__(auth_level=http_auth_level) - self._add_http_app(AsgiMiddleware(app)) + self._add_http_app(AsgiMiddleware(app), 'asgi') class WsgiFunctionApp(ExternalHttpFunctionApp): @@ -1673,4 +1705,4 @@ def __init__(self, app, :param app: wsgi app object. """ super().__init__(auth_level=http_auth_level) - self._add_http_app(WsgiMiddleware(app)) + self._add_http_app(WsgiMiddleware(app), 'wsgi') diff --git a/tests/decorators/test_function_app.py b/tests/decorators/test_function_app.py index 8125fcdd..b799a7fa 100644 --- a/tests/decorators/test_function_app.py +++ b/tests/decorators/test_function_app.py @@ -302,6 +302,8 @@ def test_add_asgi(self, add_http_app_mock): self.assertIsInstance(add_http_app_mock.call_args[0][0], AsgiMiddleware) + self.assertEqual(add_http_app_mock.call_args[0][1], 'asgi') + @mock.patch('azure.functions.decorators.function_app.WsgiFunctionApp' '._add_http_app') def test_add_wsgi(self, add_http_app_mock): @@ -311,6 +313,7 @@ def test_add_wsgi(self, add_http_app_mock): add_http_app_mock.assert_called_once() self.assertIsInstance(add_http_app_mock.call_args[0][0], WsgiMiddleware) + self.assertEqual(add_http_app_mock.call_args[0][1], 'wsgi') def test_add_http_app(self): app = AsgiFunctionApp(app=object()) @@ -392,6 +395,40 @@ def hello(name: str): self.assertEqual(app.get_functions()[0].get_trigger().auth_level, AuthLevel.ANONYMOUS) + def test_default_function_http_type(self): + app = FunctionApp(http_auth_level=AuthLevel.ANONYMOUS) + + @app.route("name") + def hello(name: str): + return "hello" + + funcs = app.get_functions() + self.assertEqual(len(funcs), 1) + + func = funcs[0] + self.assertEqual(func.http_type, 'function') + + def test_set_http_type(self): + app = FunctionApp(http_auth_level=AuthLevel.ANONYMOUS) + + @app.route("name1") + @app.http_type("dummy1") + def hello(name: str): + return "hello" + + @app.route("name2") + @app.http_type("dummy2") + def hello2(name: str): + return "hello" + + funcs = app.get_functions() + self.assertEqual(len(funcs), 2) + + func1 = funcs[0] + self.assertEqual(func1.http_type, 'dummy1') + func2 = funcs[1] + self.assertEqual(func2.http_type, 'dummy2') + def test_decorator_api_basic_props(self): class DummyFunctionApp(DecoratorApi): pass