Refactor HttpAsyncHook to support session-based async HTTP operations and simplify LivyAsyncHook#60458
Conversation
This reverts commit f9c503d.
…tmanager in HttpAsyncHook
…f TestLivyAsyncHook
…hook of TestLivyAsyncHook
… and simplify LivyAsyncHook (apache#60458) * refactor: Refactored HttpAsyncHook to easily support session based run operations * fix: Fixed import of LoggingMixin * refactor: LivyAsyncHook now reuses logic from HttpAsyncHook which is more DRY * refactor: Reformatted HttpAsyncHook * refactor: Fixed possible None types for merged_headers * refactor: Changed type of _retryable_error_async method * refactor: Removed unused import * refactor: Moved SessionConfig inside AsyncHttpSession * refactor: Reformatted run method of HttpAsyncHook * refactor: Removed unused import from LivyHook module * Revert "refactor: Moved SessionConfig inside AsyncHttpSession" This reverts commit f9c503d. * refactor: Added docstring for retry_limit and retry_delay parameters * refactor: Reformatted docstring in _retryable_error_async method * refactor: Added docstring for SessionConfig and AsyncHttpSession * refactor: Added warning logging when run attempt fails * refactor: Refactored run_method of LivyAsyncHook * refactor: Refactored unit tests for LivyAsyncHook * refactor: Reformatted AsyncHttpSession * refactor: Reformatted run_method of LivyAsyncHook * refactor: Escape aiohttp.ClientSession in docstring of session contextmanager in HttpAsyncHook * refactor: Also take into extra_options from connection when building AsyncHttpSession * refactor: Fixed mocking of test_run_method_success * refactor: Removed unused imports * refactor: Reorganized imports * refactor: Run method of LivyAsyncHook must internally use session from HttpAsyncHook so it doesn't rely on the error handling of the HttpAsyncHook run method * refactor: Escape reserved words in HttpAsyncHook * refactor: Mock get_async_connection in TestLivyAsyncHook * refactor: Mock get_async_connection in TestLivyAsyncHook should be patched on http hook module * refactor: Mock get_async_connection in TestLivyAsyncHook should be patched on http hook module * refactor: Make sure get_async_connection is mocked with real Connection * refactor: Reformatted Livy unit test * refactor: Add get_async_connection mock in test_run_put_method_with_type_error * refactor: Make sure http provider dependency is set to next release when livy provider is release * refactor: Added TODO on asgiref dependency as I can probably be removed as it will be resolved transiently through common-compat provider * refactor: Removed asgiref dependency in livy provider * refactor: Removed asgiref reference from docs * refactor: Fixed assertion of Connection type in test_build_get_hook of TestLivyAsyncHook * refactor: Don't need to assert connections anymore in test_build_get_hook of TestLivyAsyncHook --------- Co-authored-by: David Blain <david.blain@b-holding.be>
|
Hello @dabla , I notice that in the commit 49f48e3 You force |
You're right that it currently works because pydantic is pulled in transitively through |
|
You are absolutely right. I forgot to mention that my case was a yet non-upgraded Airflow v2 instance (version 2.11.2) where the http provider being upgraded without the explicit dependency makes the HTTPHook fail to import pydantic contents. If anyone searches for this issue, the workaround is to add explicitely |
Thanks for rising this issue @NBardelot. I’ll do a PR to fix this issue and add the dependency to the http provider |
I've created a PR to address this issue, thanks again for pointing this out. |
Was generative AI tooling used to co-author this PR?
This PR refactors
HttpAsyncHookto natively support session-based async HTTP usage, making it easier and safer to perform multiple HTTP requests within a singleaiohttp.ClientSession.As part of this refactor,
LivyAsyncHook(which extendsHttpAsyncHook) is simplified to reuse shared logic, removing duplicated code and avoiding internal state mutation during request execution.Before this change, users who wanted to reuse an
aiohttp.ClientSessionhad to manage the session lifecycle themselves and pass it intoHttpAsyncHook.run(). This resulted in boilerplate-heavy task code and made it harder to apply hook-level configuration consistently.Instead of having to do this:
You will be able to do this instead as the
HttpAsyncHooknow exposes a session-based API that encapsulates session creation, configuration, authentication, and retry logic:This approach:
aiohttp.ClientSessionmanagement in tasks (no more boilerplate code needed).LivyAsyncHook improvements
LivyAsyncHooknow reuses the session and request logic provided byHttpAsyncHooksession context manager._do_api_call_asynchas been removed.self.method) when callingrun_method.Overall behaviour is unchanged, but the implementation is more DRY, predictable, and maintainable
{pr_number}.significant.rstor{issue_number}.significant.rst, in airflow-core/newsfragments.