From c772f4b1ca6416bdff92388c3048523f377343d4 Mon Sep 17 00:00:00 2001 From: Jakob Keller <57402305+jakob-keller@users.noreply.github.com> Date: Fri, 19 Jan 2024 23:27:11 +0100 Subject: [PATCH] send project-specific `User-Agent` HTTP header #853 (#1075) --- CHANGES.rst | 4 ++++ aiobotocore/__init__.py | 2 +- aiobotocore/session.py | 16 ++++++++++++++-- tests/test_session.py | 9 ++++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d28641a6..b666e629 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,10 @@ Changes ------- +2.11.0 (2024-01-19) +^^^^^^^^^^^^^^^^^^^ +* send project-specific `User-Agent` HTTP header #853 + 2.10.0 (2024-01-18) ^^^^^^^^^^^^^^^^^^^ * bump botocore dependency specification diff --git a/aiobotocore/__init__.py b/aiobotocore/__init__.py index e2e6e4b5..fb7a749a 100644 --- a/aiobotocore/__init__.py +++ b/aiobotocore/__init__.py @@ -1 +1 @@ -__version__ = '2.10.0' +__version__ = '2.11.0' diff --git a/aiobotocore/session.py b/aiobotocore/session.py index 29fd2bb2..8d31a92d 100644 --- a/aiobotocore/session.py +++ b/aiobotocore/session.py @@ -1,10 +1,12 @@ -from botocore import UNSIGNED, translate +from botocore import UNSIGNED +from botocore import __version__ as botocore_version +from botocore import translate from botocore.exceptions import PartialCredentialsError from botocore.session import EVENT_ALIASES, ServiceModel from botocore.session import Session as _SyncSession from botocore.session import UnknownServiceError, copy -from . import retryhandler +from . import __version__, retryhandler from .client import AioBaseClient, AioClientCreator from .configprovider import AioSmartDefaultsConfigStoreFactory from .credentials import AioCredentials, create_credential_resolver @@ -43,6 +45,16 @@ def __init__( session_vars, event_hooks, include_builtin_handlers, profile ) + self._set_user_agent_for_session() + + def _set_user_agent_for_session(self): + # Mimic approach taken by AWS's aws-cli project + # https://github.com/aws/aws-cli/blob/b862122c76a3f280ff34e93c9dcafaf964e7bf9b/awscli/clidriver.py#L84 + + self.user_agent_name = 'aiobotocore' + self.user_agent_version = __version__ + self.user_agent_extra = 'botocore/%s' % botocore_version + def _create_token_resolver(self): return create_token_resolver(self) diff --git a/tests/test_session.py b/tests/test_session.py index 4c3f5874..8bae67e2 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -3,7 +3,7 @@ import pytest from _pytest.logging import LogCaptureFixture -from aiobotocore import httpsession +from aiobotocore import __version__, httpsession from aiobotocore.config import AioConfig from aiobotocore.session import AioSession @@ -52,3 +52,10 @@ async def test_retry( await client.get_object(Bucket='foo', Key='bar') assert 'sleeping for' in caplog.text + + +@pytest.mark.moto +async def test_set_user_agent_for_session(session: AioSession): + assert session.user_agent_name == "aiobotocore" + assert session.user_agent_version == __version__ + assert session.user_agent_extra.startswith("botocore/")