From 27db72db43d5737f569aae7aaa2560c2cf38b82a Mon Sep 17 00:00:00 2001 From: Mridang Agarwalla Date: Wed, 16 Apr 2025 10:56:34 +0300 Subject: [PATCH 1/3] Added support for the default user agent which can be used for tracking SDK usage --- .releaserc.json | 12 +++++++++++- test/test_api_client.py | 3 +++ test/test_configuration.py | 20 ++++++++++++++++++++ zitadel_client/api_client.py | 14 +++----------- zitadel_client/configuration.py | 25 ++++++++++++++++++++++++- zitadel_client/version.py | 2 ++ 6 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 test/test_configuration.py create mode 100644 zitadel_client/version.py diff --git a/.releaserc.json b/.releaserc.json index c17c5884..ff060d30 100644 --- a/.releaserc.json +++ b/.releaserc.json @@ -11,6 +11,15 @@ "prepareCmd": "pip install --no-cache-dir build && python -m build" } ], + [ + "semantic-release-plugin-update-version-in-files", + { + "files": [ + "zitadel_client/version.py" + ], + "placeholder": "0.0.0" + } + ], [ "@semantic-release/github", { @@ -25,7 +34,8 @@ { "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}", "assets": [ - "pyproject.toml" + "pyproject.toml", + "zitadel_client/version.py" ] } ] diff --git a/test/test_api_client.py b/test/test_api_client.py index e0242fdb..a7165731 100644 --- a/test/test_api_client.py +++ b/test/test_api_client.py @@ -53,6 +53,9 @@ def test_assert_headers_and_content_type(self): "headers": { "Authorization": { "equalTo": "Bearer mm" + }, + "User-Agent": { + "matches": "^zitadel-client/0\\.0\\.0 \\(lang=python; lang_version=[^;]+; os=[^;]+; arch=[^;]+\\)$" } } }, diff --git a/test/test_configuration.py b/test/test_configuration.py new file mode 100644 index 00000000..67c28f32 --- /dev/null +++ b/test/test_configuration.py @@ -0,0 +1,20 @@ +import unittest + +from zitadel_client.auth.no_auth_authenticator import NoAuthAuthenticator +from zitadel_client.configuration import Configuration + + +class ConfigurationTest(unittest.TestCase): + + def test_user_agent_getter_and_setter(self): + """ + Test user agent getter and setter. + + @return void + """ + config = Configuration(NoAuthAuthenticator()) + + self.assertRegex(config.user_agent, + r"^zitadel-client/0\.0\.0 \(lang=python; lang_version=[^;]+; os=[^;]+; arch=[^;]+\)$") + config.user_agent = "CustomUserAgent/1.0" + self.assertEqual(config.user_agent, "CustomUserAgent/1.0") diff --git a/zitadel_client/api_client.py b/zitadel_client/api_client.py index d686b6a1..b2c003c7 100644 --- a/zitadel_client/api_client.py +++ b/zitadel_client/api_client.py @@ -61,10 +61,11 @@ def __init__( self.configuration = configuration self.rest_client = rest.RESTClientObject(configuration) - self.default_headers = {} + self.default_headers = { + 'User-Agent': configuration.user_agent + } if header_name is not None: self.default_headers[header_name] = header_value - self.user_agent = 'OpenAPI-Generator/0.0.1/python' self.client_side_validation = configuration.client_side_validation def __enter__(self): @@ -73,15 +74,6 @@ def __enter__(self): def __exit__(self, exc_type, exc_value, traceback): pass - @property - def user_agent(self): - """User agent for this API client""" - return self.default_headers['User-Agent'] - - @user_agent.setter - def user_agent(self, value): - self.default_headers['User-Agent'] = value - def set_default_header(self, header_name, header_value): self.default_headers[header_name] = header_value diff --git a/zitadel_client/configuration.py b/zitadel_client/configuration.py index ba532dd5..19f34a91 100644 --- a/zitadel_client/configuration.py +++ b/zitadel_client/configuration.py @@ -2,6 +2,7 @@ import http.client as httplib import logging import multiprocessing +import platform import sys from logging import FileHandler from typing import Any, Dict, Optional, Union @@ -9,11 +10,12 @@ from typing_extensions import Self from zitadel_client.auth.authenticator import Authenticator - +from zitadel_client.version import Version class Configuration: """This class contains various settings of the API client. """ + USER_AGENT = f"zitadel-client/{Version.VERSION} (lang=python; lang_version={platform.python_version()}; os={platform.system()}; arch={platform.machine()})".lower() def __init__( self, @@ -26,6 +28,7 @@ def __init__( ) -> None: """Constructor """ + self._user_agent = Configuration.USER_AGENT self.authenticator = authenticator self.api_key_prefix = {} self.refresh_api_key_hook = None @@ -195,3 +198,23 @@ def to_debug_report() -> str: def host(self) -> str: """Return generated host.""" return self.authenticator.get_host() + + @property + def user_agent(self): + """ + Get the user agent string. + + Returns: + str: The current value of the user agent. + """ + return self._user_agent + + @user_agent.setter + def user_agent(self, value): + """ + Set the user agent string. + + Args: + value (str): The new user agent string to set. + """ + self._user_agent = value diff --git a/zitadel_client/version.py b/zitadel_client/version.py new file mode 100644 index 00000000..c7240117 --- /dev/null +++ b/zitadel_client/version.py @@ -0,0 +1,2 @@ +class Version: + VERSION = "0.0.0" From 88505d6fa7571ce9d9032d94b61be481df946267 Mon Sep 17 00:00:00 2001 From: Mridang Agarwalla Date: Wed, 16 Apr 2025 11:59:49 +0300 Subject: [PATCH 2/3] Test commit to trigger the workflow runs From f9fa52def70e2a05169a07775818dc0800a2ed23 Mon Sep 17 00:00:00 2001 From: Mridang Agarwalla Date: Wed, 16 Apr 2025 12:01:38 +0300 Subject: [PATCH 3/3] Test commit to trigger the workflow runs