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

labelling http function type pystein #137

Merged
merged 2 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 35 additions & 3 deletions azure/functions/decorators/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}")
Expand All @@ -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):
Expand All @@ -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')
37 changes: 37 additions & 0 deletions tests/decorators/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down