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

User-Agent component customization #2996

Merged
merged 4 commits into from
Aug 10, 2023
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
13 changes: 13 additions & 0 deletions botocore/useragent.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ def to_string(self):
return self._value


# This is not a public interface and is subject to abrupt breaking changes.
# Any usage is not advised or supported in external code bases.
try:
from botocore.customizations.useragent import modify_components
jonathan343 marked this conversation as resolved.
Show resolved Hide resolved
except ImportError:
# Default implementation that returns unmodified User-Agent components.
def modify_components(components):
return components


class UserAgentString:
"""
Generator for AWS SDK User-Agent header strings.
Expand Down Expand Up @@ -271,6 +281,9 @@ def to_string(self):
*self._build_app_id(),
*self._build_extra(),
]

components = modify_components(components)

return ' '.join([comp.to_string() for comp in components])

def _build_sdk_metadata(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_useragent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@

import pytest

import botocore.useragent
from botocore import __version__ as botocore_version
from botocore.config import Config
from botocore.useragent import (
UserAgentString,
sanitize_user_agent_string_component,
)
from tests import mock

from .. import requires_crt


# Returns a list of unmodified User-Agent components.
def unmodified_components(components):
return components


@pytest.mark.parametrize(
'raw_str, allow_hash, expected_str',
[
Expand Down Expand Up @@ -52,6 +59,9 @@ def test_sanitize_ua_string_component(raw_str, allow_hash, expected_str):
assert actual_str == expected_str


@mock.patch.object(
botocore.useragent, 'modify_components', unmodified_components
)
def test_basic_user_agent_string():
ua = UserAgentString(
platform_name='linux',
Expand Down Expand Up @@ -113,6 +123,9 @@ def test_shared_test_case():
assert indices == list(sorted(indices)), 'Elements were found out of order'


@mock.patch.object(
botocore.useragent, 'modify_components', unmodified_components
)
def test_user_agent_string_with_missing_information():
# Even when collecting information from the environment fails completely,
# some minimal string should be generated.
Expand Down