Skip to content

Commit

Permalink
retain client.meta.config.user_agent as source of computed user agent…
Browse files Browse the repository at this point in the history
… string
  • Loading branch information
jonemo committed Jun 27, 2023
1 parent d33a106 commit 03db799
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
14 changes: 13 additions & 1 deletion botocore/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ def get_client_args(
client_ua_creator = self._session_ua_creator.with_client_config(
new_config
)
if client_config:
new_config._supplied_user_agent = client_config.user_agent

return {
'serializer': serializer,
Expand Down Expand Up @@ -217,12 +219,23 @@ def compute_client_args(
s3_config=s3_config,
)
endpoint_variant_tags = endpoint_config['metadata'].get('tags', [])

# Some third-party libraries expect the final user-agent string in
# ``client.meta.config.user_agent``. To maintain backwards
# compatibility, the preliminary user-agent string (before any Config
# object modifications and without request-specific user-agent
# components) is stored in the new Config object's ``user_agent``
# property but not used by Botocore itself.
preliminary_ua_string = self._session_ua_creator.with_client_config(
client_config
).to_string()
# Create a new client config to be passed to the client based
# on the final values. We do not want the user to be able
# to try to modify an existing client with a client config.
config_kwargs = dict(
region_name=endpoint_config['region_name'],
signature_version=endpoint_config['signature_version'],
user_agent=preliminary_ua_string,
)
if 'dualstack' in endpoint_variant_tags:
config_kwargs.update(use_dualstack_endpoint=True)
Expand All @@ -239,7 +252,6 @@ def compute_client_args(
client_cert=client_config.client_cert,
inject_host_prefix=client_config.inject_host_prefix,
tcp_keepalive=client_config.tcp_keepalive,
user_agent=client_config.user_agent,
user_agent_extra=client_config.user_agent_extra,
user_agent_appid=client_config.user_agent_appid,
)
Expand Down
1 change: 1 addition & 0 deletions botocore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class Config:
('use_fips_endpoint', None),
('defaults_mode', None),
('tcp_keepalive', None),
('_supplied_user_agent', None),
]
)

Expand Down
15 changes: 11 additions & 4 deletions botocore/useragent.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,15 @@ def to_string(self):
"""
Build User-Agent header string from the object's properties.
"""
if self._client_config and self._client_config.user_agent:
return self._build_legacy_ua_string()
config_ua_override = None
if self._client_config:
if hasattr(self._client_config, '_supplied_user_agent'):
config_ua_override = self._client_config._supplied_user_agent
else:
config_ua_override = self._client_config.user_agent

if config_ua_override is not None:
return self._build_legacy_ua_string(config_ua_override)

components = [
*self._build_sdk_metadata(),
Expand Down Expand Up @@ -461,8 +468,8 @@ def _build_extra(self):
)
return extra

def _build_legacy_ua_string(self):
components = [self._client_config.user_agent]
def _build_legacy_ua_string(self, config_ua_override):
components = [config_ua_override]
if self._session_user_agent_extra:
components.append(self._session_user_agent_extra)
if self._client_config.user_agent_extra:
Expand Down

0 comments on commit 03db799

Please sign in to comment.