Skip to content

Commit eb70e09

Browse files
YunchuWangpeterstone2017gavin-aguiar
authored
labelling http function type pystein (#137)
Co-authored-by: peterstone2017 <[email protected]> Co-authored-by: gavin-aguiar <[email protected]>
1 parent 15d679d commit eb70e09

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

azure/functions/decorators/function_app.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(self, func: Callable, script_file: str):
4545
self._trigger: Optional[Trigger] = None
4646
self._bindings: List[Binding] = []
4747
self.function_script_file = script_file
48+
self.http_type = 'function'
4849

4950
def add_binding(self, binding: Binding) -> None:
5051
"""Add a binding instance to the function.
@@ -85,6 +86,13 @@ def set_function_name(self, function_name: Optional[str] = None) -> None:
8586
if function_name:
8687
self._name = function_name
8788

89+
def set_http_type(self, http_type: str) -> None:
90+
"""Set or update the http type for the function if :param:`http_type`
91+
.
92+
:param http_type: Http function type.
93+
"""
94+
self.http_type = http_type
95+
8896
def get_trigger(self) -> Optional[Trigger]:
8997
"""Get attached trigger instance of the function.
9098
@@ -158,6 +166,11 @@ def configure_function_name(self, function_name: str) -> 'FunctionBuilder':
158166

159167
return self
160168

169+
def configure_http_type(self, http_type: str) -> 'FunctionBuilder':
170+
self._function.set_http_type(http_type)
171+
172+
return self
173+
161174
def add_trigger(self, trigger: Trigger) -> 'FunctionBuilder':
162175
self._function.add_trigger(trigger=trigger)
163176
return self
@@ -281,6 +294,23 @@ def decorator():
281294

282295
return wrap
283296

297+
def http_type(self, http_type: str) -> Callable:
298+
"""Set http type of the :class:`Function` object.
299+
300+
:param http_type: Http type of the function.
301+
:return: Decorator function.
302+
"""
303+
304+
@self._configure_function_builder
305+
def wrap(fb):
306+
def decorator():
307+
fb.configure_http_type(http_type)
308+
return fb
309+
310+
return decorator()
311+
312+
return wrap
313+
284314

285315
class HttpFunctionsAuthLevelMixin(ABC):
286316
"""Interface to extend for enabling function app level http
@@ -1638,7 +1668,8 @@ class ExternalHttpFunctionApp(FunctionRegister, TriggerApi, ABC):
16381668

16391669
def _add_http_app(self,
16401670
http_middleware: Union[
1641-
AsgiMiddleware, WsgiMiddleware]) -> None:
1671+
AsgiMiddleware, WsgiMiddleware],
1672+
http_type: str) -> None:
16421673
"""Add a Wsgi or Asgi app integrated http function.
16431674
16441675
:param http_middleware: :class:`AsgiMiddleware` or
@@ -1647,6 +1678,7 @@ def _add_http_app(self,
16471678
:return: None
16481679
"""
16491680

1681+
@self.http_type(http_type=http_type)
16501682
@self.route(methods=(method for method in HttpMethod),
16511683
auth_level=self.auth_level,
16521684
route="/{*route}")
@@ -1662,7 +1694,7 @@ def __init__(self, app,
16621694
:param app: asgi app object.
16631695
"""
16641696
super().__init__(auth_level=http_auth_level)
1665-
self._add_http_app(AsgiMiddleware(app))
1697+
self._add_http_app(AsgiMiddleware(app), 'asgi')
16661698

16671699

16681700
class WsgiFunctionApp(ExternalHttpFunctionApp):
@@ -1673,4 +1705,4 @@ def __init__(self, app,
16731705
:param app: wsgi app object.
16741706
"""
16751707
super().__init__(auth_level=http_auth_level)
1676-
self._add_http_app(WsgiMiddleware(app))
1708+
self._add_http_app(WsgiMiddleware(app), 'wsgi')

tests/decorators/test_function_app.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ def test_add_asgi(self, add_http_app_mock):
302302
self.assertIsInstance(add_http_app_mock.call_args[0][0],
303303
AsgiMiddleware)
304304

305+
self.assertEqual(add_http_app_mock.call_args[0][1], 'asgi')
306+
305307
@mock.patch('azure.functions.decorators.function_app.WsgiFunctionApp'
306308
'._add_http_app')
307309
def test_add_wsgi(self, add_http_app_mock):
@@ -311,6 +313,7 @@ def test_add_wsgi(self, add_http_app_mock):
311313
add_http_app_mock.assert_called_once()
312314
self.assertIsInstance(add_http_app_mock.call_args[0][0],
313315
WsgiMiddleware)
316+
self.assertEqual(add_http_app_mock.call_args[0][1], 'wsgi')
314317

315318
def test_add_http_app(self):
316319
app = AsgiFunctionApp(app=object())
@@ -392,6 +395,40 @@ def hello(name: str):
392395
self.assertEqual(app.get_functions()[0].get_trigger().auth_level,
393396
AuthLevel.ANONYMOUS)
394397

398+
def test_default_function_http_type(self):
399+
app = FunctionApp(http_auth_level=AuthLevel.ANONYMOUS)
400+
401+
@app.route("name")
402+
def hello(name: str):
403+
return "hello"
404+
405+
funcs = app.get_functions()
406+
self.assertEqual(len(funcs), 1)
407+
408+
func = funcs[0]
409+
self.assertEqual(func.http_type, 'function')
410+
411+
def test_set_http_type(self):
412+
app = FunctionApp(http_auth_level=AuthLevel.ANONYMOUS)
413+
414+
@app.route("name1")
415+
@app.http_type("dummy1")
416+
def hello(name: str):
417+
return "hello"
418+
419+
@app.route("name2")
420+
@app.http_type("dummy2")
421+
def hello2(name: str):
422+
return "hello"
423+
424+
funcs = app.get_functions()
425+
self.assertEqual(len(funcs), 2)
426+
427+
func1 = funcs[0]
428+
self.assertEqual(func1.http_type, 'dummy1')
429+
func2 = funcs[1]
430+
self.assertEqual(func2.http_type, 'dummy2')
431+
395432
def test_decorator_api_basic_props(self):
396433
class DummyFunctionApp(DecoratorApi):
397434
pass

0 commit comments

Comments
 (0)