Skip to content

Commit a5acc4f

Browse files
authored
Merge pull request #9 from zitadel/add-zitadel-user-agent
Configure the default user-agent for Telemetry purposes
2 parents bcbd381 + f9fa52d commit a5acc4f

File tree

6 files changed

+63
-13
lines changed

6 files changed

+63
-13
lines changed

.releaserc.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
"prepareCmd": "pip install --no-cache-dir build && python -m build"
1212
}
1313
],
14+
[
15+
"semantic-release-plugin-update-version-in-files",
16+
{
17+
"files": [
18+
"zitadel_client/version.py"
19+
],
20+
"placeholder": "0.0.0"
21+
}
22+
],
1423
[
1524
"@semantic-release/github",
1625
{
@@ -25,7 +34,8 @@
2534
{
2635
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}",
2736
"assets": [
28-
"pyproject.toml"
37+
"pyproject.toml",
38+
"zitadel_client/version.py"
2939
]
3040
}
3141
]

test/test_api_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def test_assert_headers_and_content_type(self):
5353
"headers": {
5454
"Authorization": {
5555
"equalTo": "Bearer mm"
56+
},
57+
"User-Agent": {
58+
"matches": "^zitadel-client/0\\.0\\.0 \\(lang=python; lang_version=[^;]+; os=[^;]+; arch=[^;]+\\)$"
5659
}
5760
}
5861
},

test/test_configuration.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
3+
from zitadel_client.auth.no_auth_authenticator import NoAuthAuthenticator
4+
from zitadel_client.configuration import Configuration
5+
6+
7+
class ConfigurationTest(unittest.TestCase):
8+
9+
def test_user_agent_getter_and_setter(self):
10+
"""
11+
Test user agent getter and setter.
12+
13+
@return void
14+
"""
15+
config = Configuration(NoAuthAuthenticator())
16+
17+
self.assertRegex(config.user_agent,
18+
r"^zitadel-client/0\.0\.0 \(lang=python; lang_version=[^;]+; os=[^;]+; arch=[^;]+\)$")
19+
config.user_agent = "CustomUserAgent/1.0"
20+
self.assertEqual(config.user_agent, "CustomUserAgent/1.0")

zitadel_client/api_client.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ def __init__(
6161
self.configuration = configuration
6262

6363
self.rest_client = rest.RESTClientObject(configuration)
64-
self.default_headers = {}
64+
self.default_headers = {
65+
'User-Agent': configuration.user_agent
66+
}
6567
if header_name is not None:
6668
self.default_headers[header_name] = header_value
67-
self.user_agent = 'OpenAPI-Generator/0.0.1/python'
6869
self.client_side_validation = configuration.client_side_validation
6970

7071
def __enter__(self):
@@ -73,15 +74,6 @@ def __enter__(self):
7374
def __exit__(self, exc_type, exc_value, traceback):
7475
pass
7576

76-
@property
77-
def user_agent(self):
78-
"""User agent for this API client"""
79-
return self.default_headers['User-Agent']
80-
81-
@user_agent.setter
82-
def user_agent(self, value):
83-
self.default_headers['User-Agent'] = value
84-
8577
def set_default_header(self, header_name, header_value):
8678
self.default_headers[header_name] = header_value
8779

zitadel_client/configuration.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
import http.client as httplib
33
import logging
44
import multiprocessing
5+
import platform
56
import sys
67
from logging import FileHandler
78
from typing import Any, Dict, Optional, Union
89

910
from typing_extensions import Self
1011

1112
from zitadel_client.auth.authenticator import Authenticator
12-
13+
from zitadel_client.version import Version
1314

1415
class Configuration:
1516
"""This class contains various settings of the API client.
1617
"""
18+
USER_AGENT = f"zitadel-client/{Version.VERSION} (lang=python; lang_version={platform.python_version()}; os={platform.system()}; arch={platform.machine()})".lower()
1719

1820
def __init__(
1921
self,
@@ -26,6 +28,7 @@ def __init__(
2628
) -> None:
2729
"""Constructor
2830
"""
31+
self._user_agent = Configuration.USER_AGENT
2932
self.authenticator = authenticator
3033
self.api_key_prefix = {}
3134
self.refresh_api_key_hook = None
@@ -195,3 +198,23 @@ def to_debug_report() -> str:
195198
def host(self) -> str:
196199
"""Return generated host."""
197200
return self.authenticator.get_host()
201+
202+
@property
203+
def user_agent(self):
204+
"""
205+
Get the user agent string.
206+
207+
Returns:
208+
str: The current value of the user agent.
209+
"""
210+
return self._user_agent
211+
212+
@user_agent.setter
213+
def user_agent(self, value):
214+
"""
215+
Set the user agent string.
216+
217+
Args:
218+
value (str): The new user agent string to set.
219+
"""
220+
self._user_agent = value

zitadel_client/version.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Version:
2+
VERSION = "0.0.0"

0 commit comments

Comments
 (0)