Skip to content
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

expose http_session_cls in AioConfig #1102

Merged
merged 4 commits into from
Apr 2, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changes
-------

2.12.2 (2024-04-01)
^^^^^^^^^^^^^^^^^^^
* expose configuration of ``http_session_cls`` in ``AioConfig``

2.12.1 (2024-03-04)
^^^^^^^^^^^^^^^^^^^
* fix use of proxies #1070
Expand Down
2 changes: 1 addition & 1 deletion aiobotocore/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.12.1'
__version__ = '2.12.2'
7 changes: 5 additions & 2 deletions aiobotocore/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from botocore.args import ClientArgsCreator

from .config import AioConfig
from .endpoint import AioEndpointCreator
from .endpoint import DEFAULT_HTTP_SESSION_CLS, AioEndpointCreator
from .regions import AioEndpointRulesetResolver
from .signers import AioRequestSigner

Expand Down Expand Up @@ -67,8 +67,10 @@ def get_client_args(
# aiobotocore addition
if isinstance(client_config, AioConfig):
connector_args = client_config.connector_args
http_session_cls = client_config.http_session_cls
else:
connector_args = None
http_session_cls = DEFAULT_HTTP_SESSION_CLS

new_config = AioConfig(connector_args, **config_kwargs)
endpoint_creator = AioEndpointCreator(event_emitter)
Expand All @@ -79,9 +81,10 @@ def get_client_args(
endpoint_url=endpoint_config['endpoint_url'],
verify=verify,
response_parser_factory=self._response_parser_factory,
timeout=(new_config.connect_timeout, new_config.read_timeout),
max_pool_connections=new_config.max_pool_connections,
http_session_cls=http_session_cls,
proxies=new_config.proxies,
timeout=(new_config.connect_timeout, new_config.read_timeout),
socket_options=socket_options,
client_cert=new_config.client_cert,
proxies_config=new_config.proxies_config,
Expand Down
10 changes: 9 additions & 1 deletion aiobotocore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
import botocore.client
from botocore.exceptions import ParamValidationError

from aiobotocore.endpoint import DEFAULT_HTTP_SESSION_CLS


class AioConfig(botocore.client.Config):
def __init__(self, connector_args=None, **kwargs):
def __init__(
self,
connector_args=None,
http_session_cls=DEFAULT_HTTP_SESSION_CLS,
**kwargs,
):
super().__init__(**kwargs)

self._validate_connector_args(connector_args)
self.connector_args = copy.copy(connector_args)
self.http_session_cls = http_session_cls
if not self.connector_args:
self.connector_args = dict()

Expand Down
4 changes: 3 additions & 1 deletion aiobotocore/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from aiobotocore.httpsession import AIOHTTPSession
from aiobotocore.response import StreamingBody

DEFAULT_HTTP_SESSION_CLS = AIOHTTPSession


async def convert_to_response_dict(http_response, operation_model):
"""Convert an HTTP response object to a request dict.
Expand Down Expand Up @@ -295,7 +297,7 @@ def create_endpoint(
response_parser_factory=None,
timeout=DEFAULT_TIMEOUT,
max_pool_connections=MAX_POOL_CONNECTIONS,
http_session_cls=AIOHTTPSession,
http_session_cls=DEFAULT_HTTP_SESSION_CLS,
proxies=None,
socket_options=None,
client_cert=None,
Expand Down
25 changes: 25 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from botocore.exceptions import ParamValidationError, ReadTimeoutError

from aiobotocore.config import AioConfig
from aiobotocore.httpsession import AIOHTTPSession
from aiobotocore.session import AioSession, get_session
from tests.mock_server import AIOServer

Expand Down Expand Up @@ -132,3 +133,27 @@ def test_merge():
assert isinstance(new_config, AioConfig)
assert new_config is not config
assert new_config is not other_config


# Check that it's possible to specify custom http_session_cls
@pytest.mark.moto
@pytest.mark.asyncio
async def test_config_http_session_cls():
class SuccessExc(Exception):
...

class MyHttpSession(AIOHTTPSession):
async def send(self, request):
raise SuccessExc

config = AioConfig(http_session_cls=MyHttpSession)
session = AioSession()
async with AIOServer() as server, session.create_client(
's3',
config=config,
endpoint_url=server.endpoint_url,
aws_secret_access_key='xxx',
aws_access_key_id='xxx',
) as s3_client:
with pytest.raises(SuccessExc):
await s3_client.get_object(Bucket='foo', Key='bar')
Loading