Skip to content

Add configurable prefix for A/WsgiFunctionApp #215

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

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions azure/functions/decorators/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3730,7 +3730,8 @@ def _add_http_app(self,
class AsgiFunctionApp(ExternalHttpFunctionApp):
def __init__(self, app,
http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION,
function_name: str = 'http_app_func'):
function_name: str = 'http_app_func',
prefix: str = ''):
"""Constructor of :class:`AsgiFunctionApp` object.

:param app: asgi app object.
Expand All @@ -3742,6 +3743,7 @@ def __init__(self, app,
self.middleware = AsgiMiddleware(app)
self._add_http_app(self.middleware, function_name)
self.startup_task_done = False
self.prefix = prefix

def __del__(self):
if self.startup_task_done:
Expand All @@ -3768,7 +3770,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()
Expand All @@ -3782,14 +3784,17 @@ async def http_app_func(req: HttpRequest, context: Context):
class WsgiFunctionApp(ExternalHttpFunctionApp):
def __init__(self, app,
http_auth_level: Union[AuthLevel, str] = AuthLevel.FUNCTION,
function_name: str = 'http_app_func'):
function_name: str = 'http_app_func',
prefix = ''):
"""Constructor of :class:`WsgiFunctionApp` object.

:param app: wsgi app object.
:param function_name: function name
"""
super().__init__(auth_level=http_auth_level)

self._add_http_app(WsgiMiddleware(app), function_name)
self.prefix = prefix

def _add_http_app(self,
http_middleware: Union[
Expand All @@ -3813,6 +3818,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)
6 changes: 4 additions & 2 deletions docs/ProgModelSpec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,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.
Expand All @@ -1258,7 +1259,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.
Expand Down
8 changes: 8 additions & 0 deletions tests/decorators/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,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()
Expand All @@ -933,6 +937,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(),
Expand Down