diff --git a/CHANGELOG.md b/CHANGELOG.md index 51ae8c1e6..02af3586c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 The intended audience of this file is for py42 consumers -- as such, changes that don't affect how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here. +## Unreleased + +### Added + +- A setting to add a prefix to the user-agent. + ## 1.27.2 - 2024-11-27 ### Fixed diff --git a/src/py42/settings/__init__.py b/src/py42/settings/__init__.py index 0a87236b5..c53aaa231 100644 --- a/src/py42/settings/__init__.py +++ b/src/py42/settings/__init__.py @@ -11,16 +11,22 @@ items_per_page = 500 security_events_per_page = 500 +_custom_user_prefix = "" _custom_user_suffix = "" _python_version = f"{sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}" def get_user_agent_string(): - return "py42 {} python {}{}".format( - __version__, _python_version, _custom_user_suffix + return "{}py42/{} python/{}{}".format( + _custom_user_prefix, __version__, _python_version, _custom_user_suffix ) def set_user_agent_suffix(suffix): global _custom_user_suffix - _custom_user_suffix = f" {suffix}" + _custom_user_suffix = f" {suffix}" if suffix else "" + + +def set_user_agent_prefix(prefix): + global _custom_user_prefix + _custom_user_prefix = f"{prefix} " if prefix else "" diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index 3eceddff1..89a33f265 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -5,7 +5,7 @@ import py42.settings as settings from py42.__version__ import __version__ -DEFAULT_USER_AGENT_FORMAT = "py42 {0} python {1}" +DEFAULT_USER_AGENT_FORMAT = "py42/{0} python/{1}" @pytest.fixture @@ -29,3 +29,10 @@ def test_get_user_agent_returns_correct_value_after_setting_suffix(default_user_ assert settings.get_user_agent_string() == f"{default_user_agent} example-suffix" # reset settings to default settings.set_user_agent_suffix("") + + +def test_get_user_agent_returns_correct_value_after_setting_prefix(default_user_agent): + settings.set_user_agent_prefix("example-prefix") + assert settings.get_user_agent_string() == f"example-prefix {default_user_agent}" + # reset settings to default + settings.set_user_agent_prefix("")