-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor handler invocation into its own file for improved organizati…
…on: handler_invoker.py
- Loading branch information
1 parent
858e6ac
commit 77415e6
Showing
2 changed files
with
30 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import inspect | ||
from typing import Dict, Any | ||
|
||
from pyblaze.requests import Request | ||
from pyblaze.utils.param_converter import convert_param_type | ||
|
||
|
||
async def invoke_handler( | ||
handler, request: Request, scope: Dict[str, Any], params=None | ||
): | ||
handler_signature = inspect.signature(handler) | ||
handler_params = {} | ||
for param_name, param in handler_signature.parameters.items(): | ||
if param_name == "request": | ||
handler_params["request"] = request | ||
elif param_name == "scope": | ||
handler_params["scope"] = scope | ||
elif param_name in params: | ||
handler_params[param_name] = convert_param_type( | ||
params[param_name], param.annotation | ||
) | ||
elif param.default != inspect.Parameter.empty: | ||
handler_params[param_name] = param.default | ||
else: | ||
handler_params[param_name] = None | ||
|
||
return await handler(**handler_params) |