From 070d3399b3edabef2af3b6426c633e324e70d25e Mon Sep 17 00:00:00 2001 From: Michel Hua Date: Wed, 1 May 2024 11:25:20 +0200 Subject: [PATCH 1/3] add configurable prefix --- azure/functions/decorators/function_app.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/azure/functions/decorators/function_app.py b/azure/functions/decorators/function_app.py index 172e2b05..c429d150 100644 --- a/azure/functions/decorators/function_app.py +++ b/azure/functions/decorators/function_app.py @@ -2886,7 +2886,8 @@ def _add_http_app(self, class AsgiFunctionApp(ExternalHttpFunctionApp): def __init__(self, app, - http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION): + http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION, + prefix: str = ""): """Constructor of :class:`AsgiFunctionApp` object. :param app: asgi app object. @@ -2898,6 +2899,7 @@ def __init__(self, app, self.middleware = AsgiMiddleware(app) self._add_http_app(self.middleware) self.startup_task_done = False + self.prefix = prefix def __del__(self): if self.startup_task_done: @@ -2922,7 +2924,7 @@ def _add_http_app(self, @self.http_type(http_type='asgi') @self.route(methods=(method for method in HttpMethod), auth_level=self.auth_level, - route="/{*route}") + route=self.prefix + "/{*route}") async def http_app_func(req: HttpRequest, context: Context): if not self.startup_task_done: success = await asgi_middleware.notify_startup() @@ -2935,13 +2937,15 @@ async def http_app_func(req: HttpRequest, context: Context): class WsgiFunctionApp(ExternalHttpFunctionApp): def __init__(self, app, - http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION): + http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION, + prefix = ""): """Constructor of :class:`WsgiFunctionApp` object. :param app: wsgi app object. """ super().__init__(auth_level=http_auth_level) self._add_http_app(WsgiMiddleware(app)) + self.prefix = prefix def _add_http_app(self, http_middleware: Union[ @@ -2962,6 +2966,6 @@ def _add_http_app(self, @self.http_type(http_type='wsgi') @self.route(methods=(method for method in HttpMethod), auth_level=self.auth_level, - route="/{*route}") + route=self.prefix + "/{*route}") def http_app_func(req: HttpRequest, context: Context): return wsgi_middleware.handle(req, context) From 19a45bcc7bd4833f0bf5d0334cf73fa4e0e24c15 Mon Sep 17 00:00:00 2001 From: Michel Hua Date: Wed, 1 May 2024 11:38:30 +0200 Subject: [PATCH 2/3] Add unit test --- tests/decorators/test_function_app.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/decorators/test_function_app.py b/tests/decorators/test_function_app.py index 2f687262..8f19f7bd 100644 --- a/tests/decorators/test_function_app.py +++ b/tests/decorators/test_function_app.py @@ -558,6 +558,10 @@ def test_asgi_function_app_custom(self): http_auth_level=AuthLevel.ANONYMOUS) self.assertEqual(app.auth_level, AuthLevel.ANONYMOUS) + def test_asgi_function_app_prefix(self): + app = AsgiFunctionApp(app=object(), prefix="v1") + self.assertEqual(app.prefix, "v1") + def test_asgi_function_app_is_http_function(self): app = AsgiFunctionApp(app=object()) funcs = app.get_functions() @@ -574,6 +578,10 @@ def test_wsgi_function_app_custom(self): http_auth_level=AuthLevel.ANONYMOUS) self.assertEqual(app.auth_level, AuthLevel.ANONYMOUS) + def test_wsgi_function_app_prefix(self): + app = WsgiFunctionApp(app=object(), prefix="v1") + self.assertEqual(app.prefix, "v1") + def test_wsgi_function_app_is_http_function(self): app = WsgiFunctionApp(app=object()) funcs = app.get_functions() From 3b9ff9372e975e6cd76020c491b9c4182dedf3b5 Mon Sep 17 00:00:00 2001 From: Michel Hua Date: Wed, 1 May 2024 11:47:49 +0200 Subject: [PATCH 3/3] update ProgModelSpec --- docs/ProgModelSpec.pyi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/ProgModelSpec.pyi b/docs/ProgModelSpec.pyi index a01d8168..624c49ad 100644 --- a/docs/ProgModelSpec.pyi +++ b/docs/ProgModelSpec.pyi @@ -1241,7 +1241,8 @@ class ThirdPartyHttpFunctionApp(FunctionRegister, TriggerApi, ABC): class AsgiFunctionApp(ThirdPartyHttpFunctionApp): def __init__(self, app, - http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION): + http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION, + prefix: str = ""): """Constructor of :class:`AsgiFunctionApp` object. :param app: asgi app object. @@ -1251,7 +1252,8 @@ class AsgiFunctionApp(ThirdPartyHttpFunctionApp): class WsgiFunctionApp(ThirdPartyHttpFunctionApp): def __init__(self, app, - http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION): + http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION, + prefix: str = ""): """Constructor of :class:`WsgiFunctionApp` object. :param app: wsgi app object.