Skip to content

feat(event-handler): add _registered_api_adapter_async() internal building block#8157

Open
hirenkumar-n-dholariya wants to merge 4 commits intoaws-powertools:developfrom
hirenkumar-n-dholariya:feat/registered-api-adapter-async
Open

feat(event-handler): add _registered_api_adapter_async() internal building block#8157
hirenkumar-n-dholariya wants to merge 4 commits intoaws-powertools:developfrom
hirenkumar-n-dholariya:feat/registered-api-adapter-async

Conversation

@hirenkumar-n-dholariya
Copy link
Copy Markdown

@hirenkumar-n-dholariya hirenkumar-n-dholariya commented Apr 16, 2026

Summary

Add _registered_api_adapter_async() as the async counterpart to the existing _registered_api_adapter(), placed in async_utils.py alongside other async event handler internals.

Changes

  • Add _registered_api_adapter_async() to async_utils.py:
    • Extracts route args from context (same as sync version)
    • Injects Request object if handler declares one (same as sync version)
    • Resolves Depends() parameters (same as sync version)
    • Calls the handler and detects if result is a coroutine via inspect.iscoroutine()
    • If coroutine: awaits it
    • Passes result through _to_response() (sync, CPU-bound — stays sync)
  • Revert change to api_gateway.py — function now lives in async_utils.py
  • Add unit tests covering sync, async, and mixed handler scenarios
  • Nothing calls this in the resolve chain yet — internal building block only

Related issues

fixes #8135, closes #3934

Acknowledgment


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…lding block

- Add async counterpart to _registered_api_adapter()
- Detects if route handler result is a coroutine using inspect.iscoroutine()
- Awaits async handlers; passes sync handlers through unchanged
- _to_response() remains sync (CPU-bound, no async benefit)
- Move import inspect to top-level imports (consistent with file style)
- Nothing calls this in the resolve chain yet — internal building block only

Closes aws-powertools#8135
Part of parent: aws-powertools#3934
Next step: aws-powertools#8137 (public resolve_async()) will wire this in

Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com>
@boring-cyborg
Copy link
Copy Markdown

boring-cyborg bot commented Apr 16, 2026

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

Copy link
Copy Markdown
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hirenkumar-n-dholariya, thanks for working on this!

I see a few things that need to be addressed before we can move forward:

Rebase needed: PR #8138 was merged into develop. That PR extracted all async event handler logic into a dedicated file: aws_lambda_powertools/event_handler/middlewares/async_utils.py. Your branch doesn't have this change yet, so you'll need to rebase on top of current develop.

Move the function to async_utils.py: Given that async_utils.py now exists as the home for async event handler internals (wrap_middleware_async, _run_sync_middleware_in_thread, etc.), _registered_api_adapter_async() should live there too, not in api_gateway.py. This keeps things consistent with the refactoring direction. Take a look at the file and you'll see the pattern.

Tests are missing: The checklist says "Tests are added" but only api_gateway.py was modified. The issue #8135 explicitly asks for unit tests covering sync, async, and mixed handler scenarios. Please add those.

Let me know if you have any questions. Happy to help!

hirenkumar-n-dholariya added a commit to hirenkumar-n-dholariya/powertools-lambda-python that referenced this pull request Apr 17, 2026
…utils.py

refactor(event-handler): move _registered_api_adapter_async to async_utils.py

Per maintainer feedback on aws-powertools#8157 - function belongs in async_utils.py
alongside other async event handler internals (wrap_middleware_async,
_run_sync_middleware_in_thread, etc.)

Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com>
…utils.py

Per maintainer feedback on aws-powertools#8157 - function belongs in async_utils.py alongside other async event handler internals (wrap_middleware_async, _run_sync_middleware_in_thread, etc.)

Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com>
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 17, 2026
Tests cover sync handler, async handler, and mixed scenarios as required by issue aws-powertools#8135

Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com>
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 17, 2026
…utils.py

Per maintainer feedback on aws-powertools#8157 - function belongs in async_utils.py
alongside other async event handler internals (wrap_middleware_async,
_run_sync_middleware_in_thread, etc.)

Signed-off-by: hirenkumar-n-dholariya <hirenkumarnd@gmail.com>
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 17, 2026
@sonarqubecloud
Copy link
Copy Markdown

@hirenkumar-n-dholariya
Copy link
Copy Markdown
Author

Hey @hirenkumar-n-dholariya, thanks for working on this!

I see a few things that need to be addressed before we can move forward:

Rebase needed: PR #8138 was merged into develop. That PR extracted all async event handler logic into a dedicated file: aws_lambda_powertools/event_handler/middlewares/async_utils.py. Your branch doesn't have this change yet, so you'll need to rebase on top of current develop.

Move the function to async_utils.py: Given that async_utils.py now exists as the home for async event handler internals (wrap_middleware_async, _run_sync_middleware_in_thread, etc.), _registered_api_adapter_async() should live there too, not in api_gateway.py. This keeps things consistent with the refactoring direction. Take a look at the file and you'll see the pattern.

Tests are missing: The checklist says "Tests are added" but only api_gateway.py was modified. The issue #8135 explicitly asks for unit tests covering sync, async, and mixed handler scenarios. Please add those.

Let me know if you have any questions. Happy to help!

Hey @leandrodamascena, thanks for the detailed feedback, really appreciate it!

I have addressed all three points:

  1. Rebase - Synced my branch with the latest develop and made changes on top.

  2. Moved to async_utils.py - _registered_api_adapter_async() now lives in
    aws_lambda_powertools/event_handler/middlewares/async_utils.py alongside
    wrap_middleware_async and _run_sync_middleware_in_thread. Also added BedrockResponse to the TYPE_CHECKING imports to match the return type annotation. The function has been removed from api_gateway.py.

  3. Tests added - Added unit tests in `tests/functional/event_handler/test_registered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event_handlers size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: add async api adapter for async resolver support Feature request: Async support for ApiGatewayResolver

2 participants